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
Karthi_VeluKarthi_Velu 

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.

 

ex,

 

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;

 

<end> 

 

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! 

Karthi_VeluKarthi_Velu

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

 

FYI 

Br1Br1

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 .

 

Example:

 

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

 

MyLead.Name = 'Modified Norman';

 

update MyLead;

 

 

Bruno. 

 

 

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!

Regards,
Suraj Tripathi