function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
iSqFt_ADiSqFt_AD 

Trigger Assistance - How do I get this trigger to only fire the first time the criteria is met?

Disclaimer: I am not a developer. I had someone assist me with the code for this trigger. It works great except for it fires every time there is an edit and I need it to only fire the first time the Stage is set to Closed Won.

 

trigger Original_Owner_Role on Opportunity (before insert, before update){

Set<Id> uIds = new set<Id>();
for(Opportunity o : Trigger.New){
uIds.add(o.OwnerId);
}
Map<Id,User> userOppty = new Map<Id,User>([Select Id, UserRole.Name from User where Id IN :uIDs]);
for(Opportunity o : Trigger.New){
if(o.StageName == 'Closed Won'){
o.Original_Owner_Role__c = userOppty.get(o.OwnerId).UserRole.Name;
}
}
}

 

jucuzoglujucuzoglu

This post should help you with the code: http://boards.developerforce.com/t5/Apex-Code-Development/How-to-find-the-values-in-trigger-old/td-p/160641

 

Essentially you want to check that the Stage value for Trigger.Old and Trigger.New are not equal. If they are not equal you can continue processing the code. Let me know if an example would help.

raseshtcsraseshtcs

You can create a hidden field on the opportunity and set the value to true when the trigger runs for the first time. And in the trigger check the value of the field before starting any processing.... This might not be the most optimal solution but will work for sure !!!!

iSqFt_ADiSqFt_AD

Thanks for the link but not being a developer, it leaves me just as confused as before I read the posts. I did not write the trigger, so I do not understand the old trigger and new trigger code that the link references. 


Id prefer to not add a field to the page for this. Can anyone assist me on how to edit this trigger code to get it to look at old and new and not update every time?

raseshtcsraseshtcs

The field will not be visible on the page or page layout it will just be an aid for the trigger to know if it has fired before for that reocord or not.

 

iSqFt_ADiSqFt_AD

I guess what I meant to say was that I prefer not to add an additional field to Salesforce. Thank you though. Right now I just need assistance on how to write the code to only fire on the first stage change to Closed Won and not upon edits.

raseshtcsraseshtcs

As far as I understand... Trigger.old and Trigger.new provides you with the information/field values before the save event and after the save event respectively. So I am not sure if we can leverage trigger.old & trigger.new to restrict the trigger from running more than once.

jucuzoglujucuzoglu

raseshtcs is technically correct. The Trigger.old and Trigger.new can only assure that the trigger is perhaps only fired when an initial value has changed to another. It is true, someone could change the value, and then change it back again to cause it to re-fire. In which case the checkbox becomes the only way to truly assure it fires once.

iSqFt_ADiSqFt_AD

If I were to use a check box field for this, would it work to use the Won field (IsWon), rather than creating a new field?

raseshtcsraseshtcs

Yes you can use the Is Won field to do this... but then you would not be able to use that field for any other purpose...