Need an example of how to get reference field and auto-populate a field

Lexle
Tera Contributor

Hello, I need some guidance as I have never used glideAjax() before. I need to get two reference fields in order to auto-populate fields on the catalog client script. 

My scenario:

Catalog Client script -

  • need floor and building fields (user reference fields) to auto-populate based on script includes reference fields.
  • There is a 'requested for' field that references the user table.

Script includes-

  • need floor and building reference field sys_id's based on user sys_id.
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

First consider/try using the code-less Variable auto-populate feature:

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc... 

 

If this doesn't work in your scenario, here is an excellent guide and walk-through of GlideAjax

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

Hi @Lexle 

use the below Script : 

 

onchange client script :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var sitedetails = g_form.getValue('requested_for');
    alert(sitedetails);

    var ga = new GlideAjax('GetManagerDetailsScript');
    ga.addParam('sysparm_name', 'getsitedetails');
    ga.addParam('sysparm_site', sitedetails);
    ga.getXMLAnswer(callback);

    function callback(response) {
        var answer = response;
        answer=answer.split(',');
		g_form.setValue('description',answer[0]);
		g_form.setValue('latitude',answer[1]);
		

    }
    //Type appropriate comment here, and begin script below

}

 

Script include :

 

var GetManagerDetailsScript = Class.create();
GetManagerDetailsScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getsitedetails: function() {
        var sitedetailsvalue = this.getParameter('sysparm_site');
        var userdetails = new GlideRecord('sys_user');
        userdetails.addQuery('sys_id', sitedetailsvalue);
        userdetails.query();
        if (userdetails.next()) {
            return userdetails.email + ',' + userdetails.last_name;
        }

    },



});

 

 

Thanks and Regards

Sai Venkatesh

Sumanth16
Kilo Patron

Hi @Lexle ,

 

Below is the sample script:

 

 

Client script:

 

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

 

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

 

          return;

 

    }

 

   

 

    var ga = new GlideAjax('GetCustomerData');

 

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

 

  ga.addParam('sysparm_field','u_reference_1');

 

  ga.addParam('sysparm_user_id', g_form.getValue('caller'));

 

      ga.getXML(DoSomething);

 

       

 

      function DoSomething(response) {

 

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

 

            g_form.setValue('company',company);

 

      }

 

}

 

Script include:

 

var GetCustomerData = Class.create();

 

GetCustomerData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

 

    getFieldValue: function() {

 

              var user = new GlideRecord('x_medt2_ihs_customers');

 

              user.get(this.getParameter('sysparm_user_id'));

 

              if(! user.sys_id){

 

                                return false;

 

              }

 

        return user[this.getParameter('sysparm_field')];

 

    }

 

});

 

Mark my answer correct & Helpful, if Applicable.

 

Thanks,

Sumanth meda