- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 04:09 AM - edited 09-15-2023 04:11 AM
How to access workflow input variables in workflow script?
I used below script in scheduled job
var ch;
var op='update';
var a = new GlideRecord('change_request');
a.addEncodedQuery('state!=-2^start_dateISNOTEMPTY^ORwork_startISNOTEMPTY^work_startRELATIVELT@minute@ago@15^number=CHG0000096');
a.query();
if (a.next()) {
var ch=a.short_description;
}
else{
ch='ERROR';
}
var vars=[];
vars.sd = ch;
var wf = new Workflow();
var wf_ID = wf.getWorkflowFromName('TEST:WF Inputs');
wf.startFlow(wf_ID, a, op, vars);
To access the input variable in workflow I used below script
var input=workflow.inputs.sd;
I try to check the value of input by using logs, it's 'undefined'
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 08:20 AM
should be simple enough
I shared the steps in this link with screenshots
Trouble Accessing variables from inbound action
sharing the screenshots again here
1) Create workflow input variable
2) Then send the data like this
var inc = new GlideRecord('incident');
inc.get('8d44ecd62ff56010aedd55f62799b691');
inc.short_description = 'test';
inc.update();
var wflow = new Workflow();
var wfId = wflow.getWorkflowFromName("Incident") ;
var vars= {};
// you need to set the value in the same column name u_username
vars.u_username = 'my_id_to_workflow';
wflow.startFlow (wfId, inc, inc.operation() , vars) ;
3) Then access it like this
// u_username is the column name which you need to use to access the value
gs.info("workflow value"+ workflow.inputs.u_username);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 07:33 AM
You need to pass the inputs when you are calling the flow. The syntax will be like this where you need to define the inputs in the var
var vars={ key1: value1, key2: value2,};
vars.sd = ch;
var wf = new Workflow();
var wf_ID = wf.getWorkflowFromName('TEST:WF Inputs');
wf.startFlow(wf_ID, a, op, vars);
Kindly mark the comment as a correct answer and also helpful if this has helped to solve your problem.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 07:47 AM - edited 09-15-2023 07:48 AM
Input parameters need to define in key value pair of JSON. can update in your script.
var vars={
"sd":ch
};
Thanks,
Vijay Balotia
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks
Vijay Balotia

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 08:05 AM
@Pratiksha Kalas You should be setting the workflow variables as follows.
var wf = new Workflow ( )
//Get the workflow id
var wfId = wf. getWorkflowFromName ( "Workflow Name" ) ;
//Start workflow, passing along object containing name/value pairs mapping to inputs expected by the workflow
var vars = { } ;
vars. input_var_name1 = input_var_value1 ;
vars. input_var_name2 = input_var_value2 ;
// add as many variables as your workflow is expecting, then pass the object
wf. startFlow (wfId , null , "Workflow Name" , vars ) ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 08:20 AM
should be simple enough
I shared the steps in this link with screenshots
Trouble Accessing variables from inbound action
sharing the screenshots again here
1) Create workflow input variable
2) Then send the data like this
var inc = new GlideRecord('incident');
inc.get('8d44ecd62ff56010aedd55f62799b691');
inc.short_description = 'test';
inc.update();
var wflow = new Workflow();
var wfId = wflow.getWorkflowFromName("Incident") ;
var vars= {};
// you need to set the value in the same column name u_username
vars.u_username = 'my_id_to_workflow';
wflow.startFlow (wfId, inc, inc.operation() , vars) ;
3) Then access it like this
// u_username is the column name which you need to use to access the value
gs.info("workflow value"+ workflow.inputs.u_username);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader