- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:17 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:21 PM - edited 11-07-2023 06:46 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:21 PM - edited 11-07-2023 06:46 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 08:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 10:15 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:51 PM - edited 11-07-2023 06:57 PM
Hi @shaikkhasim
Let's try the Auto-populate feature with no script. 😋
And also in your client script, try not to use GlideRecord directly there.
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