unable to fetch value with getDisplayvalue command

dpi
Kilo Contributor

Hi ,

i am trying to fetch open on behalf of user Region value (service catalog form ) and i use the below script , i tried with getDisplayValue() and get value() also , both are not working- can someone please correct/suggest me -Region field is of "String type" in cmn_localtion table

function onChange(control, oldValue, newValue, isLoading) {

  var caller = g_form.getReference('open_on_behalf_of', getValue);

  function getValue(caller)

{

var k1=caller.location;

alert(k1); //getting Sys_id here

var k=new GlideRecord('cmn_location');

k.addQuery('sys_id',k1);

k.query();

alert(k); //getting object from table here   ******i used below commands to get Region value

var k2 = k.getValue('u_region');

var k2 =k.u_region.getDisplayValue();

alert(k2);

6 REPLIES 6

Gaurav Bajaj
Kilo Sage

Hi Durgaprasad,



You missed adding the



while(k.next()){


}



Please use below script and try



function onChange(control, oldValue, newValue, isLoading) {



  var caller = g_form.getReference('open_on_behalf_of', getValue);


  function getValue(caller)


{


var k1=caller.location;


alert(k1); //getting Sys_id here



var k=new GlideRecord('cmn_location');


k.addQuery('sys_id',k1);


k.query();


alert(k); //getting object from table here   ******i used below commands to get Region value


while(k.next()){


var k2 = k.getValue('u_region');


var k2 =k.u_region.getDisplayValue();


alert(k2);




}




Please mark the response correct/helpful based on the impact.




Thanks


Gaurav


DilipKumar DJ
Kilo Guru

Hi,



Try this:



var k=new GlideRecord('cmn_location');


k.get(k1);


var k2 = k.u_region.name;// or u_name based on field name


alert(k2);


Prateek kumar
Mega Sage

Hello


Try using GlideAjax like this


Region SI.PNG



Region SI1.PNG


In the script include change "email =   gr.location.city.getDisplayValue()" to "email =   gr.location.u_region.getDisplayValue()"



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

shivam_singhal
ServiceNow Employee
ServiceNow Employee

Hi Durgaprasad,



Use the below script, It is working fine. check the screenshot attached.



function onChange(control, oldValue, newValue, isLoading) {


        if (isLoading || newValue == '') {


                  return;


        }



        var caller = g_form.getReference('caller');


        var k1 = caller.location;


       


        var k = new GlideRecord('cmn_location');


        k.addQuery('sys_id', k1);


        k.query();


       


        var location_obj = k.rows[0];


        for (var x in location_obj) {


                  if (location_obj[x].name === 'name') {


                            alert(location_obj[x].value);


                            return;


                  }


        }


}




check the location_obj value in the console as well to get a fair idea.



Screen Shot 2017-11-21 at 9.25.31 PM.png



Mark this as correct answer, If this helps in solving your use-case.



Thanks,
Shivam