+ 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.