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

Community Alums
Not applicable

Can you explain more please with screenshots..your question is not clear

Jaspal Singh
Mega Patron
Mega Patron

Kindly share what you have started with & tried & where you are stuck.

The logic of Script include is what is to be followed but you can also try below.

1. Drop down (Lookup selectbox) field of catalog items to reference User Role table

2. You can pass qualifier as

javascript: 'user='+current.variables.variable_name;

//replace variable_name with user variable from Catalog item

find_real_file.png

This is okay but we have to achieve via script include and client script because we have to achieve some more customizations.

Hi jaspal,

I have tried below script include and client script but it is not working, Please correct it as per my requirement.

script include:

getStates : function() {

  var list = [];

  var userValue = this.getParameter('sysparm_Val');

 var map = new GlideRecord(sys_user_has_role);

  map.addQuery('role', userValue);

map.query();

  if (map.next()) {

var eObj={};

eObj= map.getValue('role');

  list.push(eObj);

}

}

 return JSON.stringify(list);

},

 

client script :

  var userVal = g_form.getValue('req');

  var ga = new GlideAjax('useRrole');

  ga.addParam('sysparm_name', 'getStates');

  ga.addParam('sysparm_Val', userVal);

  ga.getXMLAnswer(stateCallback);

  function stateCallback(answer) {

  alert('answer=' + answer);

  var objList = JSON.parse(answer);

  var selected = 0;

  g_form.clearOptions('role');

  for (var i = 0; i < objList.length; i++) {

  var obj = objList[i];

  g_form.addOption('role', obj.value, obj.label);

  if (obj.selected) {

  selected = i;

  }

  }

  g_form.setValue('role', objList[selected].value, objList[selected].label);

  }