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

Maik Skoddow
Tera Patron
Tera Patron

Hi

there is no need to script anything as the prefilling can be configured for your catalog variables:

MaikSkoddow_0-1691558372371.png

 

yes this one is prefered way, but requirement is to achive scripting only

Prince Arora
Tera Sage

@Gillerla Rajesh 

 

You can achieve this without using script include (with the method of getReference) as below:

 

var data = g_form.getReference("requestor",mydata);


function mydata(data){

g_form.setValue("location",data.location);

g_form.setValue("phone",data.phone); // please check the fields backend value according to your instance

}

 

 

Please update the field values according to your instance, in this case you dont need to call script include,, just mention this code in client script

 

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

 

i tried this one but location field i am getting sys_id , so i move to script include.

Hello @Gillerla Rajesh , Have you tried option posted by me ?