You need to sign in to do that
Don't have an account?
John Neilan
Fire Trigger Only When Stage Moves to Closed Won
I have the trigger class below and I would like it to fire nly when an Opportunity Stage is changed to Closed Won. Can anyone helpme figure out how to do this? I have an If statement, but it fires any time an Opportunity that is Closed Won is edited, and when I tried to add another condition it gives me the error message: Error: Compile Error: Field expression not allowed for generic SObject at line 12 column 85. I only want it to fire when the Stage is initially changed. Thanks.
Trigger:
Trigger Class:
Trigger:
trigger MainTriggerOpportunity on Opportunity (after update) { ClassRenewalOppClone updater13 = new ClassRenewalOppClone(); updater13.cloneOpp(Trigger.new); }
Trigger Class:
public class ClassRenewalOppClone { public void cloneOpp(List<Opportunity> cloneOpp){ String recordTypeName = 'Renewals'; Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); FOR(Opportunity opp1 : cloneOpp){ IF(opp1.StageName.contains('Closed Won') && trigger.OldMap.get(opp1.Id).isclosed == false && opp1.RecordTypeId == recType){ String OppId = opp1.Id; //Clone the Opportunity that is associated with the handoff and all createable fields /* query Opportunity and then clone it */ String soql = RecClone.getCreatableFieldsSOQL('Opportunity','Id =: OppId'); Opportunity opp = (Opportunity)Database.query(soql); Opportunity opp2 = opp.clone(false, true); insert opp2; List<OpportunityLineItem> itemList = (List<OpportunityLineItem>)Database.query(RecClone.getCreatableFieldsSOQL('OpportunityLineItem','OpportunityId =: OppId')); List<OpportunityLineItem> newItemList = new List<OpportunityLineItem>(); for (OpportunityLineItem item : itemList) { OpportunityLineItem ol = item.clone(false, true); ol.totalprice = null; ol.opportunityid = opp2.id; newItemList.add(ol); } insert newItemList; } } } }
- As this is an After trigger, you don't have acces to Trigger.oldMap - https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
- So, first I would suggest making it a (after insert, after update) trigger - That way you're covered if the user creates a new record and immediately sets it to close won and saves.
- Then you could create a new field on Opportunity to store the previous value of the StageName and populate it with a Before trigger or possibly workflow (can't reemember the order of operations, but I think a workflow would do it and save you some coding)
- Then update your ClassRenewalOppClone class to compare the previous value of StageName to the current value of StageName in Trigger.new and I think you'll be set.
Hope this helps!Once your questions is answered, please take a moment to mark a post as "Best Answer" to help others in the community with similar questions. Thanks and good luck!
Then the order of operation would be
Good luck.