5

My Trigger is firing twice due to the work flow after update on the account object

Here is my code

Trigger

trigger updateCreditRating on Account(after  Update) {
AccountTriggerHandler handler = new AccountTriggerHandler();
if(trigger.Isupdate && AccountTriggerHandler.firstRun){
    handler.processRecords(trigger.new, trigger.newMap, trigger.oldMap);
    AccountTriggerHandler.firstRun = false;
}
} 

Apex Class

public class AccountTriggerHandler {

public static  boolean firstRun = true;

public void processRecords(List<Account> newList, Map<Id,Account> newMap, Map<Id, Account> oldMap){  

    trailerwizardsComWebservice.ArrayOfSIV_CREDIT_RATING newArray = new trailerwizardsComWebservice.ArrayOfSIV_CREDIT_RATING();
    List<trailerwizardsComWebservice.SIV_CREDIT_RATING> lstSivCreditRating = new List<trailerwizardsComWebservice.SIV_CREDIT_RATING>();
    for(Account myAccount : newList){   

        trailerwizardsComWebservice.SIV_CREDIT_RATING objSivCreditRating = new trailerwizardsComWebservice.SIV_CREDIT_RATING();
        if(myaccount.Credit_Rating__c != '' && oldMap.get(myAccount.id).Credit_Rating__c != myaccount.Credit_Rating__c){

            objSivCreditRating.CUST_CODE = myAccount.Account_Number__c;
            objSivCreditRating.CUST_CREDIT_RATE = myAccount.Credit_Rating__c;
            objSivCreditRating.CUST_DATE_MODIFY = myAccount.LastModifiedDate.format('yyyy-MM-dd');
            objSivCreditRating.CUST_SETCREDIT_BY = myAccount.Credit_Rating_Set_By__c;
            objSivCreditRating.CUST_SETCREDIT_DATE = myAccount.CreatedDate.format('yyyy-MM-dd');
            objSivCreditRating.CUST_SETCREDIT_USER = myAccount.Owner.Name;
            objSivCreditRating.RecordId = '';

        }//End If
        lstSivCreditRating.add(objSivCreditRating);
    }//End For*/
    newArray.SIV_CREDIT_RATING = lstSivCreditRating;
   //postToSFDCCallout(newArray);
    String s = json.serialize(newArray);
    futureCall(s);

}

@future(callout = true)
public static void futureCall(String str){
    trailerwizardsComWebservice.BasicHttpBinding_ISeriesIVServices s = new trailerwizardsComWebservice.BasicHttpBinding_ISeriesIVServices();
    trailerwizardsComWebservice.ArrayOfSIV_CREDIT_RATING newArray = (trailerwizardsComWebservice.ArrayOfSIV_CREDIT_RATING) System.JSON.deserialize(str, trailerwizardsComWebservice.ArrayOfSIV_CREDIT_RATING.class); 
        s.UpdateCreditRating(newArray);


}

}
1
  • Its normal that because of workflow rule trigger would get fired twice. But whenever you write code in trigger you should always check for change in before value and new value so that even if trigger fires twice sytem will run your code only once. Currently because of trigger in which area of code you are facing sideeffects? Dec 8, 2015 at 19:00

2 Answers 2

5

Just simple mistake

if(trigger.Isupdate && AccountTriggerHandler.firstRun){
    AccountTriggerHandler.firstRun = false; 
    handler.processRecords(trigger.new, trigger.newMap, trigger.oldMap);

}

First set the static variable after that call handler method.

if you first call the handler. Workflow will fire from your handler DML.

So it will fire again trigger but still we are in first time handler.

that's the reason you need to update static variable first.

10
  • Hi Rathan ....Trigger is still firing on insert of the Account record after u r suggested change in the code too
    – Donald
    Dec 8, 2015 at 16:10
  • @rathan it is called twice i am checking in the Apex jobs to conform for sure it is firing twice.
    – Donald
    Dec 8, 2015 at 16:23
  • @rathan...The issue is the trigger is firing on the insert of the record.But it should fire after the update .....right ....Is something i am missing here
    – Donald
    Dec 8, 2015 at 16:37
  • @rathan---->The trigger is firing once only but the thing is it is firing on insert instead of firing on after update.
    – Donald
    Dec 8, 2015 at 16:43
  • @rathan --->NO Trigers mine is the only one trigger .But i have work flow rules that updates the account after the insertion
    – Donald
    Dec 8, 2015 at 17:04
3

Yes, It is certain that Triggers would fire again (only again) due to workflow field update association. Please refer 12th Point from Salesforce documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm)

12th Point: If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules, duplicate rules, and escalation rules are not run again.

Please refer : https://help.salesforce.com/articleView?id=000005328&type=1 knwoledge article for simple solution to overcome this by introducing a Boolean flag.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .