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 NarayansudhIr Narayan 

Trigger is not updating Lead Status

Hi, 

  I wrote below trigger to update lead status. when appoinment is created. But I am getting below error 

  trigger Update_Appointment_SQL on Lead (After Insert, After Update) 
{
set<id> leadId=new set<id>();
for(Lead ts : trigger.new)
{
leadId.add(ts.id);
Lead ld=[Select status,Appointment_Created_Date__c from Lead where id in :leadId];
if( ld.Appointment_Created_Date__c <> null)
{
ld.Status='SQL';
update ld;
}
}
}
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Update_Appointment_SQL caused an unexpected exception, contact your administrator: Update_Appointment_SQL: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Q6000000myNGaEAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Update_Appointment_SQL: maximum trigger depth exceeded Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa] Lead trigger event AfterUpdate for [00Q6000000myNGa]: []: Trigger.Update_Appointment_SQL: line 11, column 1
 Please suggest me 


Thanks
Sudhir

Best Answer chosen by sudhIr Narayan
Arunkumar RArunkumar R
Hi the below code is working,

trigger UpdateLeadStatus on Lead (before insert,before update) 
{

    for(Lead ld : Trigger.new)
    {
        if(ld.Appointment_Created_Date__c <> null)
        {
            ld.Status='SQL';
        }
    }
    
    
}

Hope this will helpful to you...!

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
You are updating the same record in this Trigger which fire the Trigger again and again on the same record to avoid this situation write the trigger for Before Insert and for Before update and change the value of your status field.

Use below code instead of your code hope it will help you
trigger Update_Appointment_SQL on Lead (Before Insert, Before Update) {
set<id> leadId=new set<id>();
for(Lead ts : trigger.new) {
leadId.add(ts.id);
Lead ld=[Select status,Appointment_Created_Date__c from Lead where id in :leadId];
if( ld.Appointment_Created_Date__c <> null) {
ld.Status='SQL';
}
}
}

Please mark my answer as a best solution to your question if it solves your problem.
Arunkumar RArunkumar R
Hi the below code is working,

trigger UpdateLeadStatus on Lead (before insert,before update) 
{

    for(Lead ld : Trigger.new)
    {
        if(ld.Appointment_Created_Date__c <> null)
        {
            ld.Status='SQL';
        }
    }
    
    
}

Hope this will helpful to you...!
This was selected as the best answer
Ravi NarayananRavi Narayanan

You cannot assign values /modify the trigger.new records in after trigger.