1

The trigger creates a related record when a checkbox is updated. The problem is it is creating a new related record everytime the record is edited. I want to edit the trigger to only create the related record once. (When the criteria is first met). The code is below

Trigger copyEquipmentonsite on Unit_Placements_Sales__c(after insert, after update) {
     List<Services__c> sub=new List<Services__c>();
     for(Unit_Placements_Sales__c u : Trigger.new) {
          if(u.Service_Month_is_Next_Month__c == TRUE) {
                Services__c s = new Services__c();
                s.Equipment_Onsite__c=u.ID;
                s.Equipment_Type__c=u.Equipment__c;
                s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c;   
                s.Product__c=u.The_Product__c;  
                s.Dell_Tag_Number__c=u.Dell_Tag_Number__c;
                s.Serial_Number__c=u.Serial_Number__c; 
                s.XD_Number__c=u.XD_Number__c;
                sub.add(s);
          }
          if(sub.size()>0)
          insert sub;
     }
}
5

My guess is that you have a work flow rule that has a field update on the Unit_Placements_Sales__c object that is causing the trigger to fire a second time. A common way to prevent this problem is to use a Static variable. You need to use class to do this. This is an example of of how a static variable with prevent the recursion.

Utility Class

public class TriggerContextUtility {

    private static boolean firstRun = true;

    public static boolean isFirstRun() {
        return firstRun;
    }

    public static void setFirstRunFalse(){
        firstRun = false;
    }
}

Trigger

Trigger copyEquipmentonsite on Unit_Placements_Sales__c(after insert, after update) {
     if(TriggerContextUtility.isFirstRun()){

         TriggerContextUtility.setFirstRunFalse();

         List<Services__c> sub=new List<Services__c>();
         for(Unit_Placements_Sales__c u : Trigger.new) {
               if(u.Service_Month_is_Next_Month__c && !oldMap.get(u.Id).Service_Month_is_Next_Month__c) {
                   Services__c s=new Services__c();

                   s.Equipment_Onsite__c=u.ID;
                   s.Equipment_Type__c=u.Equipment__c;
                   s.Machine_Specific_Notes__c=u.Machine_Specific_Notes__c;   
                   s.Product__c=u.The_Product__c;  
                   s.Dell_Tag_Number__c=u.Dell_Tag_Number__c;
                   s.Serial_Number__c=u.Serial_Number__c; 
                   s.XD_Number__c=u.XD_Number__c;
                   sub.add(s);
                }
                if(sub.size()>0)
                insert sub;
         }
     }
}
  • Hi Sfdc Ninja, I have tried your method but the records are still being created everytime I edit the record. Do I need to create a checkbox field or something? What am I missing? – GMan Sep 8 '14 at 12:52
  • You need to add a check that the checkbox was just checked. You do this by comparing the value in trigger.old with trigger.new. I have updated my answer to reflect. – Chris Duncombe Sep 8 '14 at 13:14
1

In addition to checking whether Service_Month_is_Next_Month__c is set, you want to check whether it wasn't previously set. There are two cases to cover: 1) the field is set during an insert and 2) it is set during an update and wasn't previously set:

trigger copyEquipmentonsite on Unit_Placements_Sales__c(after insert, after update) {
   List<Services__c> sub = new List<Services__c>();
   for (Unit_Placements_Sales__c u : Trigger.new) {
   if (u.Service_Month_is_Next_Month__c && (Trigger.isInsert || !Trigger.oldMap.get(u.Id).Service_Month_is_Next_Month__c)) {
      ...
0

Create another checkbox and check if it is false, and if it is, create the related record. Then you update the value to true, so next time you edit the record it won't trigger the creating of related record.

           if(u.Service_Month_is_Next_Month__c == TRUE && u.New_checkbox__c==false){
               // ...
               u.New_checkbox__c=true;
           }
  • Hi Ziga Just so I am understanding this correctly- Is the idea to add a line in the trigger that sets this new checkbox to true after the trigger has run and then include this in the trigger criteria. For example If (u.Service_Month_is_Next_Month__c == true && u.new_checkbox_field__c ==true) – GMan Sep 8 '14 at 12:01
  • Yes, like that, except it has to be false. I've just edited my answer. – ziga Sep 8 '14 at 12:02
  • This is not an ideal approach as you would need an extra field for every field in which you want to test in a trigger. Right now, you are only looking for one field, Service_Month_is_Next_Month__c , so you only need to add one field. If your logic changes and you need to add additional field, you will need to add additional 'extra' fields. – Chris Duncombe Sep 8 '14 at 12:08
  • Hi Ziga, i have tried your Method also but am now getting an error message when I add this line "u.Trigger_has_run__c=True". The error says the record is read only? – GMan Sep 8 '14 at 12:54
  • Trigger should be before insert and before update. – ziga Sep 8 '14 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.