+ Start a Discussion
santhiya duraisanthiya durai 

How to merge two trigger into one trigger

I have two triggers , I need to merge these two trigger into one trigger. Can anybody please help me on that.

Trigger 1:

trigger APTS_AgreementTrigger on Apttus__APTS_Agreement__c (after update, before update) { System.Debug('APTS_AgreementTrigger: Entering'); public static boolean createCase = true; public static boolean runOnce = true; APTS_AgreementTriggerHandler handler = new APTS_AgreementTriggerHandler(); //After Update logic if(Trigger.isUpdate && Trigger.isAfter){ if(runOnce){ handler.handleAfterUpdateEvents(Trigger.newMap, trigger.new, Trigger.oldMap); runOnce = false; } } //------------------------------------------------------------------------------- System.Debug('Checking for Trigger Status: isInsert:' + Trigger.isInsert + ' isBefore:' + Trigger.isBefore + ' isAfter:' + Trigger.isAfter + ' isUpdate: ' + Trigger.isUpdate ); final static string PMODTRIGGERCONTROL = 'P-MOD TRIGGER CONTROL'; //Trigger control - proceed if trigger switched ON if(ActiveTriggers__c.getInstance(PMODTRIGGERCONTROL) != null && ActiveTriggers__c.getInstance(PMODTRIGGERCONTROL).APTS_AgreementTrigger__c) { //Create a Case for any agreement generated from Aurora Integration System.Debug('Calling to Create a Case for integration request : createCase : ' + createCase); System.Debug('Checking for Trigger Status: : runonce : ' + runOnce + ' isInsert ' + Trigger.isInsert + ' : isAfter ' + + Trigger.isAfter + ' : isUpdate ' + + Trigger.isUpdate + ' : ' ); if(createCase) { //if(Trigger.isUpdate && Trigger.isAfter) if(Trigger.isUpdate && Trigger.isBefore) { System.Debug('Trigger.isInsert && Trigger.isBefore'); //System.Debug('Trigger.isInsert && Trigger.isAfter'); List<Apttus__APTS_Agreement__c> agreementList = trigger.new; System.Debug('List of Agreement requires Case : Total : ' + agreementList.size() + ' : ' + agreementList ); //call future method if ( agreementList != null && agreementList.size() > 0 ) { System.Debug('Calling future case method'); AgreementCustomerHelper_Cardinal.createCase(trigger.new); System.Debug('Completed future case method'); createCase = false; } } } } System.Debug('APTS_AgreementTrigger: Exiting'); //------------------------------------------------------------------------------- }

Trigger 2:

trigger APTS_AgreementAccountShare on Apttus__APTS_Agreement__c (after Update, after insert) { if( Trigger.isAfter) { Map<String,Apttus__APTS_Agreement__c> agreementIDMap1 = new Map<String,Apttus__APTS_Agreement__c>(); set<String> accountid = new set<String>(); //Account Team Member and AgreementSharing Memember for(Apttus__APTS_Agreement__c eachAgreement: Trigger.new ) { if(eachAgreement.Apttus__Account__c != null) { if(Trigger.isUpdate) { if(eachAgreement.Apttus__Account__c != Trigger.oldMap.get(eachAgreement.Id).Apttus__Account__c) { agreementIDMap1.put(eachAgreement.Id, eachAgreement); accountid.add(eachAgreement.Apttus__Account__c); } }else if ( Trigger.isInsert) { agreementIDMap1.put(eachAgreement.Id, eachAgreement); accountid.add(eachAgreement.Apttus__Account__c); } } } if(accountid != null ) { List <Apttus__APTS_Agreement__Share> agrmShareDelete = [SELECT ID, UserOrGroupId,ParentId FROM Apttus__APTS_Agreement__Share WHERE ParentId IN : agreementIDMap1.keySet() AND rowCause =: Schema.Apttus__APTS_Agreement__Share.RowCause.AgsSharing__c ]; if(agrmShareDelete != null ) { APTS_AgreementAccountShareClass.deletingAgreementShare(agrmShareDelete); } List<AccountTeamMember> listaccTm = [SELECT ID, UserId , AccountId FROM AccountTeamMember WHERE AccountId IN: accountid ]; if( listaccTm != null) { APTS_AgreementAccountShareClass.agreementSharingAddition(agreementIDMap1.values(),listaccTm); } } } }
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Santhiya,

As of now, we cannot directly merge the two triggers directly but you would have to rewrite both the triggers so that you can make sure the whole logic is present in a single trigger rather than two triggers.

Also, as we know that having only one trigger per object is considered as a best practice so while doing this you would, in turn, be following one of the many best practices.

I hope this helps and in case if you found this handy can you please choose this as the best answer so that it can be used by others in the future.

Just for reference, I found this developer forum thread has the best practices for a trigger, please have a look at the thread: [https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG]

Regards,
Anutej