+ 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';

l.id = new ID(leadOldObject.id); // if i commited this line getting error as no id was assigned for update objects

update l;




l.id = new ID(leadOldObject.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