The CreatorCon Call for Content is officially open! Get started here.

g_form.getReference returns undefined in Client script if user has no ITIL permissions:

tkrishna29
Giga Guru

Hi,

below code works perfect and returns manager sysid if run for users with ITIL license. However, it's returning undefined for normal end users. Any ideas? Thanks.

I'm trying to run this on client script if user selects a requestor, his manager name should write to console for now.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
     return;
   }
  var usr = g_form.getReference('requestor',callBack);
  function callBack(usr){
     console.log ("hello2:" + usr.manager);
     g_form.setValue('requestor_manager',usr.manager);
   } 
}

 

1 ACCEPTED SOLUTION

tkrishna29
Giga Guru

@Allen : It worked on the portal for end users who have ITIL license and not for other users. They will always access this form through portal to request a service.

Hi,

I tried all the suggested combinations and it still didnt work. To make this work, I'm utilizing one of our existing script includes and called it using glideAjax. Surprisingly, this worked without tweaking any ACLs for the user record.

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
     return;
   }
//  var usr = g_form.getReference('requestor',callBack);
  var ga = new GlideAjax('getUserManager');
  ga.addParam("sysparm_name", "getonBehalfOfManager"); 
  ga.addParam("sysparm_user", g_form.getValue('requestor'));
  ga.getXML(getResponse);
function getResponse(response) {
  var values = response.responseXML.documentElement.getAttribute('answer').toString().split('||');
  g_form.setValue('requestor_manager', values[1]);
} 
} 
/*function callBack(usr){
	   console.log ("hello: " + usr.manager);
	   g_form.setValue('requestor_manager',usr.manager.toString());
   }     */

 

Here is my code on the script include:

var getUserManager = Class.create();
getUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getonBehalfOfManager : function(){
		
		var group = this.getParameter('sysparm_user');
		var rel = new GlideRecord("sys_user");
		rel.addQuery("sys_id", group);
		rel.query();
		
		var name_arr = [];
		var sys_id_arr = [];
		
		if(rel.next())
			{
			name_arr.push(rel.manager.name.toString());
			
			sys_id_arr.push(rel.manager.toString());
		}
		return name_arr.toString() + '||' + sys_id_arr.toString();
	},
	type: 'getUserManager'
});

 

Regards,

Krishna

View solution in original post

11 REPLIES 11

tkrishna29
Giga Guru

@Allen : It worked on the portal for end users who have ITIL license and not for other users. They will always access this form through portal to request a service.

Hi,

I tried all the suggested combinations and it still didnt work. To make this work, I'm utilizing one of our existing script includes and called it using glideAjax. Surprisingly, this worked without tweaking any ACLs for the user record.

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
     return;
   }
//  var usr = g_form.getReference('requestor',callBack);
  var ga = new GlideAjax('getUserManager');
  ga.addParam("sysparm_name", "getonBehalfOfManager"); 
  ga.addParam("sysparm_user", g_form.getValue('requestor'));
  ga.getXML(getResponse);
function getResponse(response) {
  var values = response.responseXML.documentElement.getAttribute('answer').toString().split('||');
  g_form.setValue('requestor_manager', values[1]);
} 
} 
/*function callBack(usr){
	   console.log ("hello: " + usr.manager);
	   g_form.setValue('requestor_manager',usr.manager.toString());
   }     */

 

Here is my code on the script include:

var getUserManager = Class.create();
getUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getonBehalfOfManager : function(){
		
		var group = this.getParameter('sysparm_user');
		var rel = new GlideRecord("sys_user");
		rel.addQuery("sys_id", group);
		rel.query();
		
		var name_arr = [];
		var sys_id_arr = [];
		
		if(rel.next())
			{
			name_arr.push(rel.manager.name.toString());
			
			sys_id_arr.push(rel.manager.toString());
		}
		return name_arr.toString() + '||' + sys_id_arr.toString();
	},
	type: 'getUserManager'
});

 

Regards,

Krishna

Great. Which is best practice (and not use getReference) and better for performance anyway.

Obviously there is other stuff going on in your instance for that code to not work (because it works for others)...but we'll leave that for another day as you've went with a Script Include.

Glad this issue is done.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!