How to achieve list of roles of a user?

uma7
Kilo Guru

Hi all,

How to get list of roles belong to the user in drop down in catalog item?

Please suggest me the script include and catalog client script to achieve this requirement.

Thanks

 

1 ACCEPTED SOLUTION

Kartik Sethi
Tera Guru
Tera Guru

Hi @uma 

 

Please update your scripts as provided below:

Script Include: Just to remind

  • Client Callable: true
  • Script:
    var yourScriptIncludeName = Class.create();
    yourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    			getStates: function() {
    				var list = [];
    				var userValue = this.getParameter('sysparm_val');
    				//Issue1: Table name was not in quotes
    				var map = new GlideRecord('sys_user_has_role');
    				map.addQuery('user.sys_id', userValue);
    				map.query();
    				while (map.next()) {
    					var eObj = {};
    					eObj.name = map.role.name + '';
    					eObj.sys_id = map.getValue('role');
    				list.push(eObj);
    			}
    		}
    		return JSON.stringify(list);
    	},
    	type: 'yourScriptIncludeName'
    });

     

 

Client Script:

  • Type: On Change
  • Field/Variable: Field corresponding to req
  • Script:
    function onChange(control, oldValue, newValue, isLoading, isTemplate) {
       if (isLoading || newValue === '') {
          return;
       }
       
       //GlideAjax Call to fetch Roles for Requestor
       var ga = new GlideAjax('yourScriptIncludeName');
       ga.addParam('sysparm_name', 'getStates');
       ga.addParam('sysparm_val', newValue);
       ga.getXMLAnswer(stateCallback);
    }
    
    function stateCallback(answer) {
    	alert('answer=' + answer);
    	var i = 1;
    	var objList = JSON.parse(answer);
    	g_form.clearOptions('role');
    	for (var keys in objList) {
    		g_form.addOption('role', objList.sys_id, objList.name);
    		i++;
    	}
    	
    	//Select the Last Option
    	g_form.setValue('role', objList[i].sys_id, objList[i].name);
    }

Remark: Ensure the variable/field is of Type Choice.


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

View solution in original post

15 REPLIES 15

Kartik Sethi
Tera Guru
Tera Guru

Hi @uma 

 

Please update your scripts as provided below:

Script Include: Just to remind

  • Client Callable: true
  • Script:
    var yourScriptIncludeName = Class.create();
    yourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    			getStates: function() {
    				var list = [];
    				var userValue = this.getParameter('sysparm_val');
    				//Issue1: Table name was not in quotes
    				var map = new GlideRecord('sys_user_has_role');
    				map.addQuery('user.sys_id', userValue);
    				map.query();
    				while (map.next()) {
    					var eObj = {};
    					eObj.name = map.role.name + '';
    					eObj.sys_id = map.getValue('role');
    				list.push(eObj);
    			}
    		}
    		return JSON.stringify(list);
    	},
    	type: 'yourScriptIncludeName'
    });

     

 

Client Script:

  • Type: On Change
  • Field/Variable: Field corresponding to req
  • Script:
    function onChange(control, oldValue, newValue, isLoading, isTemplate) {
       if (isLoading || newValue === '') {
          return;
       }
       
       //GlideAjax Call to fetch Roles for Requestor
       var ga = new GlideAjax('yourScriptIncludeName');
       ga.addParam('sysparm_name', 'getStates');
       ga.addParam('sysparm_val', newValue);
       ga.getXMLAnswer(stateCallback);
    }
    
    function stateCallback(answer) {
    	alert('answer=' + answer);
    	var i = 1;
    	var objList = JSON.parse(answer);
    	g_form.clearOptions('role');
    	for (var keys in objList) {
    		g_form.addOption('role', objList.sys_id, objList.name);
    		i++;
    	}
    	
    	//Select the Last Option
    	g_form.setValue('role', objList[i].sys_id, objList[i].name);
    }

Remark: Ensure the variable/field is of Type Choice.


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Hi,

I am getting below error, please correct it

find_real_file.png

Hi @uma 

 

Did you try to copy the whole function (in Script Include) and Client Script as I have changed the parameter "sysparm_val" and did you also ensure that:

  • Script Include is Client Callable
  • Client Script is onChange and it is on Field/Variable corresponding to Requested For

Please check these and let me know if this solves your issue.

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Hi,

 I am getting roles as alert at the top, what's wrong in my code, please correct it. above code only i used

find_real_file.png

Hi @uma 

In the response, you are receiving an array of objects so we need to adapt our client script, so please find the updated Client Script:

Client Script:

  • Type: On Change
  • Field/Variable: Field corresponding to req
  • Script:
    function onChange(control, oldValue, newValue, isLoading, isTemplate) {
       if (isLoading || newValue === '') {
    	   
    	   //If form is loading or Requester is empty then
    	   //Clear the options
    	   g_form.clearOptions('role');
          return;
       }
       
       //Clear options to rebuild the list on change of Requester
       g_form.clearOptions('role');
       
       //GlideAjax Call to fetch Roles for Requestor
       var ga = new GlideAjax('yourScriptIncludeName');
       ga.addParam('sysparm_name', 'getStates');
       ga.addParam('sysparm_val', newValue);
       ga.getXMLAnswer(stateCallback);
    }
    
    function stateCallback(answer) {
    	alert('answer=' + answer);
    	if(answer) {
    		for(var i = 0; i < answer.length; i++) {
    			var objList = JSON.parse(answer[i]);
    			g_form.addOption('role', objList.sysID, objList.name);
    			
    			//Select the Last Option
    			if(i == answer.length - 1) {
    				g_form.setValue('role', objList.sys_id, objList.name);
    			}
    		}
    	}
    }

Remark: Ensure the variable/field is of Type Choice.

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik