trying to write a trigger involving two objects, standard Case and custom Milestone__c. When Case status is marked "sold" and case is saved, create a milestone object. My code is partially working but I am afraid I am accidently looping twice, as two Milestone__c records are being created as opposed to the desired one. Below is the code in my triggerhandler for this, any suggestions are appreciated I'm hitting a wall:

    public void createMilestone_After(map<Id,Case> oldMap, list<case> newList){
    list<Milestone__c> newMilestoneList = new list<Milestone__c>();

    for(Case currCase : newList){
        if(currCase.Status =='Sold' && currCase.Status != (oldMap.get(currCase.Id).Status)){

            Milestone__c newMstone = new Milestone__c();
            Employee__c em = [SELECT Id, OwnerId FROM Employee__c WHERE SF_User__c =:currCase.Sold_By__c];
            newMstone.Employee__c = em.Id;
            newMstone.Carrier_Milestone__c = currCase.Policy_Carrier__c;
        upsert newMilestoneList;
  • Do you have any workflow field updates on Case?
    – BarCotter
    Commented Sep 30, 2014 at 15:51
  • 1
    Good point @BarCotter I didnt think of that, we do, one that fires on the same criteria (case status being sold) that updates the case. Wouldn't the below line account for that though..specifically if the status is the same don't create other record?: if(currCase.Status =='Sold' && currCase.Status != (oldMap.get(currCase.Id).Status))`
    – GeorgeA
    Commented Sep 30, 2014 at 15:57
  • 1
    No, the old and new values are not updated when the trigger fires a second time. Have a look at the answer @sfdc_ninja provided here for more details.
    – BarCotter
    Commented Sep 30, 2014 at 16:06
  • Thanks @BarCotter that gave me the direction I needed. I am still in the midst of tinkering with my code but you've pointed me in the right direction.
    – GeorgeA
    Commented Sep 30, 2014 at 19:00

1 Answer 1


You should first check exactly which before/after insert/update cases this code is called from; you have hopefully done that already.

Then I suggest you check this Triggers and Order of Execution documentation that outlines the situations under which a trigger is fired more than once:

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 are not run again.

It might be necessary to add a static variable guard if that is what is happening to stop the trigger body doing anything the second time.

(As a separate matter, the Employee__c query would be better moved out of the loop to bulkify the code.)

You must log in to answer this question.

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