You need to sign in to do that
Don't have an account?
Joe Stolz
How to prevent a workflow from making my trigger fire twice
In my Org we have a picklist field for our opportunity statuses. There's a workflow that moves the stage to 'Awarded' when the status is set to 'Close-Won.' I have a trigger that creates a custom object when a certain opportunity record type reaches 'Awarded.' I only want one object created. Everything works well as long as users follow the script of moving the status instead of the status and stage. But users always seem to find a way to break what I create (or I just didn't create them well enough).
So here is my trigger:
I could make the workflow trigger a hidden checkbox as well as update the stage, but I wanted something more straighforward. Have any of you been able to solve for this and would be willing to help me out?
Thank you,
Joe Stolz
So here is my trigger:
trigger CreatePMopp on Opportunity (after update) { List<Phasing_PM_Info__c> ppi = new List<Phasing_PM_Info__c>(); List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Opportunity' and isActive=true]; Map<String,String> opportunityRecordTypes = new Map<String,String>{}; for(RecordType rt: rtypes) opportunityRecordTypes.put(rt.Name,rt.Id); for ( Opportunity o : Trigger.new ) if ((o.RecordTypeId==opportunityRecordTypes.get('RPA Capital')) && ( o.StageName=='Close-Won') && (Trigger.oldMap.get(o.id).Status__c!='Close-Won')) { ppi.add (new Phasing_PM_Info__C( Name = o.Name, PM_Opportunity__c = o.Id)); } insert ppi; }
I could make the workflow trigger a hidden checkbox as well as update the stage, but I wanted something more straighforward. Have any of you been able to solve for this and would be willing to help me out?
Thank you,
Joe Stolz
Sorry I misunderstood your requirement a bit,
This is the solution you need,
Trigger, And the class, --Akram
All Answers
This is what I prefer to avoid this kind of problem.
First create a class with a static boolean variable to keep the state of the trigger,
Next bypass the execution of the trigger for the second time,
--Akram
I created the sharing class and added your code and now nothing. Is there anything else?
Sorry I misunderstood your requirement a bit,
This is the solution you need,
Trigger, And the class, --Akram
Thank you again for all your time and help.