You need to sign in to do that
Don't have an account?
Jaynandan Prasad 8
trigger to copy the opportunity name in a custom text field in the case object
I need to write a before insert/update trigger to copy the opportunity name(opportunity__c), a lookup field in the case object to another custom text field(CopyOppty__c) in the same object(case). i wrote the below trigger, but not working.
trigger mapOppty on Case (before insert, before update) { Set<ID> oppIDs = Trigger.newMap.keySet(); Case [] cc = [SELECT Id, Opportunity__c, Opportunity__r.Name FROM Case WHERE Opportunity__c IN :oppIDs ] ; for(case ca : cc) { ca.copyOppty__c = ca.Opportunity__r.Name ; } update cc ; }
There are some errors in your code
- You cannto get Opportunity Name in case trigger by ca.Opportunity__r.Name because case only keeps the Id of the Opportunity.
- You don't need to update the case. Logics of the trigger executes before insert or update i.e. before the system DML operation.
Here I ahave a piece of code which you can try,Thanks :)