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
AneeshaAneesha 

Is there way to update a field through after trigger

I know normally it is not possible but I need to run an after trigger and then update a field. Is there a way to do it?

Best Answer chosen by Admin (Salesforce Developers) 
Jeff MayJeff May

In an after trigger, you have to create a new record since the record you are working with is 'read only'.

 

You can do this in your trigger as follows (snippet only to give you the idea -- not bulkified!)

 

 

for (Lead l : trigger.new) {

   Lead lnew = new Lead(Id = l.Id, OwnerId = QueueYouWant);

   update lnew;

 

}

All Answers

Parth_SevakParth_Sevak
After trigger execute, only after records are committed successfully to the database. then you can get record Id, and with the help of Id, you can update related object fields.
if you want to update same object field, why don't you use before trigger?
can you please elaborate requirement ?
AneeshaAneesha

Okay, I'll explain my requirement here.

I have a lead record which I send it across to another org through s2s. I am using the out-of-box s2s functionality and std 'Forward to connection' button for this. So after the record is end send across,I need to change the record owner to a queue.

 

I have written an after update trigger for the same. However I am unable to update the owner field

Parth_SevakParth_Sevak
I am not sure,but if it's possible, do this way, whenever 'Forward to connection', button is clicked, execute apex code to change lead record owner to queue, before sending leads to another org. or if you can figure out order of execution using debug log, then it will be easy to implement this.
Jeff MayJeff May

In an after trigger, you have to create a new record since the record you are working with is 'read only'.

 

You can do this in your trigger as follows (snippet only to give you the idea -- not bulkified!)

 

 

for (Lead l : trigger.new) {

   Lead lnew = new Lead(Id = l.Id, OwnerId = QueueYouWant);

   update lnew;

 

}

This was selected as the best answer
AneeshaAneesha

@parth

 

Can i do that without overriding the 'Forward to connection' button?

AneeshaAneesha

@JeffM

 

Can't do that. Its making my trigger run recursively.

Parth_SevakParth_Sevak
if you can access and modify action method of button, then no need to override,
Jeff MayJeff May

You can always control the recursion.  Here's a blurb on how that works:   http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

AneeshaAneesha

I am sorry for the troublw but I am new to salesforce. Where should I look for the action method. I found that the page is 'ForwardEntityPage'. However cannot find this page in the org.

AneeshaAneesha
Thanks JeffM .. this partially solved my problem.