The CreatorCon Call for Content is officially open! Get started here.

How to access workflow input variables in workflow script

Pratiksha Kalas
Tera Contributor

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'


 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Pratiksha Kalas 

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

AnkurBawiskar_0-1694791189094.png

 

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:

AnkurBawiskar_1-1694791189137.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

asifnoor
Kilo Patron

Hi @Pratiksha Kalas 

 

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.

Vijay Balotia1
Tera Guru

Hi @Pratiksha Kalas 

 

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

Sandeep Rajput
Tera Patron
Tera Patron

@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 ) ;

Source: https://docs.servicenow.com/en-US/bundle/rome-servicenow-platform/page/administer/using-workflows/co...

Ankur Bawiskar
Tera Patron
Tera Patron

@Pratiksha Kalas 

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

AnkurBawiskar_0-1694791189094.png

 

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:

AnkurBawiskar_1-1694791189137.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader