in catalog form i need to autopopulate field like location, phone no etc.

Gillerla Rajesh
Tera Contributor

in catalog form i need to autopopulate field like location, phone no etc. i written script include and client script.

but it's not fetching value.

1 ACCEPTED SOLUTION

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Gillerla Rajesh ,

               try below code

Client Script :

 

// Client script for onChange on change of requestor 
function onChangeUser() {
    var requester = g_form.getControl('requester'); // Replace 'requester' with the actual  field name

    // Create a GlideAjax instance and specify the script include name
    var ga = new GlideAjax('CustomUserUtils'); // Replace 'CustomUserUtils' with your actual script include name
    ga.addParam('sysparm_name', 'getUserInfo'); // script include function name
    ga.addParam('sysparm_user', requester);
    ga.getXML(getResponse);
    
function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            var userInfo = JSON.parse(answer);
            g_form.setValue('location_field', userInfo.location);
            g_form.setValue('phone_number_field', userInfo.phone_number);
        }
}

 

Script include : make sure it should be client callable

 

getUserInfo: function() {
        var userId = this.getParameter('sysparm_user');
        var userInfo = {};

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {
            userInfo.location = userGR.location.getDisplayValue();
            userInfo.phone_number = userGR.phone.getDisplayValue();
        }

        return JSON.stringify(userInfo);
    },

 

Kindly mark correct and helpful if applicable 

View solution in original post

12 REPLIES 12

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @Gillerla Rajesh ,

               try below code

Client Script :

 

// Client script for onChange on change of requestor 
function onChangeUser() {
    var requester = g_form.getControl('requester'); // Replace 'requester' with the actual  field name

    // Create a GlideAjax instance and specify the script include name
    var ga = new GlideAjax('CustomUserUtils'); // Replace 'CustomUserUtils' with your actual script include name
    ga.addParam('sysparm_name', 'getUserInfo'); // script include function name
    ga.addParam('sysparm_user', requester);
    ga.getXML(getResponse);
    
function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            var userInfo = JSON.parse(answer);
            g_form.setValue('location_field', userInfo.location);
            g_form.setValue('phone_number_field', userInfo.phone_number);
        }
}

 

Script include : make sure it should be client callable

 

getUserInfo: function() {
        var userId = this.getParameter('sysparm_user');
        var userInfo = {};

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {
            userInfo.location = userGR.location.getDisplayValue();
            userInfo.phone_number = userGR.phone.getDisplayValue();
        }

        return JSON.stringify(userInfo);
    },

 

Kindly mark correct and helpful if applicable 

    Can u tell me here 

userInfo.location = userGR.location.getDisplayValue();

      userInfo.phone_number = userGR.phone.getDisplayValue()

 

user info.loc, user info.phone_number is user defined one right like variable.

Harish Bainsla
Tera Sage
Tera Sage

Aman Kumar S
Kilo Patron

Hi @Gillerla Rajesh ,

I think what Chetan suggested will work for you, there is one more approach which you can use which is the no code way, which is Catalog Data Lookup.

 

Please follow below link for the same:

https://www.servicenow.com/community/developer-articles/catalog-data-lookup-definition-on-any-table-...

 

 

Best Regards
Aman Kumar