Catalog item variable- Lookup Selectbox

Sanjay21
Tera Contributor

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 

Sanjay21_0-1681894906894.png

 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

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'
});

AnveshKumarM_1-1681896222669.png

 

 

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', '');
        }
    }
}

AnveshKumarM_0-1681896198390.png

 

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

Sanjay21
Tera Contributor

Hi Mehta,

Thanks for the answer but this works only for reference fields not for lookup select box.

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Sanjay21 

Please use reference type and not look up select box

No scripting required. you can use Catalog Lookup definition for this

Catalog Data Lookup Definition - A zero code way to fetch data like requester Details in a catalog i... 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader