Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This guide will help you to configure Automatic Jira issue creation from Salesforce by configuring the required Salesforce triggers. This automation has some important prerequisites and conditions, as noted in the section below.

Prerequisites

  1. The Salesforce Package must be installed and configured.
  2. You will need to configure the available objects and fields before proceeding with other steps. 
  3. You will also need to confirm that you can manually create a Jira Issue from Salesforce. (warning) If the manual method does not work, then the automation will not work either.
  4. Note that Automatic Jira issue creation will work only if:
    1. Salesforce objects, from which you want to create Jira issues automatically, are configured to be available for the authorized connection.
    2. Apex triggers for the respective objects are installed.

Create and deploy Apex triggers

You need Apex triggers to automatically create Jira issues from Salesforce objects. These triggers have to be created and deployed manually in Salesforce. For Salesforce Enterprise and Unlimited edition, the standard deployment of Apex triggers is required (see Salesforce documentation).

The following code samples show how to write triggers for Cases. To create triggers for other objects, replace the object name in the code accordingly.

Automatically create a Jira issue after a Salesforce Object is inserted

...

deckIdAutoCreate

...

titleCloud

...

titleServer

...

Cloud

Code Block
languagejava
themeEclipse
titleCaseInsertedTrigger
linenumberstrue
trigger CaseInsertedTrigger on Case (after insert) {
    JCFS.API.createJiraIssue('1000', '10002');
}

Server

Code Block
languagejava
themeEclipse
titleCaseInsertedTrigger
linenumberstrue
trigger CaseInsertedTrigger on Case (after insert) {
    JSFS.API.createJiraIssue('1000', '10002');
}

Note that you need to, respectively, hardcode the Jira project id and Jira issue type id. You can get these values from the Jira REST API. In future versions of the package, there will be a feature helping admins generate the full code for the trigger from a template.

Automatically create a Jira issue after a Salesforce Object is updated

You can also create an issue automatically after an object is updated (e. g. in an after update trigger) as follows:

...

deckIdAutoCreate

...

Cloud

For cloud administrators, you can choose either:

Code Block
languagejava
themeEclipse
titleCaseUpdatedTrigger
linenumberstrue
trigger CaseUpdatedTrigger on Case (after update) {
    JCFS.API.createJiraIssueWithDefaultPostAction('1000', '10002');
}
  • A trigger that omits any post action once the Jira issue is created

    Code Block
    languagejava
    themeEclipse
    titleCaseUpdatedTrigger

...

...

  • linenumbers

...

titleServer
  • true
    trigger CaseUpdatedTrigger on Case (after update) {
        JCFS.API.createJiraIssue('1000', '10002');
    }

Caveats

The above-mentioned method works only if you call the API directly from within a trigger. If you need to call the API indirectly, for instance from a helper method, see the advanced usage section below.

Apex test class for the triggers

Once triggers are created, they need a certain level of test coverage to be allowed to deploy to the Production environment. The package contains a test helper which tests the whole scenario. You simply need to add a unit test for each trigger and call the test helper method. Depending on the trigger event (after insert or after update) you will have to call the corresponding test helper method. A sample of both tests is shown in the following example:

...

deckIdAutoCreateTest

...

titleCloud

...

titleServer

...

Just as

Cloud

Code Block
languagejava
themeEclipse
titleCaseTriggerTest
linenumberstrue
@isTest public class CaseTriggerTest {
    @isTest static void caseAfterInsertTest() {
    	JCFS.JiraTriggerTestHelper.testAfterInsert('Case');
	}
	@isTest static void caseAfterUpdateTest() {
    	JCFS.JiraTriggerTestHelper.testAfterUpdate('Case');
	}
}

Server

Code Block
languagejava
themeEclipse
titleCaseTriggerTest
linenumberstrue
@isTest public class CaseTriggerTest {
    @isTest static void caseAfterInsertTest() {
    	JSFS.JiraTriggerTestHelper.testAfterInsert('Case');
	}
	@isTest static void caseAfterUpdateTest() {
    	JSFS.JiraTriggerTestHelper.testAfterUpdate('Case');
	}
}

Just as with the trigger itself, you need a test for each object trigger. All you have to do is replace replace Case in the above code with the actual object name.

...

You will need to re-run the Apex class if you ever deactivate and reactivate the trigger.

Custom objects

The Apex API and test helper methods work as well with any custom object. You just need to find out the object name (e.g. 'MyObject__c') and use it in the trigger code as documented above. If you are testing a Custom Object, provide the fully qualified name (include your namespace in the Salesforce Object Name, e. g. 'yournamespace__MyObject__c')

Advanced Usage

Our Apex API supports selective issue creation so that you can avoid the automatic creation of Jira issues from unwanted Salesforce objects. For this purpose you can use the following API method:

...

deckIdSelectiveCreate

...


...

Cloud

For administrators, you can choose either:

A trigger that automatically creates a new Jira issue and the post action respects the settings after creating the Jira issue. This was an enhancement introduced JCFS 4.7 onwards to deliver on a popular request at our idea portal

Code Block
languagejava
themeEclipse
titleSignature of JCFS.API.createJiraIssueWithDefaultPostAction
linenumberstrue
trigger CaseInsertedTrigger on Case (after insert) {
    List<Case> toBeCreated = new List<Case>();
    for(Case c : Trigger.new) {
        toBeCreated.add(c);
    }
    JCFS.API.createJiraIssueWithDefaultPostAction('10300', '10001', toBeCreated, Trigger.old);
}

A trigger that omits any post action once the Jira issue is created

truejavaEclipse
Code Block
languagejava
themeEclipse
titleSignature of JCFS.API.createJiraIssue
Tab
titleServer
linenumberstrue
trigger CaseInsertedTrigger on Case (after insert) {
    List<Case> toBeCreated = new List<Case>();
    for(Case c : Trigger.new) {
        toBeCreated.add(c);
    }
    JCFS.API.createJiraIssue('10300', '10001', toBeCreated, Trigger.old);
}

Server

Code Block
languagejava
themeEclipse
titleSignature of JCFS.API.createJiraIssue
linenumberstrue
JSFS.API.createJiraIssue(String JiraProjectId, String JiraIssueTypeId, List<SObject> newObjects, List<SObject> oldObjects)


Pass all the objects you want to create a Jira issue from as newObjects. Note that all the objects in this list must be of the same runtime type. So we recommend using a concrete type, e.g. List<Case> or List<Account> for the variable, you pass as this parameter. The oldObjects parameter is not used at the moment so you can pass Trigger.old or an empty list for it.

For instance, if you want to create Jira issues only from Cases whose summaries start with Post you you can use the following trigger code:

...

deckIdSelectiveCreateTest

...

Cloud

javaEclipsejavaEclipse
Code Block
languagejava
themeEclipse
title
Cloud
Selective automatic JIRA issue creation
linenumberstrue
Tab
titleServer
trigger CaseInsertedTrigger on Case (after insert) {
    List<Case> toBeCreated = new List<Case>(); // proper runtime type, List<SObject> won't work
    for(Case c : Trigger.new) {
        if(c.Subject.startsWithIgnoreCase('Post')) {
            toBeCreated.add(c);
        }
    }
    JCFS.API.createJiraIssue('10300', '10001', toBeCreated, Trigger.old);
}

Server

Code Block
languagejava
themeEclipse
titleSelective automatic JIRA issue creation
linenumberstrue
trigger CaseInsertedTrigger on Case (after insert) {
    List<Case> toBeCreated = new List<Case>();
    for(Case c : Trigger.new) {
        toBeCreated.add(c);
    }
    JSFS.API.createJiraIssueWithDefaultPostAction('10300', '10001', toBeCreated, Trigger.old);
} 
Info

Give us your feedback

We're gathering feedback on implementing an easier method of adding triggers for the Connector. Visit our Ideas page to leave your comments.

...