The CreatorCon Call for Content is officially open! Get started here.

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

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

@Gillerla Rajesh 

 

You can do one thing change the data type of location as Reference to "cmn_location" table, it will work 100%

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Harish KM
Kilo Patron
Kilo Patron

Hello if your using Utah version, you dont need a script to populate, use autopopulate option under variable

Regards
Harish

Maik Skoddow
Tera Patron
Tera Patron

Hi @Gillerla Rajesh 

may I ask why you accept the complicated, error-prone solution with scripting if you can have a no-code solution as proposed by me?

Just want to know why you don't use the provided OOTB features and instead want to increase the risk of broken functionalities.

Maik