Passing a value from a script to use in a workflow

Katie A
Mega Guru

I have a basic scheduled job that kicks off a workflow.

I want to be able to pass the value of a variable to the workflow and use it within the workflow.

Here is the scheduled job that is intended to pass the value (currently it is just a hard-coded string for testing). Once this is working it will be an onchange client script via glideajax that will pass the value of the catalog item variable.

var gr = new GlideRecord('wf_workflow');

  gr.addQuery('name', 'Hostname Check if Taken');

  gr.query();

  if (gr.next()) {

  var wf = new Workflow();

  var workflowId = '' + gr.sys_id;

  var vars = {u_hostname: "hostname123"};

  wf.startFlow(workflowId, current, current.operation, vars);

  }


Then within the workflow there is a   "Run Script" activity that sets the variable to the scratchpad and should print the value to the log. However the log shows that the variable is undefined. It should be   as that hard-coded string "hostname123"

workflow.scratchpad.u_hostname = current.variables.u_hostname;

var hostname = workflow.scratchpad.u_hostname;

gs.log(hostname);

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

I think you also need to configure this variable in workflow inputs and then the variable will be accessible using workflow.inputs.variable_name


View solution in original post

6 REPLIES 6

dhasselquist
Mega Guru

Try modifying:


var vars = {u_hostname: "hostname123"};



To


var vars = {"u_hostname": "hostname123"}; 

Inactive_Us1883
Mega Expert

Hi,



Please replace the code:


var vars = {u_hostname: "hostname123"};  



with



var vars = {};


vars.u_hostname = "hostname123";



Best Regards,


/Suraj


Gurpreet07
Mega Sage

I think you also need to configure this variable in workflow inputs and then the variable will be accessible using workflow.inputs.variable_name


Gurpreet07
Mega Sage

Check section 4 from following link


Using Variables in a Workflow - ServiceNow Wiki