You need to sign in to do that
Don't have an account?

Populate a lookup field using the current user, when an opportunity is closed
Hello,
new to Triggers, we want to populate a custom lookup field with the current user when an opportunity is closed.
For example, we have a custom field called closed_by__c which is a lookup to the user object. When a user selects "Closed" from the standard stagename field we want to have the users name populated in the closed_by__c
I've spent a ton of time looking through the forums but I haven't come across somebody with the same situation.
any thoughts on how to do this ?
here is what I started with
new to Triggers, we want to populate a custom lookup field with the current user when an opportunity is closed.
For example, we have a custom field called closed_by__c which is a lookup to the user object. When a user selects "Closed" from the standard stagename field we want to have the users name populated in the closed_by__c
I've spent a ton of time looking through the forums but I haven't come across somebody with the same situation.
any thoughts on how to do this ?
here is what I started with
trigger updateClosedByField on Opportunity (after insert, after update){ for(opportunity opp : trigger.new) { opp.closed_by__c = opp.StageName; } }
All Answers
Here are some programmatic notes that should help:
- opp.IsClosed is a boolean flag that should let you know if the updated|inserted Opportunity is Closed.
- opp.LastModifiedById should reference the User who updated record. In my testing, newly created Opportunity shows modification as well as creation info; however, you always can use CreatedById when the Trigger.isInsert (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm) applies.
- In some other tests, I found that I could update previously closed records. In those instances, the trigger still fires and your condition of opp.IsClosed would be met. Therefore, you may want to look into Trigger.isUpdate as well. When the trigger is an update, you will have both a Trigger.old and a Trigger.new with values as the names suggest. You would want to ensure your old value was not also IsClosed (i.e., oldOpp.IsClosed != newOpp.IsClosed).
I hope that makes sense.As a side note, if you think the logic for handling the custom fields may get more complete, you can create a separate helper class to handle the actual business logic.
Respectfully yours, Kevin
Error:Apex trigger OpportunityClosedByField caused an unexpected exception, contact your administrator: OpportunityClosedByField: execution of AfterUpdate caused by: System.FinalException: Record is read-only: ()
Let me know if that helps!
Happy coding!
Kevin
In any case, you could set opp.StageName = 'your_new_stage_here'. Just note that only certain stages set the IsClosed flag; therefore, you may have to update the code a little in the rest of the trigger depending on when|why you change the status.
I have similar requirement. I need to populate current user info in custom field when I create a new record. User should be able to see his own name before he saves the record.
I wrote the trigger and it populates the current user info after I save the record.
I need to pre-populate the current user info when I create the new record.
Below is my trigger code.
trigger populate_current_user on Applications_Time_Tracking__c (before insert) {
// populate the current user name on record creation
for(Applications_Time_Tracking__c tt : Trigger.new){
if(tt.User__c == null){
tt.User__c = UserInfo.getUserId();
}
}
Can you please help.