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

tmwrapelee
Tera Contributor

Jan,


Can you tell me how I would go about referencing that value in my workflow then? I am trying to retrieve the value of one of the record producer variables in an approval task, but the result is always returns "skipped."   The workflow activity log does capture the values, but I cannot seem to grab the value in my script.   Any help would be greatly appreciated.



Capture approval task for application access.PNG


Best regards,


Theresa


Hi tmwrapelee


Know this is a fairly older post, but just found this and wanted to just let you know, incase you didn't find answer, is you don't need the .value at the end.   I was able to do it with just the workflow.scratchpad.<question_name> :


12_39_55.png



Hopefully you already got this!