How to trigger a workflow from script for change record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 03:46 AM
Im Using below script to triger workflow from script but not sure what parameters to be passed to startflow() function
var w_id='3fea6773cb100200d71cb9c0c24c9c46';
startWorkflow(w_id);
function startWorkflow(id) {
var w = new Workflow();
var context = w.startFlow(id,'82455b784f771b04897f30b01310c7c4'/*change record sys_id*/,null);
if (context != null)
current.context = context.sys_id;
}
Facing below error any help on this ?
ENGINE: Workflow CommandManager.killSwitch has 1 observers
completed Begin(2661c951db7f5300d25091f38a961984): event=execute
completed Change moves to Assess(f661c951db7f5300d25091f38a96199d): event=execute
Evaluator: org.mozilla.javascript.EcmaError: Cannot set property "context" of null to "c7c74d59db7f5300d25091f38a961973" Caused by error in script at line 21 18: var w = new Workflow(); 19: var context = w.startFlow(id,'82455b784f771b04897f30b01310c7c4',null); 20: if (context != null) ==> 21: current.context = context.sys_id; 22: } 23:
Evaluator: org.mozilla.javascript.EcmaError: Cannot set property "context" of null to "c7c74d59db7f5300d25091f38a961973" Caused by error in script at line 15 12: 13: var w_id='3fea6773cb100200d71cb9c0c24c9c46'; 14: ==> 15: startWorkflow(w_id); 16: 17: function startWorkflow(id) { 18: var w = new Workflow();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 03:54 AM
I use this script in a run script from a workflow to trigger another workflow and it works fine.
The first parameter points to field on a record we get with a GlideRecord query, which is a reference to the workflow we want to trigger. It should work to put in the sys_id in any other way if needed.
var wf = new Workflow();
var context = wf.startFlow(gr.u_workflow, current, current.update());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 04:50 AM
HI Palmen ,
My use case is im changing the type of change record from emergency to normal this includes cancelling the emergenecy workflow and attaching the normal workflow .
var context = w.startFlow(/*normal change workflow sys_id*/,/*change record sys_id*/,/*not sure what to pass here */,/*not sure what to pass here */);
Can you tell me those two parameters what to pass ? correct me if anything is wrong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 04:55 AM
Please see the documentation on the developer page for information about what to put as the parameters.
https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_WF-startFlow_String_GR_S_A
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2018 04:58 AM
When looking at your code it seems you pass in id (whatever that is) as the first parameter instead of the sys_id of the workflow to start.
The second parameter should be the record to use as current, instead you have a sys_id of something. If you change these things it'll probably work to create the new workflow and you also need to deal with how to cancel the previous workflow.
Please mark as Helpful/Correct if this helps or solve the problem.