Auto-populate location field on Catalog Item variable based on requested for

tom the
Giga Guru

Hi, People

I have to auto-populate several fields on a variable catalog item based on 'requested for'.

**I have it done over the auto-populate feature - the challenge is not to use it -

I have created the catalog client script and auto-populated the needed field but for location is displaying the sys ID --

g_form.setValue('requestor_location', result.location);-- or 'undefiend' when use --location.name--

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var result = g_form.getReference('requested_for', requestorInfo);

    function requestorInfo(result) {
        g_form.setValue('requestor_first_name', result.first_name);
        g_form.setValue('requestor_last_name', result.last_name);
        g_form.setValue('requestor_email', result.email);
        g_form.setValue('requestor_middle_name', result.middle_name);
        g_form.setValue('requestor_location', result.location);
        g_form.setValue('requestor_contact_no', result.mobile_phone);
        g_form.setValue('requestor_position_title', result.title);
        g_form.setValue('users_network_id', result.u_string_1);
        g_form.setValue('requestor_employee_number', result.employee_number);
    }
}
 
tomthe_0-1740111568271.png

Searching over the community, I'm missing the script include, also struggling on how to configure it done!

thanks in advance for any help!!!

1 ACCEPTED SOLUTION

tom the
Giga Guru

Thank you for all your help, I solved it the following way.

Script include

Name: getUserDetails -- 

Glide AJAX enabled: TRUE --- client callable ---

 

Script

var getUserDetails = Class.create();

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

    getUserInfo: function() {

        var userID = this.getParameter("sysparm_userID");

        var userGr = new GlideRecord("sys_user");

        userGr.addQuery("sys_id", userID);

        userGr.query();

        if (userGr.next()) {

            var obj = {};

            obj.rfname = userGr.getDisplayValue("first_name");

            obj.rmname = userGr.getDisplayValue("middle_name");

            obj.rlname = userGr.getDisplayValue("last_name");

            obj.rlocation = userGr.getDisplayValue("location");

            obj.remail = userGr.getDisplayValue("email");

            obj.rmphone = userGr.getDisplayValue("mobile_phone");

            obj.rptitle = userGr.getDisplayValue("title");

            obj.renumber = userGr.getDisplayValue("employee_number");

            obj.rntid = userGr.getDisplayValue("u_string_1");            

            var json = new JSON();

            var data = json.encode(obj);

            return data;

        }

    },

    type: 'getUserDetails'

});

 

Catalog Client Script

Name: Requestor information

Type: onChange

Variable name: Requestor Details >> requeted_for -- 'Requestor Details' = Variable set // 'Requested For' = variable --

 

Script

 

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

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

        return;

    }

    var ga = new GlideAjax('getUserDetails');

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

    ga.addParam('sysparm_userID', newValue);

    ga.getXML(callback);

    function callback(response) {

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

        answer = answer.evalJSON();

        g_form.setValue("requestor_first_name", answer.rfname);

        g_form.setValue("requestor_middle_name", answer.rmname);

        g_form.setValue("requestor_last_name", answer.rlname);

        g_form.setValue("requestor_location", answer.rlocation);

        g_form.setValue("requestor_email", answer.remail);

        g_form.setValue("requestor_contact_no", answer.rmphone);

        g_form.setValue("requestor_employee_number", answer.renumber);

        g_form.setValue("requestor_position_title", answer.rptitle);

        g_form.setValue("users_network_id", answer.rntid);

    }

}

View solution in original post

7 REPLIES 7

Swapna Abburi
Mega Sage
Mega Sage

Hi @tom the 

Using getReference method you can only dot walk to one level. As you are already dot walking to user table so you can not dot walk further to get location fields.

 

You can use a client callable script include and call the script include in your client script using GlideAjax. 

 

 

 

Ok, thank you for that information.

Yes, as far as I am searching, I am missing the script include, I am trying to understand how to configure it and how to include (call) from the catalog script include

You write script include and call it from catalog client script 

if my answer helps you mark helpful and accept solution 

Ankur Bawiskar
Tera Patron
Tera Patron

@tom the 

your getReference with callback should also work fine, no script include required.

Your location variable should be referring to cmn_location table and script should work fine

If your location variable is string then you can use onChange + GlideAjax and get the location name and then set it

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader