function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RstrunkRstrunk 

Update trigger AND workflow rules

Good morning everyone!

 

   So I encountered an issues and it seems that it would be very common, so I thought you guys might be able to help me out.  

 

I created a trigger that runs on UPDATE, however, anytime a record is inserted the update trigger fires.  I believe I know why it happens but I need to prevent it.  I believe that when a record is inserted, workflow rules with field updates cause the record to be updated thus fireing the UPDATE trigger. 

 

So my question is, what can I do to my trigger to prevent the workflow rule from making my UPDATE trigger fire on INSERT?

 

I give kudos and follow up on all posts so if you can help I will return the favor.

 

Thanks, 

Best Answer chosen by Admin (Salesforce Developers) 
RstrunkRstrunk

Ok so here is an update.  I made a template that requires a workflow rule that will always fire on a case.  I created two custom fields that are usd as toggle switches.  The workflow rule will always update one of the fields to be true.  The other field will be turned on/off depending on whether it is an insert or update. At the end of the trigger both fields are reset.  I'm sure there are better ways of doing it but I am not going to debate that, this is a specific problem I had and a specific solution was necesary.  The @future annotation didn't meet my needs.  I am a newbie to programming on the force platform so please don't flame me lol.   Here is the template I created:

 

trigger <TRIGGER NAME> on <OBJECT> (before insert, before update){

    //Original Code Author: Robert Strunk
    //                      robert.strunk@coastalcloud.us

    // Scenario for using this trigger:	If you have logic that needs to happen once and ONLY once when an <OBJECT> record is 
    //                                  inserted(before) or updated(before) AND you have multiple assignment rules and/or workflow
    //                                  rules(with field updates) on the same object.
    //                                  Because of the order of execution, any logic done on insert or update will fire twice if 
	//                                  there is
    //                                  an assignment rule or workflow rule(with field update) on the same object.

    //DEPENDENCIES: 1.  Create two custom fields on the <OBJECT> object.  One named field_1__c and one named field_2__c 
    //                  both fields should be a checkbox type with a default value of false.
    //              2.  Create a workflow rule that will fire a field update every time an <OBJECT> record is inserted or updated.
    //                  The field that will always be updated will be field_2__c
  
    if(trigger.isInsert && trigger.isbefore){

        //BEFORE workflow rules, so no alerting should be in this block
        for(Integer i = 0; i < trigger.new.size(); i++){

            //Sets this field to true so the actual insert logic can happen in the second iteration of before update
            trigger.new[i].field_1__c = true; 
            continue;
        }
    }

    if(trigger.isUpdate && trigger.isbefore){
        for(Integer i = 0; i < trigger.new.size(); i++){

            //BEFORE workflow rules, so no logic should be in this block
            if(trigger.new[i].field_1__c == false && trigger.new[i].field_2__c == false){
                //Since this happens before workflow rules, nothing is needed.
                continue;
            }

            //AFTER workflow rules
            //For logic that needs to happen when an <OBJECT> record is UPDATED.
            else if(trigger.new[i].field_1__c == false && trigger.new[i].field_2__c == true){

                /*

                            ADD YOUR LOGIC FOR UPDATE

                */

                //Resets field_2__c to false
                trigger.new[i].field_2__c = false;
                continue;
            }

            //AFTER workflow rules
            //FOR logic that needs to happen when an <OBJECT> record is inserted.
            else if(trigger.new[i].field_1__c == true && trigger.new[i].field_2__c == true){

                /*

                            ADD YOUR LOGIC FOR INSERT

                */

                //Resets custom fields for next time this record is updated. 
                trigger.new[i].field_1__c = false;
                trigger.new[i].field_2__c = false;
                continue;
            }
        }
    }
}

 

 

All Answers

vbsvbs
Rstrunk - You will have to identify some additional conditions for the update trigger that will ensure that it executes only on specific cases. One way would be to identify what the workflow field update does and ensure that trigger will not execute if the field value changes. The alternative maybe to revisit the business cases for both the WF and trigger and see if they can be merged.
Samba GSamba G

1. adds some filter condition in workflow rule.

2. creates a field in the object.

 

 

Thanks,

Samba

RstrunkRstrunk

Ok so here is an update.  I made a template that requires a workflow rule that will always fire on a case.  I created two custom fields that are usd as toggle switches.  The workflow rule will always update one of the fields to be true.  The other field will be turned on/off depending on whether it is an insert or update. At the end of the trigger both fields are reset.  I'm sure there are better ways of doing it but I am not going to debate that, this is a specific problem I had and a specific solution was necesary.  The @future annotation didn't meet my needs.  I am a newbie to programming on the force platform so please don't flame me lol.   Here is the template I created:

 

trigger <TRIGGER NAME> on <OBJECT> (before insert, before update){

    //Original Code Author: Robert Strunk
    //                      robert.strunk@coastalcloud.us

    // Scenario for using this trigger:	If you have logic that needs to happen once and ONLY once when an <OBJECT> record is 
    //                                  inserted(before) or updated(before) AND you have multiple assignment rules and/or workflow
    //                                  rules(with field updates) on the same object.
    //                                  Because of the order of execution, any logic done on insert or update will fire twice if 
	//                                  there is
    //                                  an assignment rule or workflow rule(with field update) on the same object.

    //DEPENDENCIES: 1.  Create two custom fields on the <OBJECT> object.  One named field_1__c and one named field_2__c 
    //                  both fields should be a checkbox type with a default value of false.
    //              2.  Create a workflow rule that will fire a field update every time an <OBJECT> record is inserted or updated.
    //                  The field that will always be updated will be field_2__c
  
    if(trigger.isInsert && trigger.isbefore){

        //BEFORE workflow rules, so no alerting should be in this block
        for(Integer i = 0; i < trigger.new.size(); i++){

            //Sets this field to true so the actual insert logic can happen in the second iteration of before update
            trigger.new[i].field_1__c = true; 
            continue;
        }
    }

    if(trigger.isUpdate && trigger.isbefore){
        for(Integer i = 0; i < trigger.new.size(); i++){

            //BEFORE workflow rules, so no logic should be in this block
            if(trigger.new[i].field_1__c == false && trigger.new[i].field_2__c == false){
                //Since this happens before workflow rules, nothing is needed.
                continue;
            }

            //AFTER workflow rules
            //For logic that needs to happen when an <OBJECT> record is UPDATED.
            else if(trigger.new[i].field_1__c == false && trigger.new[i].field_2__c == true){

                /*

                            ADD YOUR LOGIC FOR UPDATE

                */

                //Resets field_2__c to false
                trigger.new[i].field_2__c = false;
                continue;
            }

            //AFTER workflow rules
            //FOR logic that needs to happen when an <OBJECT> record is inserted.
            else if(trigger.new[i].field_1__c == true && trigger.new[i].field_2__c == true){

                /*

                            ADD YOUR LOGIC FOR INSERT

                */

                //Resets custom fields for next time this record is updated. 
                trigger.new[i].field_1__c = false;
                trigger.new[i].field_2__c = false;
                continue;
            }
        }
    }
}

 

 

This was selected as the best answer