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
Sudhir_MeruSudhir_Meru 

Trigger Conflict

Hi, 

  I have put a trigger on appoinment object which will fire to update lead status Please see below code 
trigger Update_Lead_Status on Appointment_Detail__c (After Insert, Before Update) 
{
 
 Appointment_Detail__c ApptDt = [ SELECT  Completed__c,Lead_Id__c  FROM Appointment_Detail__c where id in :Trigger.newMap.keySet()];  
     
    If ( ApptDt.Lead_Id__c <> null && ApptDt.Completed__c == 'Yes' ) 
    {
      Lead ld = [select staTus from Lead where id = :ApptDt.Lead_Id__c]; 
        if ( ld.status <> 'SQL') 
        {
         ld.status = 'SQL';
         update ld;      
         } 
      
    }  
  }
There is another trigger on Lead which will again update appoinment object please see code

//Created by Unnat Shrestha on March 30, 2014
//The functionality of this trigger is to reparent the child object, "Appointment Details" of Lead to Opportunity during the Lead Conversion

trigger reparent_Lead_children on Lead (after update) {
    //Creating a map between Lead and Opportunity
    Map<Id, Id> lead2Opp= new Map<Id, Id>();
    //Map<Id, Id> lead2Acc = new Map<Id, Id>();
    Map<Id, Id> lead2Cont = new Map<Id, Id>();
    
    for (Lead l : Trigger.new)
    {
        System.debug('Entered the for loop');
        //if (l.isConverted && l.convertedOpportunityId != null)
        if (l.isConverted) {
            if (l.convertedOpportunityId != null) {       
                //adding the values into the Map
                lead2Opp.put(l.Id, l.convertedOpportunityId);
            }
            //lead2Acc.put(l.Id, l.convertedAccountId);
            lead2Cont.put(l.Id, l.convertedContactId);
        }
    }
    System.debug('The Map values are:' + lead2Opp);
    List<Appointment_Detail__c> apptDetails = [select account__c, contact__c, lead_Id__c, opportunity_Id__c from Appointment_Detail__c where lead_Id__c in :lead2Opp.keySet()];
    for (Appointment_Detail__c appointment : apptDetails)
    {
        //assigning the Opportunity Id based on this lead id
        appointment.opportunity_Id__c = lead2Opp.get(appointment.lead_Id__c);
        //appointment.account__c = lead2Acc.get(appointment.lead_Id__c);
        appointment.contact__c = lead2Cont.get(appointment.lead_Id__c);
        System.debug('For Appointments, Changed the grabbed the opportunity id');
    }
    
    System.debug('before update');
    //Update the list if there is any
    if (apptDetails.size() > 0) {
        update apptDetails;
    }
    
    //re-parent the E-Rate to Opportunity as well
    List<E_Rate__c> eRates= [select lead__c, contact__c, opportunity__c from E_Rate__c where lead__c in :lead2Opp.keySet()];
    for (E_Rate__c eRate: eRates)
    {
        //assigning the Opportunity Id based on this lead id
        eRate.opportunity__c = lead2Opp.get(eRate.lead__c);
        eRate.contact__c = lead2Cont.get(eRate.lead__c);
        
        System.debug('For E-Rate, Changed the grabbed the opportunity id');
    }
    
    System.debug('before update- 2nd time');
    //Update the list if there is any
    if (eRates.size() > 0) {
        update eRates;
    }

}

Now when I convert lead to opportunity I am gettin below error

Error: System.DmlException: Update failed. First exception on row 0 with id 00Q6000000myNGaEAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, reparent_Lead_children: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a1gm000000001G9AAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Update_Lead_Status: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Q6000000myNGaEAM; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: [] Trigger.Update_Lead_Status: line 12, column 1: [] Trigger.reparent_Lead_children: line 37, column 1: [] (System Code)

Please suggest me how to fix this issue

Thanks
Sudhir
Best Answer chosen by Sudhir_Meru
Navin MuneesamyNavin Muneesamy
Hey Sudhir
  • Firstly there is no need for the query at line 4 on trigger Update_Lead_Status. Your data is already available in trigger.new or trigger.newMap.
  • Secondly, you need to bulkify the trigger Update_Lead_Status.
  • Thirdly you have a Cyclical trigger call i.e trigger Update_Lead_Status will kick off reparent_Lead_children.

To avoid Cyclical triggers, you need to create a static variable in a class to track if the trigger was called already.

See the following for examples solutions:

http://stackoverflow.com/questions/11586739/salesforce-trigger-infinite-loop

http://salesforce.stackexchange.com/questions/10307/cyclic-trigger-calls?atw=1

Cheers

All Answers

Navin MuneesamyNavin Muneesamy
Hey Sudhir
  • Firstly there is no need for the query at line 4 on trigger Update_Lead_Status. Your data is already available in trigger.new or trigger.newMap.
  • Secondly, you need to bulkify the trigger Update_Lead_Status.
  • Thirdly you have a Cyclical trigger call i.e trigger Update_Lead_Status will kick off reparent_Lead_children.

To avoid Cyclical triggers, you need to create a static variable in a class to track if the trigger was called already.

See the following for examples solutions:

http://stackoverflow.com/questions/11586739/salesforce-trigger-infinite-loop

http://salesforce.stackexchange.com/questions/10307/cyclic-trigger-calls?atw=1

Cheers
This was selected as the best answer
Sudhir_MeruSudhir_Meru

Hi Navin, 

     Pleas give a example how to modify in my trigger 

Thanks

Sudhir