Murthy Ch
Giga Sage

Hello there,

As I have seen many questions in community on how to populate the data into catalog form using scripting.

So I decided to write this small article...

1) Using GlideAjax:

Using this, will get all user details easily and its best practice by using this method:

Client Script:

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

    var requestedFor = g_form.getValue('u_requested_for'); //gettin sysID of requestedFor
    var abc = new GlideAjax('Getuserdetails'); // script include name
    abc.addParam('sysparm_name', 'autopopulation'); //function name
    abc.addParam('sysparm_requestedfor', requestedFor); //pushing requestedFor sysID into server side
    abc.getXMLAnswer(callbackdata);

    function callbackdata(response) {
        var answer = response;
        var push = answer.split('!'); //setting user details into variables
        g_form.setValue('u_first_name', push[0]);
        g_form.setValue('u_last_name', push[1]);
        g_form.setValue('u_location', push[2], push[3]); //setting both Value+displayValue
    }
}

Script Include:

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

    autopopulation: function() {
        var details = '';
        var requestedforSysID = this.getParameter('sysparm_requestedfor');
        var grObj = new GlideRecord("sys_user");
        grObj.addQuery("sys_id", requestedforSysID); //comparing sysID
        grObj.query();
        if (grObj.next()) { //getting user details
            details = grObj.first_name + '!' + grObj.last_name + '!' + grObj.location + '!'+ grObj.location.getDisplayValue();
        }
        return details;

    },

    type: 'Getuserdetails'
});

2) Using getRefrence:

This is an easy way but I don't recommend this because as this cause performance issue sometimes:

function onChange(control, oldValue, newValue, isLoading) {
    var requestedFor = g_form.getReference('u_requested_for', getDetails);

    function getDetails(requestedFor) { //callback function
        g_form.setValue('u_first_name', requestedFor.first_name);
        g_form.setValue('u_last_name', requestedFor.last_name);
        g_form.setValue('u_location', requestedFor.location);
    }

}

3) Using Catalog Data Lookup Definitions:

By using method you will get all details with NO code, because it is a OOB which is introduced in Quebec release.

For this you can checkout this article which is written by Mark.

https://community.servicenow.com/community?id=community_article&sys_id=49deac8ddb92a010ab0202d5ca961...

Thank you,

Murthy

Comments
Mark Roethof
Tera Patron
Tera Patron

Hi there,

Any reason why not using getXMLAnswer? Which performs better than getXML.

Also maybe good to mention. something like "u_location", is probably a reference. So when applying setValue, please use the value + displayValue (= performance).

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

Murthy Ch
Giga Sage

Hello @Mark Roethof 

Thanks for the suggestions..

Actually I used location as a reference variable that's why I haven't used the displayValue.

 

 

 

Mark Roethof
Tera Patron
Tera Patron

Yes that's correct, though then you need both. Technically it will work with only value, though for performance reasons you should provide the value + displayvalue.

"The third parameter

When using setValue() on a reference field, be sure to include the reference field displayValue as the third parameter. If you set the value without the displayValue, the ServiceNow instance does a synchronous call to retrieve the display value for the record you specified. This extra round trip to the server can impact performance.

So you might have set up a nice piece of Client-Side scripting, using getReference with a callback, using GlideAjax with getXML, or even better using GlideAjax with getXMLAnswer. Though if you fail to use the third parameter when using setValue(), your effort to perform an efficient lookup is actually wasted."

Kind regards,
Mark

Murthy Ch
Giga Sage

Thanks @Mark Roethof 

Understood the concept behind the third parameter

 

Thank you

Version history
Last update:
‎08-08-2021 06:50 AM
Updated by: