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 loop all Record producer variables in the record producer script.

nj70
Kilo Contributor

I have create a record producer for change_request.
In this I am also creating change task for this change request.

So here I want to display all the variables in the record producer in change task description field like " <variable name1 >: <variable Value1>, <variable name2>: <variable Value3>}"

I tried some script by querying from "question_answer" table to get the variable details but It is not giving the variables.

Is there any way to loop all the variables.  


Thanks In advance..

1 ACCEPTED SOLUTION

ewj
Kilo Expert

try this:


var gr = new GlideRecord('question_answer');


gr.addQuery('table_sys_id', '1116a6870f0a020062b6e64be1050ec6'); //sys_id of record in question


gr.query();


while (gr.next()) {


  if (gr.value) { //exclude questions with empty values


  gs.print(gr.question.getDisplayValue() + ":   " + gr.value); //display question and answer


  }


}



should return something like:



*** Script: Please describe your issue below: user entered description


*** Script: Open on behalf of this user: 7839639120196100636f41ee6e8383d6


*** Script: Short description (Subject Line): user entered short description



will take a bit more work if you have referenced values.


View solution in original post

11 REPLIES 11

Michael Fry1
Kilo Patron

Try something like this:


current.description = producer.variable1 + ' ,   ' + producer.variable2 + ' ,   ' +producer.variable3, etc



if one of the variables is Reference type, use producer.variable.getDisplayValue()


Thanks Michael for your reply..


If I use   "producer.variable1" will get only variable value, But I need variable name (i.e Question) also.


I want to concatenate the question and answer like " {<Question1 > : < Value1> , <Question2>: <Value>......}"


srinivasthelu
Tera Guru

I think that is the correct approach.



Can you share your script that you have prepared for reading the questions from question_answer table?, What is the trigger point for that script?


Hi Srinivas,


I have tried the below script.



ql.addQuery('table_sys_id', current.sysapproval.toString());


ql.orderBy('order');


ql.query();
var tempVar='';
gs.log('current.sysapproval.toString()::::'+current.sysapproval.toString());.




but I am getting "current.sysapproval.toString()" is undefined..



I wrote this script in Record producer script