Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to pass variables from Business Rule to Workflow

Saikat Mitra
Tera Contributor

Hi Team,

 

I have written the following piece of Business Rule to pass variables in Workflow.

var wf = new Workflow();
//Get the workflow id
var wfId = wf.getWorkflowFromName("SIC_Workflow_Work");
//Start workflow, passing along object containing name/value pairs mapping to inputs expected by the workflow
var vars ={};
vars.input_var_name1 = parsedData.result.short_description;
vars.input_var_name2 = parsedData.result.number;
// add as many variables as your workflow is expecting, then pass the object
var a ="Test 1";
var b ="Test 2";

wf.startFlow (wfId,null,"SIC_Workflow_Work",a,b);
//wf.startFlow (wfId,null,"SIC_Workflow_Work",vars);

 

The workflow is triggering but variables are not being passed. Here is my workflow script.

 

gs.log("saikat workflow triggered");

workflow.scratchpad.var1 =workflow.inputs.a;
workflow.scratchpad.var2 =workflow.inputs.b;

gs.log("saikat step 1"+workflow.scratchpad.var1+"format"+typeof workflow.scratchpad.var1);
gs.log("saikat step 2"+workflow.scratchpad.var2+"format"+typeof workflow.scratchpad.var2);

 

The first log message is printing but the scratchpad logs are appearing to be undefined. Seeking your expert opinion to read the variables passed from BR to Workflow. I made a lot of changes to achieve this so far but nothing worked.

3 REPLIES 3

SanjivMeher
Mega Patron
Mega Patron

Did you make sure to add the input variables to the workflow as well?

https://docs.servicenow.com/bundle/vancouver-build-workflows/page/administer/using-workflows/concept...

 


Please mark this response as correct or helpful if it assisted you with your question.

Thanks for highlighting. I added it but still no help. Here is the updated script. "u_yet" and "u_met" are the input variable column names.

 

gs.log("saikat workflow triggered");

workflow.scratchpad.var1 =workflow.inputs.u_yet;
workflow.scratchpad.var2 =workflow.inputs.u_met;

gs.log("saikat step 1"+workflow.scratchpad.var1+"format"+typeof workflow.scratchpad.var1);
gs.log("saikat step 2"+workflow.scratchpad.var2+"format"+typeof workflow.scratchpad.var2);

This is what you should have had

var vars ={};
vars.u_yet = parsedData.result.short_description;
vars.u_met = parsedData.result.number;
// add as many variables as your workflow is expecting, then pass the object
wf.startFlow (wfId,null,"SIC_Workflow_Work",vars);

 

And then in workflow you can use it. Also I would suggest using gs.info instead of gs.log.


Please mark this response as correct or helpful if it assisted you with your question.