- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 02:01 AM
I have a variable of type Lookup Select box which is refering to sys_user table. I want to autopopulate the selected user email in another text box field. How can i achieve this.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 02:13 AM - edited 04-19-2023 02:24 AM
Hi @Sanjay21 ,
You can write a onChange Catalog client script and use Client Side GlideRecord to get the user email, or you can use GlideAjax to get the user details and display in the other field.
But, The best practice is to use the GlideAjax and call a ClientCallable Script Include to get the user details.
You can use the following scripts, but make sure to change the field names in onChange client script to the one you are using.
Client Callable Script Include:
var CatalogUserData = Class.create();
CatalogUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmail: function(){
var user = this.getParameter('sysparm_user');
var usrGr = new GlideRecord('sys_user');
if(usrGr.get('sys_id', user)){
return usrGr.getValue('email');
}else{
return '';
}
},
type: 'CatalogUserData'
});
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading && newValue) {
var kbGa = new GlideAjax('CatalogUserData');
kbGa.addParam('sysparm_name', 'getUserEmail');
kbGa.addParam('sysparm_user', newValue);
kbGa.getXMLAnswer(populateEmail);
}
function populateEmail(answer) {
if (answer) {
g_form.setValue('user_email', answer);
} else {
g_form.setValue('user_email', '');
}
}
}
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 02:13 AM - edited 04-19-2023 02:24 AM
Hi @Sanjay21 ,
You can write a onChange Catalog client script and use Client Side GlideRecord to get the user email, or you can use GlideAjax to get the user details and display in the other field.
But, The best practice is to use the GlideAjax and call a ClientCallable Script Include to get the user details.
You can use the following scripts, but make sure to change the field names in onChange client script to the one you are using.
Client Callable Script Include:
var CatalogUserData = Class.create();
CatalogUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmail: function(){
var user = this.getParameter('sysparm_user');
var usrGr = new GlideRecord('sys_user');
if(usrGr.get('sys_id', user)){
return usrGr.getValue('email');
}else{
return '';
}
},
type: 'CatalogUserData'
});
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading && newValue) {
var kbGa = new GlideAjax('CatalogUserData');
kbGa.addParam('sysparm_name', 'getUserEmail');
kbGa.addParam('sysparm_user', newValue);
kbGa.getXMLAnswer(populateEmail);
}
function populateEmail(answer) {
if (answer) {
g_form.setValue('user_email', answer);
} else {
g_form.setValue('user_email', '');
}
}
}
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 02:25 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2023 11:16 PM
Hi Anvesh,
Thanks it worked for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 02:14 AM
You can have a onchange client script and set the filed depending on the user.
Below is the sample client script code :
function onChange(control, oldValue, newValue, isLoading) {
g_form.getReference('backend_name_of_uservariable', poopulateEmail);
}
function poopulateEmail(user) {
g_form.setValue('backend_name_of_email', user.getValue('email'));
}