function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mac D GreeneMac D Greene 

Checkbox in trigger

I am trying to alter the code provided by Service Rocket. We need the trigger to fire when the approval process tied to Custom_Dev__C is approved. I have little experience with writing Apex or any code. I understand that it’s creating the Jira issue when a new case is created.
 
  1. Compare in Trigger and have this in IF Condition whether [Approved__c == True]
    1. Approved__c is a checkbox that is subject to a field update after going through the approval process.
Could someone please help me make the necessary changes to get this working? Or explain where to insert the code to make it work.
trigger CreateIssue on Custom_Dev__C (before insert) {
// Check whether current user is not JIRA agent so that we don't create an infinite loop.
  if (JIRA.currentUserIsNotJiraAgent()) {
    for (Custom_Dev__C c : Trigger.new) {
      // Define parameters to be used in calling Apex Class
      String objectType =  ‘Custom_Dev__C’; // Please change this according to the object type
      String objectId = c.id;
      String projectKey = 'CustomDev'; //Please change this according to the JIRA project key
      String issueType = '10';   //Please change this according to the JIRA issue type ID
      // Calls the actual callout to create the JIRA issue.
      JIRAConnectorWebserviceCalloutCreate.createIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId, projectKey, issueType);
    }
  }
   
}
 
 
Debanjan SharmaDebanjan Sharma
Hi Mac,

As per your requirement you need fire the trigger in after update context instead before insert. I have mentioned the modified code below-
 
trigger CreateIssue on Custom_Dev__C (after update) {

  // Check whether current user is not JIRA agent so that we don't create an infinite loop.
  if (JIRA.currentUserIsNotJiraAgent()) {

    for (Custom_Dev__C c : Trigger.new) {

    //Check each record if approved by approval process
    if(c.Approved__c){

      // Define parameters to be used in calling Apex Class
      String objectType =  ‘Custom_Dev__C’; // Please change this according to the object type
      String objectId = c.id;
      String projectKey = 'CustomDev'; //Please change this according to the JIRA project key
      String issueType = '10';   //Please change this according to the JIRA issue type ID
      // Calls the actual callout to create the JIRA issue.
      JIRAConnectorWebserviceCalloutCreate.createIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId, projectKey, issueType);

    }
      
    }
  }
   
}

Regards,
Debanjan
Naval Sharma4Naval Sharma4
Hi Mac,

Your code should only fire when this checkbox is checked from the state of unchecked then you need to make some amendments in your code. Because your code will keep creating JIRA issues whenever an update is made on any other field on the record and I think that's not what you want.
Please use the following code.
trigger CreateIssue on Custom_Dev__C (after update) {

  // Check whether current user is not JIRA agent so that we don't create an infinite loop.
  if (JIRA.currentUserIsNotJiraAgent()) {

    for (Custom_Dev__C c : Trigger.new) {

    //Check each record if approved by approval process
    if(c.Approved__c && !Trigger.oldMap(c.Id).Approved__c ){
      // Define parameters to be used in calling Apex Class
      String objectType =  ‘Custom_Dev__C’; // Please change this according to the object type
      String objectId = c.id;
      String projectKey = 'CustomDev'; //Please change this according to the JIRA project key
      String issueType = '10';   //Please change this according to the JIRA issue type ID
      // Calls the actual callout to create the JIRA issue.
      JIRAConnectorWebserviceCalloutCreate.createIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId, projectKey, issueType);

     }
      
    }
  }
   
}

I hope JIRAConnectorWebserviceCalloutCreate.createIssue​ is making a future call as you can perform callouts in a future call only.
Mac D GreeneMac D Greene
global class JIRAConnectorWebserviceCalloutCreate {
 
    @future (callout=true) WebService static void createIssue(String baseUrl, String systemId, String objectType, String objectId, String projectKey, String issueType) {
        try {
            HttpRequest req = buildRequest(baseUrl, JIRA.username, JIRA.password, systemId, objectType, objectId, projectKey, issueType);
            JIRA.sendRequest(req);
        } catch(System.CalloutException e) {
            System.debug(e);
        }
    }
 
    // Constructs request needed to create a JIRA issue from provided parameters.
    @testVisible private static HttpRequest buildRequest(String baseUrl, String username, String password,
                                           String systemId, String objectType, String objectId,
                                           String projectKey, String issueType) {
        HttpRequest req = new HttpRequest();
        String basicAuthHeader = JIRA.authHeader(username, password);
        String endpoint = getEndpoint(baseUrl, systemId, objectType, objectId);
        req.setHeader('Authorization', basicAuthHeader);
        req.setHeader('Content-Type','application/json');
        req.setMethod('POST');
        req.setEndpoint(endpoint);
        req.setBody('{"project":"' + projectKey + '", "issueType":"' + issueType + '"}');
        return req;
    }
 
    // Creates the endpoint to create the issue from provided parameters.
    private static String getEndpoint(String baseUrl, String systemId, String objectType, String objectId) {
        return baseUrl + '/rest/customware/connector/1.0/' + systemId + '/' + objectType + '/' + objectId + '/issue/create.json';
    }
 
}

Thank you for your help here is that Callout class
Mac D GreeneMac D Greene
trigger CreateIssue on CDR__c (after update) {

  // Check whether current user is not JIRA agent so that we don't create an infinite loop.
  if (JIRA.currentUserIsNotJiraAgent()) {

    for (CDR__c c : Trigger.new) {

    //Check each record if approved by approval process
    if(c.Approved__c && !Trigger.oldMap(c.Id).Approved__c){ //ERROR Variable does not exist: Trigger at line 9 column 25	

      // Define parameters to be used in calling Apex Class
      String objectType = 'CDR__c'; // Please change this according to the object type
      String objectId = c.id;
      String projectKey = 'CDR'; //Please change this according to the JIRA project key
      String issueType = '10001';   //Please change this according to the JIRA issue type ID
      // Calls the actual callout to create the JIRA issue.
      JIRAConnectorWebserviceCalloutCreate.createIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId, projectKey, issueType);

     }
      
    }
  }
   
}

So I first followed the response from Sharma and it did go into the infinite loop and created 117 Jira issues. Then I tried to follow your advice, even after 20 attempts I still could not get it to work. Do I need to define the oldMap somewhere?
  • if(c.Approved__c && !Trigger.oldMap(c.Id).Approved__c){  //ERROR Variable does not exist: Trigger at line 9 column 25
Sergii Grushai 25Sergii Grushai 25
Please have a look at this invocable Apex class to create jira issue, which can be invoced by Salesforce Process Builder: 
(taken from "7 Ways to Integrate Salesforce and Jira" (https://www.peeklogic.com/article/7-ways-to-integrate-salesforce-and-jira/))
@InvocableMethod(label='Create Jira Issue' description='Create Jira Issue from Salesforce.')

public static void createJiraIssue(List ids) {
try{
createIssue(ids);
}
catch(Exception ex){
System.debug('ERROR:' + ex.getMessage());
}
}
@Future(callout=true)
public static void createIssue(List ids){
List caseList = [SELECT Subject,Summary__c, Assignee__c, Reporter__c,Priority FROM Case WHERE id = :ids LIMIT 1];
System.debug('caseList::::::'+caseList);
if(caseList.size()>0){
String isssueSummary = caseList[0].Summary__c;
String issueAssignee = caseList[0].Assignee__c;
String issueReporter = caseList[0].Reporter__c;
Jira_Credential__c jiraCreds = [Select id, Jira_API_Token__c,Jira_Password__c,Jira_URL__c,Jira_UserName__c from Jira_Credential__c Limit 1];
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
//Construct Authorization and Content header
Blob headerValue = Blob.valueOf(jiraCreds.Jira_UserName__c+':'+jiraCreds.Jira_API_Token__c);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('content-type','application/json');
req.setHeader('accept','application/json');
//Set Method and Endpoint and Body
req.setMethod('POST');
//req.setHeader('cache-control','no-cache');
//Construct Endpoint
req.setBody('{ "fields": { "project": { "id": "10000" }, "summary": "'+isssueSummary+'", "issuetype": { "id": "10001" }, "assignee": { "id": "'+issueAssignee+'" }, "reporter": { "id": "'+issueReporter+'" }, "priority": { "id": "1" }, "labels": [ "bugfix", "blitz_test" ], "description": "description", "duedate": "2021-08-11" } }');                req.setEndpoint(jiraCreds.DemoNa__Jira_URL__c+'/rest/api/2/issue');
res = http.send(req);
System.debug('ResponseBody::'+res.getBody());
}
}
}