+ Start a Discussion
J80J80 

Trigger Translation

Hi Folks
I know tihs is a big ask but is anyone able to translate this for me?

Trigger TKR_UserStoryTrigger on TKR_User_Story__c(before insert, before update, after update) {
    if (trigger.isBefore && trigger.isInsert) {
        TKR_UserStoryTriggerClass.populateAppStakeholders(trigger.new);
    }
    if (trigger.isAfter && trigger.isUpdate) {
        //custom AC history
        Map < Id, String > mapStoryAC = new Map < Id, String > ();
        Map < Id, String > mapStoryCC = new Map < Id, String > ();
        Map < Id, TKR_User_Story__c > mapStoryInsertFields = new Map < Id, TKR_User_Story__c > ();
        Map < Id, TKR_User_Story__c > mapStoryUpdateFields = new Map < Id, TKR_User_Story__c > ();
        Map < Id, TKR_User_Story__c > mapTimeStampFields = new Map < Id, TKR_User_Story__c > ();
        for (TKR_User_Story__c thisUS: trigger.new) {
            if (thisUS.Acceptance_Criteria__c != trigger.oldMap.get(thisUS.id).Acceptance_Criteria__c) {
                mapStoryAC.put(thisUS.id, trigger.oldMap.get(thisUS.id).Acceptance_Criteria__c);
            }
            if (thisUS.Compliance_Criteria__c != trigger.oldMap.get(thisUS.id).Compliance_Criteria__c) {
                mapStoryCC.put(thisUS.id, trigger.oldMap.get(thisUS.id).Compliance_Criteria__c);
            }
            if ((trigger.oldMap.get(thisUS.id).Approval_Status__c != 'Pending Approval' && thisUS.Approval_Status__c == 'Pending Approval')) {
                mapStoryInsertFields.put(thisUS.id, thisUS);
            }
            if ((thisUS.Approval_Status__c != trigger.oldMap.get(thisUS.id).Approval_Status__c && trigger.oldMap.get(thisUS.id).Approval_Status__c == 'Pending Approval' && (thisUS.Approval_Status__c == 'Approved' || thisUS.Approval_Status__c == 'Rejected'))) {
                mapStoryUpdateFields.put(thisUS.id, thisUS);
            }
        }
        if (!mapStoryInsertFields.isEmpty()) {
            TKR_UserStoryTriggerClass.createNewApprovalHistoryRecord(mapStoryInsertFields);
        }
        TKR_UserStoryTriggerClass.createNewACHistoryRecord(mapStoryAC, mapStoryCC);
        //autoburndown
        Set < Id > affectedSprints = new Set < Id > ();
        for (TKR_User_Story__c us: trigger.new) {
            affectedSprints.add(us.Allocated_Sprint__c);
        }
        for (Id sprint: affectedSprints) {
            TKR_automateBurnDown.updateSprintBurndown(sprint);
        }
    }
}
 

 
Best Answer chosen by J80
Himanshu ParasharHimanshu Parashar
Hi,

Please find inline comments
 
Trigger TKR_UserStoryTrigger on TKR_User_Story__c(before insert, before update, after update) {
    if (trigger.isBefore && trigger.isInsert) {
        TKR_UserStoryTriggerClass.populateAppStakeholders(trigger.new);
    }

    
    if (trigger.isAfter && trigger.isUpdate) {
        //custom AC history
        Map < Id, String > mapStoryAC = new Map < Id, String > (); //Map to hold data of Acceptance criteria value
        Map < Id, String > mapStoryCC = new Map < Id, String > (); //Map to hold Compliance criteria valus
        Map < Id, TKR_User_Story__c > mapStoryInsertFields = new Map < Id, TKR_User_Story__c > ();  
        Map < Id, TKR_User_Story__c > mapStoryUpdateFields = new Map < Id, TKR_User_Story__c > ();
        Map < Id, TKR_User_Story__c > mapTimeStampFields = new Map < Id, TKR_User_Story__c > ();

      //Iterating on all records which are going to update
        for (TKR_User_Story__c thisUS: trigger.new) {
            //if current record Acceptance_Criteria__c value doesn't match with previous value of record then put that in map
            if (thisUS.Acceptance_Criteria__c != trigger.oldMap.get(thisUS.id).Acceptance_Criteria__c) {
                mapStoryAC.put(thisUS.id, trigger.oldMap.get(thisUS.id).Acceptance_Criteria__c);
            }
            
             //if current record Compliance_Criteria__c value doesn't match with previous value of record then put that in map
            if (thisUS.Compliance_Criteria__c != trigger.oldMap.get(thisUS.id).Compliance_Criteria__c) {
                mapStoryCC.put(thisUS.id, trigger.oldMap.get(thisUS.id).Compliance_Criteria__c);
            }
            //Check for approval status field value with previous value of record.
            if ((trigger.oldMap.get(thisUS.id).Approval_Status__c != 'Pending Approval' && thisUS.Approval_Status__c == 'Pending Approval')) {
                mapStoryInsertFields.put(thisUS.id, thisUS);
            }
            if ((thisUS.Approval_Status__c != trigger.oldMap.get(thisUS.id).Approval_Status__c && trigger.oldMap.get(thisUS.id).Approval_Status__c == 'Pending Approval' && (thisUS.Approval_Status__c == 'Approved' || thisUS.Approval_Status__c == 'Rejected'))) {
                mapStoryUpdateFields.put(thisUS.id, thisUS);
            }
        }
        //if records available in mapStoryInsertFields then create Approval history record.
        if (!mapStoryInsertFields.isEmpty()) {
            TKR_UserStoryTriggerClass.createNewApprovalHistoryRecord(mapStoryInsertFields);
        }

         //if records available in mapStoryInsertFields then create Acceptance criteria history record.
        TKR_UserStoryTriggerClass.createNewACHistoryRecord(mapStoryAC, mapStoryCC);
        //autoburndown
        Set < Id > affectedSprints = new Set < Id > ();
        for (TKR_User_Story__c us: trigger.new) {
            affectedSprints.add(us.Allocated_Sprint__c);
        }
        //update effected sprints
        for (Id sprint: affectedSprints) {
            TKR_automateBurnDown.updateSprintBurndown(sprint);
        }
    }
}

Does it makes sense ?