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.

Fetch Value of Lookup select box?

sneha64
Tera Contributor

I have requirement to fetch the variable value which is of Lookup select box.

The variable is declared as:

find_real_file.png

 

find_real_file.png

 

Now as we see its Lookup field value is Sysid rather than country so if i write a client script as follow:

find_real_file.png

 

 

Can anyone help me in how to get the value of this field as i need to this value to update sys_user table?

 

Regards,

Sneha 

 

 

 

3 REPLIES 3

shloke04
Kilo Patron

Hi,

Please use below code

 

g_form.getValue('variables.country');

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

sneha64
Tera Contributor

No it is still giving me the Sys_id -

find_real_file.png

 

Regards,

Sneha

To get the Display Value in Client Side you need to use below syntax:

g_form.getDisplayBox('variables.country').value;

Or you can use G-form.getReference method as well with a callback function defined as explained below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        var getRITM = g_form.getReference('requested_item', getRITMDetails);
    }

    function getRITMDetails(details) {
        g_form.setValue('number', details.number); //Replace number with Display Name of the field you want.
    }

    //Type appropriate comment here, and begin script below

}

Or if above syntax does not works then you can also use GLideAjax as well and make a Server call to get the Display Value . Below is an sample example on how to useGlideAjax:

Client Side:

 var ga = new GlideAjax('Script Include Name');

      ga.addParam('sysparm_name', 'Function Name Defined In Script Incldue');

      ga.addParam('sysparm_buildingid', g_form.getValue("variables.country"));

      ga.getXML(getResults);

}

function getResults(response) {

      var answer = response.responseXML.documentElement.getAttribute("answer");

        Now based on Answer you can set what you want where answer is the Display Value returned from Script Incldue

}

 

Script Include:

var ScriptIncludeName = Class.create();

ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      getResult: function () {

              var buildingid = this.getParameter('sysparm_buildingid');

              var loc = new GlideRecord('cmn_location');

              if (loc.get(buildingid)) {

                      var campus = new GlideRecord('cmn_location');

                      if (campus.get(loc.parent)){

                              var json = new JSON();

                              var results = {

                                      "sys_id": campus.getValue("sys_id"),

                                      "name": campus.getValue("name")

                              };

                              return json.encode(results);

                      }

              } else {    

                      return null;

              }            

      }

});

Just an sample code, you can modify it as needed or let me know your detailed requirement, I can modify and share the updated code with you.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke