+ Start a Discussion
sumanthylaksumanthylak 

how to update the records using trigger.

how to update the records using trigger. i write the trigger for inserted the record  into Email__c Field in Student__c custom object. and i need  edit the existing record then click on save button . Email__c field is updated . i try for update the record but  update is not working. this is code for insert the record into Email__c

 

Trigger:

 

        trigger suman on Student__c (after insert,after update) {

if(Trigger.isInsert){
 for(Student__c stud:Trigger.new){
  Student__c st = [Select id,Email__c from Student__c where id=:stud.id];
  st.Email__c = 'suman@thylaksoft.com';
  update st;
  }
 }
 
 }

but i need how to write code for update the record. please any one help me.........

kiranmutturukiranmutturu

u r missing the basic points in the apex developement.. like putting a DML in for loop. and query inside the loop...

u r working on student__c trigger.. and want to updated the same with some values.. hope you are practising apex....

http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf.. go through this....

Chris JohnChris John

It looks like you're attempting the update when the trigger gets executed from record insertion.

 

If I understand your requirements, I think you'll want to check when Trigger.isUpdate, and perform your update logic in there.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

 

Hope that helps,

 

-cj