Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Message stating whether user has ITIL Role

Philip Conforzi
Tera Contributor

Hello ServiceNow Community,

 

I hope you're doing well.

 

I am working on a feature that displays a specific message at the top of the user record when it is accessed. My goal is to implement this functionality within the sys_user table. When a user record is selected from the list and the user form opens, I want a notification to appear at the top, indicating whether the user has ITIL privileges.

 

However, I'm encountering a challenge: despite my efforts, I am unable to get the system to show a confirmation message for users who are designated as ITIL users. For better illustration, I will provide screenshots of the issue, along with the code I've been using, attached below.

 

I used a script include and client script for this.

 

Your thoughts and feedback would be most appreciated.

 

 

1 ACCEPTED SOLUTION

Kai Tingey
Tera Guru

Hi Philip

 

You need that script include to be client callable, and a few little changes to your code to pass through the values. Tick the "client callable" box on your ajax, and try this code instead:

 

ajax script:

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

	hasITILRole:function(){

	var user = this.getParameter('userID'); //pass in user sys_id value from client script

	var userRoleGR = new GlideRecord('sys_user_has_role');
	userRoleGR.addQuery('user',user);
	userRoleGR.addQuery('role','282bf1fac6112285017366cb5f867469') //sys_id of itil role
	userRoleGR.query();

	if (userRoleGR.next()){
		return true;
	}
	}

  //type: 'CheckUserITILRole'

});

 

client script:

function onLoad() {
 
//get user sys_id value
 var user_ID = g_form.getUniqueValue();

//time for some AJAX
var ga = new GlideAjax('CheckUserITILRole'); 
    ga.addParam('sysparm_name', 'hasITILRole'); //ajax function
    ga.addParam('userID',user_ID); //pass sys_id into ajax

	ga.getXML(myCallBack); //process response

function myCallBack(response) { 
   //Dig out the 'answer' attribute
	var ajaxcomplete = response.responseXML.documentElement.getAttribute('answer'); 
	//alert(ajaxcomplete); //alert the result to make sure all is well.
   
	if (ajaxcomplete == 'true'){
		g_form.addInfoMessage("This user has the ITIL role");
	}
   
   }
  
}

 

if that helps or answers your question, please click the appropriate button 🙂

View solution in original post

2 REPLIES 2

Kai Tingey
Tera Guru

Hi Philip

 

You need that script include to be client callable, and a few little changes to your code to pass through the values. Tick the "client callable" box on your ajax, and try this code instead:

 

ajax script:

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

	hasITILRole:function(){

	var user = this.getParameter('userID'); //pass in user sys_id value from client script

	var userRoleGR = new GlideRecord('sys_user_has_role');
	userRoleGR.addQuery('user',user);
	userRoleGR.addQuery('role','282bf1fac6112285017366cb5f867469') //sys_id of itil role
	userRoleGR.query();

	if (userRoleGR.next()){
		return true;
	}
	}

  //type: 'CheckUserITILRole'

});

 

client script:

function onLoad() {
 
//get user sys_id value
 var user_ID = g_form.getUniqueValue();

//time for some AJAX
var ga = new GlideAjax('CheckUserITILRole'); 
    ga.addParam('sysparm_name', 'hasITILRole'); //ajax function
    ga.addParam('userID',user_ID); //pass sys_id into ajax

	ga.getXML(myCallBack); //process response

function myCallBack(response) { 
   //Dig out the 'answer' attribute
	var ajaxcomplete = response.responseXML.documentElement.getAttribute('answer'); 
	//alert(ajaxcomplete); //alert the result to make sure all is well.
   
	if (ajaxcomplete == 'true'){
		g_form.addInfoMessage("This user has the ITIL role");
	}
   
   }
  
}

 

if that helps or answers your question, please click the appropriate button 🙂

Thanks this works great!