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
das61das61 

Setting and clearing a flag

Hello.

I'm new to the forums.  I have a question regarding setting a flag (checkbox) to cause certain trigger and workflow actions.  I have a button with the javascript shown below.  There is a flag called Submit to Legal.  When this is set to true, it triggers an email alert to be sent as an action in a workflow.  I orginally thought I could then clear the flag (set to false) in an apex trigger that already exists.  It appears the flag is being cleared before the workflow is executed and therefore the email is not being sent.  When I comment out the code to clear the flag, the email is sent but the flag is not cleared.  What would be the best practice to accomplish this.

Thanks in advance.

 var agreement= new sforce.SObject("Agreement__c"); 
agreement.Id= "{!Agreement__c.Id}"; 
var Agreement_Status = "{!Agreement__c.Agreement_Status__c}"; 
//var CMO_Request_Status = "{!Agreement__c.CMO_Request_Status__c}"; 

if (Agreement_Status != 'Legal Submitted' && Agreement_Status != 'Legal Assigned' && Agreement_Status != 'Legal Drafting' && Agreement_Status != 'Legal in Review' && Agreement_Status != 'Closed'){ 
agreement.Submit_To_Legal__c = true; 
var result = sforce.connection.update([agreement]); 

if (result[0].getBoolean("success")) 
{ 
// Refresh window 
window.location = window.location.href+'?eraseCache=true'; 
window.location.reload(true); 
} 
else 
{ 
alert("Error saving Agreement"+result[0]); 
} 
}else{ 
alert('Agreement is already in Legal or Closed status.'); 
}
Best Answer chosen by das61
KaranrajKaranraj
The reason for that behaviour is all your triggers will be excuted before the workflow rule fires. Check the order of excution in salesforce https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

All Answers

KaranrajKaranraj
Instead of writting a trigger to clear the falg, you can clear the falg in the same workflow rule itself.
KaranrajKaranraj
The reason for that behaviour is all your triggers will be excuted before the workflow rule fires. Check the order of excution in salesforce https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm
This was selected as the best answer
das61das61
thanks for the reply Karanraj.  The order of execution helped a lot.  Thanks for pointing me to that documentation.  I thought adding the field update may work but not in this situation.  The trigger that already exists is adding a bit of complexity but I think I can get it to work.  Thanks for the help.