pass variables to workflow

John Warren
Kilo Guru

I'm trying to pass variables from a record producer to a workflow that I'm kicking off via the script.   Here is the script I'm running. (function() {   current.short_description = "CCS Release " + producer.name;       var vars = getVars();   gs.log("vars.application" + vars.application, "jjw");   var w = new Workflow();   var wfSysId = w.getWorkflowFromName("CCS Release");   w.startFlow(wfSysId, current, current.operation(), vars);   })();   function getVars() {           var vars = {};           for (var n in producer){             vars[n] = producer[n];       }       return vars;     }   When this runs I do get the log message displaying a sys_id for the application from the record producer. However when I log out current.variables.application or workflow.variables.application in the workflow I get undefined. It seems that my variables are not getting passed into the workflow at all.

1 ACCEPTED SOLUTION

Ah. I see. From my tests I can definitely see that current is not available to the Workflow from a Record Producer, so you cannot use current.variables.variablename. I suspect that this behaviour is by design. Though it would be good to get an Incident logged with Technical Support for confirmation so that the documentation can be revised or Problem logged.



Nevertheless, you can refer to the Article that Anurag provided. I achieved this on my demo using the OOB Create Incident Record Producer.


  1. Created a Test Workflow that has a Run Script activity.
  2. Add the following line to the script
    gs.log("workflow.variables.u_comments: " + workflow.variables.u_comments);
  3. Edit the Inputs for the Workflow and added a variable called comments (it will get automatically renamed to u_comments) with the same config as the comments variable on the Record Producer
  4. In my Record Producer I added the following script

    var wf = new Workflow();


    var   wfId = wf.getWorkflowFromName("WorkflowInputTest");


    var variableArr = {};


    variableArr.u_comments = producer.comments;


    wf.startFlow(wfId, null, "WorkflowInputTest", variableArr);



When reviewing the logs I do see the output:


*** Script: workflow.variables.u_comments: testing2



The part of the Wiki article that led me to this is here: Using Variables in a Workflow - ServiceNow Wiki (Geneva: Workflow input variables)



I hope that this helps



EDIT: Adding link to Geneva


View solution in original post

9 REPLIES 9

John Warren
Kilo Guru

Apparently the link between hi and the community is broken and eats any formatting so I'll repost.



I'm trying to pass variables from a record producer to a workflow that I'm kicking off via the script.  



Here is the script I'm running.


(function() {


  current.short_description = "CCS Release " + producer.name;



  var vars = getVars();


  gs.log("vars.application" + vars.application, "jjw");


  var w = new Workflow();


  var wfSysId = w.getWorkflowFromName("CCS Release");


  w.startFlow(wfSysId, current, current.operation(), vars);


})();



function getVars() {  


    var vars = {};  


    for (var n in producer){


          vars[n] = producer[n];


    }


    return vars;  


}



When this runs I do get the log message displaying a sys_id for the application from the record producer. However when I log out current.variables.application or workflow.variables.application in the workflow I get undefined. It seems that my variables are not getting passed into the workflow at all.


Hi John



This is the first time I am seeing a Workflow being kicked off from a Record Producer's script. I'm not sure if you will have a "complete" current record as the script is effectively running as part of, or during, the insert of the target record. Is there a reason why you are not defining a Workflow on the target table (e.g. Incident)? You can access the current.variables object from there.



EDIT: I should have mentioned that I am assuming you are indeed trying to use current since you said that you are using current.variables.variablename. If on the other hand you want to use the key/value pairs in the last parameter of startFlow(), I believe the call would be workflow.inputs.variablename. Nevertheless, I am interested in knowing the reasoning behind this route rather than letting there be a workflow on the target table which gets triggered on insert - I'm sure I will learn more about the platform.



Shahid


We are creating a custom flow that will produce a chain of SDLC records



1 release followed by a flow of stories that have child scrum tasks.



The issue I ran into is I don't want this to happen every time a release is created. It should only occur if the release is created through a specific catalog item.



So I created a record producer to create a release and start the workflow that will generate the prescribed stories and scrum tasks. Now I just need to get the variables from the intake form on the record producer to be available in the workflow.


Ah. I see. From my tests I can definitely see that current is not available to the Workflow from a Record Producer, so you cannot use current.variables.variablename. I suspect that this behaviour is by design. Though it would be good to get an Incident logged with Technical Support for confirmation so that the documentation can be revised or Problem logged.



Nevertheless, you can refer to the Article that Anurag provided. I achieved this on my demo using the OOB Create Incident Record Producer.


  1. Created a Test Workflow that has a Run Script activity.
  2. Add the following line to the script
    gs.log("workflow.variables.u_comments: " + workflow.variables.u_comments);
  3. Edit the Inputs for the Workflow and added a variable called comments (it will get automatically renamed to u_comments) with the same config as the comments variable on the Record Producer
  4. In my Record Producer I added the following script

    var wf = new Workflow();


    var   wfId = wf.getWorkflowFromName("WorkflowInputTest");


    var variableArr = {};


    variableArr.u_comments = producer.comments;


    wf.startFlow(wfId, null, "WorkflowInputTest", variableArr);



When reviewing the logs I do see the output:


*** Script: workflow.variables.u_comments: testing2



The part of the Wiki article that led me to this is here: Using Variables in a Workflow - ServiceNow Wiki (Geneva: Workflow input variables)



I hope that this helps



EDIT: Adding link to Geneva