Skip to main content

I have a trigger on after insert and after update, I want to update a few custom fields on contact object but I am getting a DML exception.

 

Class 

public class UTMController {     @future(callout = true)     public static void UTManipulator(set<id> Contactid){                 System.debug(Contactid);         List<contact>Contactlist = [Select UTM_URL__c from Contact where Contact.id in:Contactid ];         system.debug('List'+Contactlist);         if(Contactlist.size()>0){         for(Contact con:Contactlist){             System.debug(con.UTM_URL__c);             if(con.UTM_URL__c!=null){                  con.UTM_Source__c = con.UTM_URL__c.substringBetween('utm_source=','&');                 con.UTM_Medium__c =  con.UTM_URL__c.substringBetween('utm_medium=','&');                 con.UTM_Campaign__c = con.UTM_URL__c.substringAfter('utm_campaign=');                 system.debug(con.UTM_Source__c);                 system.debug(con.UTM_Medium__c);                 system.debug(con.UTM_Campaign__c);                                               }           }             UPDATE Contactlist;                                  }                                }           }

 

Trigger 

trigger ContactUTM on Contact (After insert,after Update) {             if(trigger.isAfter&& (trigger.IsInsert || trigger.Isupdate)) {                              for(Contact CustomLead : Trigger.New){                      set<id> Contactid = new set<id>();                     if(CustomLead.UTM_URL__c!=null){                        Contactid.add(CustomLead.Id);                                                  UTMController.UTManipulator(Contactid);                         } }             }         }  ERROR

EXCEPTION_THROWN [22]|System.DmlException: Update failed. First exception on row 0 with id 0035j00000SQENjAAP; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactUTM: execution of AfterUpdate 

1 resposta
  1. 20 de jul. de 2022, 17:32

    Hello Shivam, it seems like your trigger is firing recursively. Please add static Boolean flag in your controller class and manage to fire the trigger only once during whole transaction.