The CreatorCon Call for Content is officially open! Get started here.

How can I get the values of the record producer variables?

gmorales
Kilo Expert

Hello everyone,

I have a record producer with some variables, let say for example url1, url2, url3 type single text, I need to capture the values of these variables(This is the hard part) for a further work. I've tried using producer.variable1, and as planned get the value of the variable , but I need to get all of them using a loop or something similar, I tried to access the producer object but didn't work

var sitesArray = [];

for(var v in producer){

if(v.indexOf('url') > -1){

              var temp = '';

              if( (v.indexOf('url') > -1) && (current.variables[v] != '') ){

                      temp = v.substring(3);

                      if(sitesArray[temp] == null || sitesArray[temp] == undefined){

                              sitesArray[temp] = [];

                      }

                      sitesArray[temp]['url'] = current.variables[v];

              }

}

But seems to not work, if you guys have any suggestions please let me know.

1 ACCEPTED SOLUTION

Hello Guillermo,



I played around with a the Create Incident record producer in my personal instance. The variables are included in the producer object and their key starts with the letters "IO" followed by their sys_id. I'm not sure if there is a better/easier way to get the question text than using a GlideRecord query.



This script put the key value pair of all variables in the work_notes for me in my test:



var test = [];


for(var v in producer){


  if (v.startsWith("IO")) { //only variables


      var question = new GlideRecord('item_option_new');


      question.get(v.substring(2)); // Querying by sys_id to determine question text


      test += question.question_text + ": " + producer[v] + "\n"; // Set key:value pair to variable


  }


}


current.work_notes = test; // Set Work Notes on new record


View solution in original post

10 REPLIES 10

Robert Beeman
Kilo Sage

Hello Guillermo,



I'm having a little problem following what you are trying to do, so forgive me if I'm off base, but here are a couple of my initial observations.


  1. Line 4 and line 6 have the same condition of (v.indexOf('url') > -1) so it is redundant (doesn't change anything, just a comment).
  2. v is going to evaluate to the index number of the array. So you probably want to use producer[v] for your condition.
  3. I think you should be referencing the producer object instead of current on lines 6 and 11. Producer is what the end user submits, and current is what ends up in the record (Record Producer - ServiceNow Wiki). Generally the producer is what populates the current.

04: if(producer[v].indexOf('url') > -1){  


06: if(producer[v] != '') ){ // line 04 now makes this line redundant


11: sitesArray[temp]['url'] = producer[v];


Hi Robert,


Thanks for your reply, I changed but still not getting values, I just did a for loop and log the content of "producer[v] " I got a list of mixed values, including the ones from non empty variables, I need to figure it out how to get only the variables and it values and not all the elements inside of Producer... There are no much information about producer on wiki.


Hello Guillermo,



I played around with a the Create Incident record producer in my personal instance. The variables are included in the producer object and their key starts with the letters "IO" followed by their sys_id. I'm not sure if there is a better/easier way to get the question text than using a GlideRecord query.



This script put the key value pair of all variables in the work_notes for me in my test:



var test = [];


for(var v in producer){


  if (v.startsWith("IO")) { //only variables


      var question = new GlideRecord('item_option_new');


      question.get(v.substring(2)); // Querying by sys_id to determine question text


      test += question.question_text + ": " + producer[v] + "\n"; // Set key:value pair to variable


  }


}


current.work_notes = test; // Set Work Notes on new record


Awesome!! you did it, Thanks a lot