User details showing sys id's

Shaik22
Tera Expert

Hello,

I created 2 variables 1.Requested For(Reference to user table),2.Reason(Single line text).I want to requested for details like email,department,manager,location... for that i wrote on change catalog client script but the problem here is location,manager,department showing sys id's.Please help me.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//function onChangeRequestedFor() {
var requestedForSysID = g_form.getValue('requested_for');

if (requestedForSysID) {
// Get the 'Requested For' user record
var grRequestedFor = new GlideRecord('sys_user');
if (grRequestedFor.get(requestedForSysID)) {
// Get the 'Requested For' user's email and department
var email = grRequestedFor.email;
var loc = grRequestedFor.location;
var mans = grRequestedFor.manager;

// Set the email and department values in the 'Reason' field
g_form.setValue('reason', 'Requested For Email: ' + email + 'Department: ' + loc + 'Manager: ' + mans);
}
}
}

4 REPLIES 4

Samaksh Wani
Giga Sage
Giga Sage

@Shaik22 

 

After line var grRequestedFor = new GlideRecord('sys_user');

Add this :-

grRequestedFor.addQuery('caller_id',requestedForSysID);

grRequestedFor.query();

 

Replace If statement line by this :-

 

if(grRequestedFor.next()){

 

It is not working.and i have a doubt should we use Glide Record methods in on Change client scripts?

 

Shaik22_1-1690210566547.pngShaik22_2-1690210610610.png

 

Ahmmed Ali
Mega Sage

Hello @Shaik22 

 

Simple solution would be to change the type of the fields to reference with their respective tables.

 

location referring to location table cmn_location

manager referring to user table sys_user

department referring to Department table cmn_department

 

Thank you,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Rahul Talreja
Mega Sage
Mega Sage

Hi @Shaik22 ,
You should not use Glide record in catlog client script.
alternate method is of script include and client script.

Script include client callable: 

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

	checkRecordPresent: function(){
		var obj = {};
		var id = this.getParameter('sysparm_userID');            
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', id); 
		gr.query();
		if(gr.next()){
			obj['email'] = gr.getDisplayValue('email');
			obj['manager'] = gr.getDisplayValue('manager');
			obj['location'] = gr.getDisplayValue('location');
		}
		return JSON.stringify(obj);
	},
	type: 'CheckRecords'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('CheckRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_userID', g_form.getValue('requested_for')); // give here your variable name
	ga.getXMLAnswer(function(answer){
		if(answer != 'not found'){
			var parser = JSON.parse(answer);
			g_form.setValue('reason', 'Requested For Email: ' + parser.email + 'Department: ' + parser.location + 'Manager: ' +parser.manager);
		}
	});
	//Type appropriate comment here, and begin script below

}



Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul