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

Retrieve choice label not the value

codedude
Mega Expert

I have a glide record where I am pulling the short description, update on and stage fields. The stage field I am having a problem with, it is giving me the value, but I really need to human readable label. What is the best way to accomplish this? This is all being done in the client script section of the UI Page. Here is my script:

for(var r = 0; r < requestsRITMs.length; r++)

  {

            var grRITMDetails = new GlideRecord('sc_req_item');

            grRITMDetails.addQuery('number', requestsRITMs[r]);

            grRITMDetails.query();

while(grRITMDetails.next())

  {

              var description = grRITMDetails.short_descritpion;

              var date = grRITMDetails.sys_updated_on;

              var stage = grRITMDetails.stage;   // this is where I am getting the value not the label. I want the label...

  }

}

12 REPLIES 12

I get this error in the console:



grRITMDetails.stage.getDisplayValue is not a function(…)


Are you doing this from a client script? If so, you'll need to retrieve the information with GlideAjax rather than a GlideRecord call.



Docs: Client Scripts


Docs: GlideForm


Docs: GlideAjax


Client Script Best Practices - ServiceNow Wiki      


So I would need to write a script include, correct?


Yes, that can return you just about anything you like in a JSON string. An array of display values, objects, array of objects, you name it. GlideAjax is ultimate flexibility. Although not terribly timely in your case, we'll be doing the next TechNow on this topic on Dec 15.



TechNow Episode List


I wrote it up, this is what I scripted and works just fine in case anyone else runs into this issue:



Script Include:



var GetChoiceLabel   = Class. create ( ) ;


GetChoiceLabel.prototype = Object.extendsObject (AbstractAjaxProcessor , {


getChoiceLabel : function ( )


{


        var choiceLabel = '';


        var grChoices = new GlideRecord('sys_choice');


        grChoices.addQuery('value', this.getParameter( 'sysparm_user_name' ));


        grChoices.query();



        while(grChoices.next()) { choiceLabel = grChoices.label; }



        return choiceLabel;


} ,


} ) ;



Client Script


var ga   = new GlideAjax ('GetChoiceLabel');


ga.addParam ('sysparm_name', 'getChoiceLabel');


ga.addParam ('sysparm_user_name', "complete");//hard coded for testing


ga.getXML (GetChoiceLabelParse ) ;



function GetChoiceLabelParse (response ) { var answer   = response.responseXML.documentElement.getAttribute ( "answer" ); alert (answer ); }