How can I access Record Producer names in a script?

ianj
Kilo Expert

I have created a new Record Producer, and am trying to use the resulting record in a workflow that's triggered from that table.   I've gotten to the point where I can retrieve the field values (ie, the answers to the questions in the RP), but I haven't figured out how to retrieve the names of the fields to correspond to the values.   Using this code in the workflow:

 

var fields = producer.getFields();

 

for (var i=0; i<fields.size(); i++)

{

      var ge = fields.get(i);

      if (ge.hasValue())

      {

              workflow.info(ge.getName() + " = " + ge);

      }

}


I get the following result:


IO5cb71d636fba21004740bf21be3ee4f3 = no

IO5cb71d636fba21004740bf78aaf3ed90 = 15

(and so on)


All the fields I expect to see are there, which is great.   However, I'd like to be able to retrieve the 'name' field specified on the RP table programmatically, for processing in the workflow.   Specifically, I was hoping to see the following result:


action_required = no

minutes = 15

(and so on)

 

If I run this code, it works like I expect:


if (producer.minutes > 10)

{

        workflow.info("minutes greater than ten");

}

else

{

        workflow.info("minutes less than ten");

}


I'm quite sure there's a simple way to do this, and I'm just not experienced enough in SN to know what it is.   Can anyone provide me with a pointer to getting the name of the field along with the field value?

1 ACCEPTED SOLUTION

morgang
Giga Expert

You might try querying the question_answer table by the sys ID of the current record.  



Something like:



function getProducerVariablesfn()


{


     


      var gr = new GlideRecord('question_answer'); //Indicate the table to query from


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


gr.query(); //Execute the query


while (gr.next()) { //While the recordset contains records, iterate through them


      current.comments = 'Question:'+gr.question.question_text +' (with the variable name '+gr.question.name+')'+' Answered with the value:'+gr.value;


      gs.addInfoMessage('Question:'+gr.question.question_text +' (with the variable name '+gr.question.name+')'+' Answered with the value:'+gr.value);      



}


     


} getProducerVariablesfn();



In our instance I had to add a delay first before running the above, guessing values may have been writing out / committing to the table, but they do render in the work notes.


View solution in original post

2 REPLIES 2

morgang
Giga Expert

You might try querying the question_answer table by the sys ID of the current record.  



Something like:



function getProducerVariablesfn()


{


     


      var gr = new GlideRecord('question_answer'); //Indicate the table to query from


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


gr.query(); //Execute the query


while (gr.next()) { //While the recordset contains records, iterate through them


      current.comments = 'Question:'+gr.question.question_text +' (with the variable name '+gr.question.name+')'+' Answered with the value:'+gr.value;


      gs.addInfoMessage('Question:'+gr.question.question_text +' (with the variable name '+gr.question.name+')'+' Answered with the value:'+gr.value);      



}


     


} getProducerVariablesfn();



In our instance I had to add a delay first before running the above, guessing values may have been writing out / committing to the table, but they do render in the work notes.


Thanks Morgan, that's exactly what I needed!