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

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

Hi @uma 

 

Any update on your issue?

Did my solution work for you?

If yes and if my answer helped you out then please mark my answer as correct/helpful, so that other community members facing similar issues might get help.

Thanks and regards,

Kartik