How to auto populate current user's name phone number and email in a catalog item.

vishalj
Tera Expert

How to auto populate current user's name phone number and email in a catalog item through client script and Script include.

 

Scenario: there are 3 variables in a catalog item: requested_for, phone_number and email.

client script: onLoad

function onLoad() {
    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('demoscript');
    ga.addParam('sysParm_name', 'vishalj');
    ga.addParam('sysParm_user', g_form.getValue('getUserID()'));
    ga.getXML(getResponse);

    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var result = JSON.parse(answer);
        g_form.setValue('requested_for', result.name);
        g_form.setValue('email', result.email);
        g_form.setValue('phone_number', result.phone);
    }



}

 

Script include: 

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

vishalj: function(){

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', this.getParameter('sysParm_user'));
gr.query();
if(gr.next()){
	var obj = {};
	obj.name = gr.name.toString();
	obj.email = gr.email.toString();
	obj.phone = gr.mobile_phone.toString();

	return JSON.stringify(obj);

}
},



    type: 'demoscript'
});

 

I used above script and methods but still not working. can anyone help in this?

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage
Tera Sage

@vishalj 

 

Updated Script

 

function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('demoscript');
ga.addParam('sysparm_name', 'vishalj');
ga.getXML(getResponse);

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
g_form.setValue('requested_for', result.name);
g_form.setValue('email', result.email);
g_form.setValue('phone_number', result.phone);
}
}

 

Script include: client callable should be checked

 

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

vishalj: function(){
var user = gs.getUserID();
var gr = new GlideRecord('sys_user');
gr.get(user);
var obj = {};
obj.name = gr.name.toString();
obj.email = gr.email.toString();
obj.phone = gr.mobile_phone.toString();
return JSON.stringify(obj);
},

type: 'demoscript'
});

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

View solution in original post

3 REPLIES 3

Jaspal Singh
Mega Patron
Mega Patron

Hi Vishal,

Change the text for sysParm to be sysparm i.e. all in lower case

Also, did you try adding logs to see what is the result printed.

Prince Arora
Tera Sage
Tera Sage

@vishalj 

 

Updated Script

 

function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('demoscript');
ga.addParam('sysparm_name', 'vishalj');
ga.getXML(getResponse);

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
g_form.setValue('requested_for', result.name);
g_form.setValue('email', result.email);
g_form.setValue('phone_number', result.phone);
}
}

 

Script include: client callable should be checked

 

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

vishalj: function(){
var user = gs.getUserID();
var gr = new GlideRecord('sys_user');
gr.get(user);
var obj = {};
obj.name = gr.name.toString();
obj.email = gr.email.toString();
obj.phone = gr.mobile_phone.toString();
return JSON.stringify(obj);
},

type: 'demoscript'
});

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

Balakrishna2
Tera Contributor

We have an Auto-populate section while creating a field in catalog items, there you can select the Dependent Question as a User reference field and start with the Dot walk path. In the Dot walk path you will be able to see the fields from the user table, so that you can easily auto-populate the value without writing the script. 

 

Balakrishna2_0-1715174176377.png