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

Katie A
Mega Guru

Thank you! I can send the value to the workflow as an input value. I am now trying to get the result of the workflow BACK on the client side before the form is submitted if that is possible.



Run Workflow from Client Script then retrieve value BACK before submit


HI Katie,

 

Could you please share the piece of code like how you are able to fetch the variables from script into the workflow.

I tried however it says undefined in the log.

Thanks,

Akram

 

script:

var wf = new Workflow();
var wfId = wf. getWorkflowFromName ( "Scheduling Deprovision for ContEmp" ) ;

var vars={};
requestsys_id=createRequest.sys_id.toString();
vars.test="sampledata";
wf.startFlow(wfId, conEmp, conEmp.operation(), vars);

 

workflow:

var test1=workflow.inputs.test;
gs.info("test for auto call in workflow "+test1);

 

I am trying to get the 'vars.test' value in workflow.