You need to sign in to do that
Don't have an account?
Hermann Ouré
Field Update Trigger when creating a record on related custom object
Hello,
I have a lookup field (First_JIRA_Issue_Number__c) on Case object that needs to be updated with the Jira Number when the related issue is created.
Basically from a Case, I have the possibility to create a Jira Issue (object: JIRA_Issue__c).
When the Jira Issue is created, the field First_JIRA_Issue_Number__c needs to be updated with the Jira Number. In this example the field First Jira Issue Number on Case is updated with JIRA000692
I have created a class and a trigger but it does work.
Could someone help.
Thanks
Apex Class
public class CaseManager { public static void updateJiraIssue(List<JIRA_Issue__c> lstJira) { Set<Id> jiraIds = new Set<Id>(); for(JIRA_Issue__c jissue : lstJira){ if(jissue.Case__c != null){ jiraIds.add(jissue.Id); } } List<Case> lstCase = new List<Case>([SELECT Id, First_JIRA_Issue_Number__c,(SELECT Id, Case__c FROM JIRA_Issues__r) FROM Case WHERE First_JIRA_Issue_Number__c IN: jiraIds]); System.debug('##### lstCase ' + lstCase.Size()); if(lstCase.size()>0){ for(Case c: lstCase){ List<JIRA_Issue__c> lstJiraIssue = new List<JIRA_Issue__c>(); lstJiraIssue = c.JIRA_Issues__r; if(lstJiraIssue.size() > 0){ for(JIRA_Issue__c ji: lstJiraIssue){ if(ji.Case__c == c.Id){ c.First_JIRA_Issue_Number__c = ji.Id; } } } } update lstCase; } } }Apex Trigger:
trigger CaseJiraIssueNumber on JIRA_Issue__c (after insert) { if(trigger.isAfter){ CaseManager.updateJiraIssue(Trigger.New); } }
Apex Class Apex Trigger: