Catalog client script is not working

shaikkhasim
Tera Contributor

Hi Team

i have created a catalog client script 

i have created two fields, user name and email ..my requirement is , if i select the user , email should be auto populate in email filed 

 

user name (type is reference)

email (type singline text)

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var userid=g_form.getValue('user_name');
var grv=new GlideRecord('sys_user');
gr.addQuery('sys_user',userid);
gr.query();
while(gr.next())
{
g_form.setValue('email',grv.email);
}

 

Regards

Khsaim

 

khasim
1 ACCEPTED SOLUTION

AshishKM
Kilo Patron
Kilo Patron

Hi @shaikkhasim 

 

Please correct the result set name use either gr or grv.

and update the addQuery parameter it should be “sys_id” not the “sys_user”.

 

use the updated code: 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var userId=g_form.getValue('user_name');
var grv=new GlideRecord('sys_user');
grv.addQuery('sys_id', userId);
grv.query();
// use if condition as only one user will match the sys_id record
if(grv.next())
{
g_form.setValue('email',grv.email);
}

 

-Thanks

AshishKMishra

Please accept solution and mark helpful for others if it helps you.

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

View solution in original post

6 REPLIES 6

AshishKM
Kilo Patron
Kilo Patron

Hi @shaikkhasim 

 

Please correct the result set name use either gr or grv.

and update the addQuery parameter it should be “sys_id” not the “sys_user”.

 

use the updated code: 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var userId=g_form.getValue('user_name');
var grv=new GlideRecord('sys_user');
grv.addQuery('sys_id', userId);
grv.query();
// use if condition as only one user will match the sys_id record
if(grv.next())
{
g_form.setValue('email',grv.email);
}

 

-Thanks

AshishKMishra

Please accept solution and mark helpful for others if it helps you.

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hi Ashish

 

your explanation is too good.. 

with the help of sys_id, my code is running success fully ..

Thanks for your support 

 

Regards

khasim

khasim

Hi @shaikkhasim ,

Good to hear back, happy to help.

 

-Thanks

AshishKMishra

Please accept solution and mark helpful for others if it helps you.


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Tai Vu
Kilo Patron
Kilo Patron

Hi @shaikkhasim 

Let's try the Auto-populate feature with no script. 😋

TaiVu_0-1699411798541.png

 

And also in your client script, try not to use GlideRecord directly there.

Screenshot 2023-11-08 at 09.50.59.png

 

Let's use GlideAjax instead. Sample below.

 

//Client Callable Script Include
var CLCatalogItemUtilAJAX = Class.create();
CLCatalogItemUtilAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getUserEmail: function(){
		var userSysID = this.getParameter('sysparm_user_sys_id');
		var grUser = new GlideRecord('sys_user');
		if(grUser.get(userSysID)){
			return grUser.getValue('email');
		}
		return '';
	},

    type: 'CLCatalogItemUtilAJAX'
});
//Client Script
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue === '') {
        g_form.clearValue('email');
        return;
    }

    var ga = new GlideAjax('global.CLCatalogItemUtilAJAX');
    ga.addParam('sysparm_name', 'getUserEmail');
    ga.addParam('sysparm_user_sys_id', newValue);
    ga.getXMLAnswer(function(response) {
        g_form.setValue('email', response);
    });
}

 

 

 

 

Cheers,

Tai Vu