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
F.H.KazmiF.H.Kazmi 

Unable to update lead using trigger

trigger LeadTrigger on Lead (after insert) {
    //works dsdcc
    for (Lead lea : Trigger.New)
    {
        lea.City = 'New York';
        update lea;
    }
   
}

 

/*error when create lead error is:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger LeadTrigger caused an unexpected exception, contact your administrator: LeadTrigger: execution of AfterInsert caused by: System.Exception: Record is read-only: */

 

aalbertaalbert

Yes, that trigger is an "after" trigger (after insert to be exact) and an "after" trigger can't update the Lead records that initiated the request. If you change the trigger to a "before" trigger, this code should work.

 

You also won't need the explicit update statement in a "before" trigger. For example:

 

trigger LeadTrigger on Lead (before insert) { for (Lead lea : Trigger.New) { lea.City = 'New York'; } }