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

Update a date field when opportunity was closed using trigger
I am trying to update a date/time field (Actual_Closed_Date__c) with the current date when an opportunity was closed (Stage__c = Closed Won || Stage__c = Closed Lost) using a trigger, any suggestions?
Please try below code: If solve your problem, mark as solved.
Thanks
It can be done via process builder in which in the criteria you can add (ISCHANGED(Stage__c) && Stage__c == Closed Won || Stage__c = Closed Lost) and update the opportunity records.
If you want to write a trigger then you can create a trigger on Opportunity with 'before update' event.
Hope it helps, if it does, mark it as solved.
Thanks
Thank you so much for your help. I tried your code, looks very straight forward though I encoutnered an error saying "execution of AfterUpdate caused by: System.FinalException: Record is read-only". Should I add "before insert"?
Also, can this be modified so that it only happens if the StageName is originally not closed, meaning it only fires one time (only when it was closed). I can see that this may also be fired when update an already closed opportunity.
It was originally done using Process Builder, though now we need the value of the actual closed date before any opportunity trigger occurs and triggers run before processes/wokflow rules.
Please have a look on below code.
If solve your requirement. Please mark as solved.
Thanks
Does removing after insert or after update affect any of the classes that were being called in the trigger? Not quite familiar with triggers & classes yet but I think they were put there on purpose by the other developer.
In that case, please use below code where the additional functionality is working only when the trigger is running before any update( or insert).
If found satisfying, then mark as solved.
Thanks
can you please share the proper code. so I can help you that removing After insert and After update does affect any of the classes that were being called in the trigger.
Thanks & Regards
Akash Pandey
Piyush replied with a good response and here is the additional answer to your question:-
Does removing after insert or after update affect any of the classes that were being called in the trigger? :- No, it won't when you add the condition like if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) {// your code}.The previous developer added it but there is no need of after event in your case.
Before Trigger -> When DML is performed on the same object
After Trigger -> When DML is performed on other/related object.
Thanks
To your reply:-
It was originally done using Process Builder, though now we need the value of the actual closed date before any opportunity trigger occurs and triggers run before processes/wokflow rules.
You are right that it runs before processes/workflow rules but it does the same thing which you are doing via code and salesforce best practice follows OOTB functionality where you don't have to write code.Just my opinion.
Thanks
The actual reason I want the field to be updated on the trigger is because the class that is being called uses the value of that field on creating/updating a record for another related object.
I added another condition to still be able call the classes that perform task for another object. Did I miss anything here or is there a better (more standard) approach here or I am good to go with it? Should the condition be elseif?
Try this code:
Trigger:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Use this code:
Thanks
To give you the process here, when a capital(type) opportunity was closed won, a target vs actual(custom object) record will be created/updated with the opportunity amount. All of those function were working. We are revising the code so that instead of closedate field we use actual closed date field to create/update the correct target vs actual record. The error I think is saying that instead of calling the class to update the existing target it is creating a new one which causes a duplcate?
The target vs Actual Custom Object has lookup/MD relationship to Opportunity, I think.
Let me know if it works.
Thanks
Thanks for your time, we already have a class for OpptyTriggerHandler2 so I think we don't need to revise that, I think the problem is coming from just the trigger itself calling the classes in either before of after. To make the picture clearer I have here the trigger and classes that were being used.
OpptyTrigger2 --> OpptyTriggerHandler --> TargetVsActualManager