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.

Getting value of the field instead of sys_id

ravali6
Giga Contributor

I have a script where I do a GlideRecord query to the request table. Once I get the records, I am getting to the value of the opned by field. But since opened by is a reference field, it is returning sys_id. How can I get the displayed value ?

Below is the script :

var gr = new GlideRecord("sc_req_item");
gr.addQuery("u_requested_for", g_form.getValue("emp_name"));
gr.addQuery("stage",'!', "waiting_for_approval");
gr.query();

if(gr.next())
{
alert("A request has already been submitted by " + gr.opened_by); // this is returming sys_id, I need the value

}


Please let me know if anyone has any suggestions.

22 REPLIES 22

Deepak,


Your suggestion makes sense, but what can you do if you have multiple reference fields?   Do I have to use some if statements for each reference field?



Here's my current code, it works great except I get the sys_id anytime we use a reference variable.



var item = GlideRecord("sc_req_item");


item.addQuery('sys_id','71bb57fb0f93030059daba8ce1050e11');


item.query();


while(item.next()){


        var c_item = GlideRecord("sc_cat_item");


        c_item.addQuery('sys_id',item.cat_item);


        c_item.query();


        while(c_item.next()){


                  gs.info("<a href='" + "sp?id=ticket&table=sc_req_item&sys_id=" + item.sys_id + "'>" + item.number + "</a>:   " + c_item.name + " <br />");


                  var item_vbls = new GlideRecord("sc_item_option_mtom");


                  item_vbls.addQuery('request_item', item.sys_id);


  item_vbls.orderBy('sc_item_option.order');


                  item_vbls.query();


                  while(item_vbls.next()) {


                  gs.info("<b>" + item_vbls.sc_item_option.item_option_new.question_text + ": </b>" + item_vbls.sc_item_option.value + "<br />");


}  


}


}


To get the display value at server side... user gr.getDisplayValue('field_name')


I actually got it work using functions GlideappVariablePoolQuestionSet() and getFlatQuestions().   Here's the snip it of code that I used:



var set = new GlideappVariablePoolQuestionSet();  


                  set.setRequestID(req.sys_id);  


                  set.load();  


                  var vs = set.getFlatQuestions();  


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


                          if(vs.get(i).getLabel() != '') {  


                                            gs.info("<b>" + vs.get(i).getLabel() + ":</b> " + vs.get(i).getDisplayValue() + "<br />");  


                          }    


Rashmi Bansal
Mega Guru

Hi Ravali,



Please try below code and fetch your name value via script include.



Script Include:


find_real_file.png


Client script : onload


find_real_file.png



Please mark my answer correct if it works for you.



Thanks,


Rashmi


if you are hitting a refrence field by GlideRecord API and to getDisplayValue not sys_id you must do following:


1. gr.getDisplayValue('field_name')


2. Ensure that the particular field also has a display value true in table on which you are hitting glide record else it will show sys_id.