logo

NJP

mini lab : Return multiple values to GlideAjax from Script include

Import · Feb 03, 2020 · article

Hi All,

This lab is simple and easy to understand. this will give you idea about returning multiple value from script include to client side.

Requirement

when we select any user in form, all the details of that user should get auto-populated.

image

Design

Created form with 5 fields

-User (referring to sys_user table)

-User Department (referring to cmn_department table )

-User Email (type String)

-User Location (referring to cmn_location table)

-User Company (referring to core_company table)

Development

1. Client Script

Name: AutoPopulateUserData

Table: u_test

UI: all

Type: onChnage

Field: user

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

    var ga = new GlideAjax('AutoPopulateUserData');
    ga.addParam('sysparm_name','populatedata');
    ga.addParam('sysparm_userID',newValue);
    ga.getXML(callback);

 function callback(response)
    {
        var ANSWER =response.responseXML.documentElement.getAttribute('answer');
        var parsedVal = JSON.parse(ANSWER);

        g_form.setValue('u_department',parsedVal.dept);
        g_form.setValue('u_user_location',parsedVal.loc);
        g_form.setValue('u_user_email',parsedVal.eml);
        g_form.setValue('u_user_company',parsedVal.cmp);

    }

    //Type appropriate comment here, and begin script below

}

2. Script Include

Name: AutoPopulateUserData

Client Callable: Checked

var AutoPopulateUserData = Class.create();
AutoPopulateUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    populatedata:function()
    {
        var obj={};
        var id=this.getParameter('sysparm_userID');
    var gr=new GlideRecord('sys_user');
    gr.addQuery('sys_id',id);
    gr.query();
    if(gr.next())
    {
    obj.dept=gr.getValue('department');
    obj.eml=gr.getValue('email');
    obj.loc=gr.getValue('location');
    obj.cmp =gr.getValue('company');
}
    return JSON.stringify(obj);

    },

});

If you find this article helps you, don't forget to log in and "like" it!

Thanks,

Shrutika

View original source

https://www.servicenow.com/community/itsm-articles/mini-lab-return-multiple-values-to-glideajax-from-script-include/ta-p/2311967