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

Update record in Apex or Trigger.

Hi All,


Can any help me How to update a Field in paritcular Table(object), i tried to update a Lead record, but i coud't set the id value.




Lead leadOldObject = ref for exsiting; //existing lead object ....


Lead l = new Lead();

l.LastName =  l.LastName +'Its me'; = new ID(; // if i commited this line getting error as no id was assigned for update objects

update l;


<end> = new ID(;

if i commited the above line getting error as no id was assigned for update throw 


not commented means i compiler error on this line..


Thnx in advance! 


l.LastName =  leadOldObject.LastName +'Its me'; // this is correct line.... 




The id field cannot be written, you must query (if you don't have the object already loaded) the object first, modify it, then update .




Lead MyLead = [Select Id, Name from Lead where Name = 'Norman Bates'];


MyLead.Name = 'Modified Norman';


update MyLead;






Suraj Tripathi 47Suraj Tripathi 47
Hi karthi,

Fetch the record from the object using query then update the field from that object.
Lead leadObj= [Select Id, Name from Lead LIMIT 1];
leadObj.Name='Lead Object';
update leadObj;

If you find your Solution then mark this as the best answer.

Thank you!

Suraj Tripathi