Difference between revisions of "Trigger a Workflow from a Custom Browser Script"

From SmartWiki
Jump to: navigation, search
Line 1: Line 1:
[[Workflow|Workflows]] can be triggered based on a value in a [[Custom Fields|custom field]] on the object using a [[Browser|browser]] script. The [[Workflow|workflow]] id is passed to the system from the script.
+
#REDIRECT [[Workflows_Overview#Trigger_a_Workflow_from_a_Custom_Browser_Script]]
 
 
 
 
==Syntax==
 
:<font size="3">'''_triggerwf(''objectid'',''workflowid'',''[[Entity IDs|Entity ID]]'');''' </font>
 
 
 
 
 
''The Entity ID should not be included for [[UTA]] Level 1 workflows''
 
 
 
You must first create the workflow. Once it has been created you need to [[Determining the workflowid|find the workflow ID]].
 
 
 
In the browser script you would use triggerwf with the following syntax:<br>
 
'''Level 1''':  _triggerwf('@opportunityid@',''workflowid'');<br>
 
:Example:
 
_triggerwf('@opportunityid@',98765);
 
 
 
 
 
'''Level 2''':  _triggerwf('@eventid@',''workflowid'',26);<br>
 
:Example:
 
_triggerwf('@eventid@',98765,26);
 
('''26''' is the objecttype for Level 2 activities, and will be the same for all Level 2 workflows)
 
 
 
 
 
'''NOTE''':  For [[Workflow|workflows]] triggered by a [[Browser Script]] within a [[UTA]] you should set '''Trigger When''' to '''--Deactivate--''' rather than selecting a [[Status]].
 
 
 
 
 
==Examples==
 
1) To create a button that triggers a workflow at Level 2:
 
 
 
Create a [[Custom Field Type: Read Only – System Variables|Read Only – System Variables]] Custom Field. In the '''Text/Link:''' section include the following:
 
<input type="button" class=Button value="Button Label" onClick="_triggerwf('@eventid@',''workflowid'',26);"/>
 
 
 
 
 
2) To trigger a workflow based on the value in a [[Custom Field]] at Level 1:
 
 
 
In order to trigger workflow #32123 when the Custom Field named ''My Field'' (Custom Field ID 123456) has the value ''This Value'':
 
 
 
Create a [[Browser Script]] Custom Field:
 
 
 
<pre>
 
function savefunc(frm){
 
if("@My Field@"!="This value" && frm.123456.value=="This value")
 
  {
 
  _triggerwf('@opportunityid@',32123);  // trigger the workflow before the alert so server has more time to complete the action.
 
  alert("The workflow will now be triggered");
 
  }
 
return true;
 
}
 
</pre>
 
 
 
Notes:
 
* The function [[savefunc]] is run whenever a record is saved.
 
* The if statement will ensure that the workflow is only triggered when the ''My Field'' value is '''changed''' to ''This Value'', not every time the page is saved with with ''This Value''.
 
 
 
==Notes==
 
* You must not select '''Let current user select target user from this role''' for the '''Routing''' of workflows triggered by script, as they will not execute.
 
* Because the workflow is triggered when the script executes it happens '''before''' the record is saved. If the workflow changes the [[Status]] or a Custom Field value, when the record on the screen is actually saved it will overwrite any changes made by the workflow by those on the page that is being saved. Accordingly you should include any Status or Custom Field changes within the [[JavaScript]] that triggers the Workflow.
 
* If this function is called with a button on a page where standard libraries are not included, make sure to include '''/js/sstools.js'''.
 
 
 
 
 
==See Also==
 
* [[Entity IDs]]
 
* [[Determining_the_workflowid|Determining the Workflow ID]]
 
* [[Adding a Button that Runs a JavaScript]]
 
 
 
[[Category:Workflows]][[Category:JavaScript]][[Category:Custom Fields]]
 

Revision as of 13:10, 20 July 2017