Retrieve all variables from Record Producers in a workflow

jan_vercauteren
Giga Contributor

We use ServiceNow for Case Management. Mostly questions and problems, but users can also request a service.

These requests are generated via Record Producers.

I set up some workflows, and found it difficult that there is no clear link between the variables in Record Producers and the workflow.

So I set up the following script right after the beginning of the workflow:

  var gr = new GlideRecord('question_answer');

  gr.addQuery('table_sys_id', current.sys_id);

  gr.addQuery('question.type', 'NOT IN', '12,20,24,19,11,14,17');

  gr.query();

  while(gr.next()) {

            workflow.scratchpad[gr.question.name] = gr.value;

            workflow.info(gr.question.name + ' : ' + gr.value);

  };

With this, I have access to all the variables from the Record Producer that I can use to add conditions and other scripts to the workflow.

UPDATE

I took me too long, but I finally solved the bug in this script. Take away: do not ever name the label of a field 'value' or something similar that is used in scripts.

Feel free to give a good explanation in the comments, but I think the script above did not put a value in the scratchpad variables, but a pointer to an ever changing variable. That's why in the end all my scratchpad variables had the same value: the value of the last record in the query.

So, what does work is:

  var gr = new GlideRecord('question_answer');

  gr.addQuery('table_sys_id', current.sys_id);  

  gr.addQuery('question.type', 'NOT IN', '12,20,24,19,11,14,17');

  gr.query();  

  while(gr.next()) {  

            workflow.scratchpad[gr.question.name] = gr.getValue('value');

            workflow.info(gr.question.name + ' : ' + workflow.scratchpad[gr.question.name]);

  };  

getValue of value. This shouldn't be.

6 REPLIES 6

Daniel Draes
ServiceNow Employee
ServiceNow Employee

Yep, common mistake. Using a dot-walked field in a loop will result in a pointer, not the value of that field. You always need to force it into a string objects by either using getValue (as you did above) or adding an empty string which would force the script to generate a new string (e.g. gr.value + '').


Amy Lind1
Giga Guru

Hi Jan,


Can you tell me how I would go about referencing that value in my workflow then?  


Amy Lind1
Giga Guru

Nevermind.   I got it!  


Hi Amy,


How did you reference the variables in the workflow?   I applied the Run Script to the beginning of the workflow, but I still cannot pull the variable answer in my workflow tasks (i.e. approver name is one of the variable questions).   Thanks.


twl