caller Role client scripts

saran4
Kilo Guru

How to find the role of the caller in incident form from client side

1 ACCEPTED SOLUTION

JC Icaro1
Kilo Guru

From the client-side, you will have access to the g_user object. The methods which might help you are:

- hasRole

- hasRoleExactly

 

Here's some links which might help you.

https://servicenowguru.com/scripting/user-object-cheat-sheet/

https://developer.servicenow.com/dev.do#!/reference/api/quebec/client/c_GlideUserAPI?navFilter=glideuser

 

Otherwise, you will need to do a GlideAjax call or use g_scratchpad to make it available from the server-side to the client-side.

View solution in original post

11 REPLIES 11

Here are some alternative scripts that better adhere to standards and best practices than the scripts provided in a previous response.

// Script Include
// class names should be nouns and expressed in upper camel case
var UserRole = Class.create();

UserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  // function names should be verbs and expressed in lower camel case
  getUserRoles: function () {
    var callerId = this.getParameter('sysparm_caller_id');

    var gr = new GlideRecord('sys_user_has_role');

    // parameters passed into an Ajax call are already strings, so toString() is not required
    gr.addQuery('user', callerId);
    gr.query();

    var roleIds = [];

    while (gr.next()) {
      // getValue() returns a string in this case, so toString() is not required
      roleIds.push(gr.getValue('role'));
    }

    // call join() on an array rather than toString()
    return roleIds.join(',');
  },

  type: 'UserRole'
});

// Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (!isLoading) {
    if (newValue) {
      var ga = new GlideAjax('UserRole');

      ga.addParam('sysparm_name', 'getUserRoles');
      ga.addParam('sysparm_caller_id', newValue);

      // use getXMLAnswer here rather than getXML so you do not have to deal with XML directly
      // response here is a string containing the role sys_ids from the script include
      ga.getXMLAnswer(function (response) {
        if (response) {
          alert(response);
        }
      });
    }
  }
}

JC Icaro1
Kilo Guru

From the client-side, you will have access to the g_user object. The methods which might help you are:

- hasRole

- hasRoleExactly

 

Here's some links which might help you.

https://servicenowguru.com/scripting/user-object-cheat-sheet/

https://developer.servicenow.com/dev.do#!/reference/api/quebec/client/c_GlideUserAPI?navFilter=glideuser

 

Otherwise, you will need to do a GlideAjax call or use g_scratchpad to make it available from the server-side to the client-side.

g_user only gives the role of the currently logged in user. GlideAjax would be required to get information about the user set as the Caller.

And g_scratchpad would be wonderful here, but I assume the role information would need to be available if the Caller value were to change as well, which would then still require a GlideAjax call.

shloke04
Kilo Patron

Hi @Saran 

If you want to get the Name of Roles of the caller on Incident form, then you will need a combination of Client Script and Script Include(Glide Ajax) as shown below:

1) Now the question is when do you want to fetch it , in an On load of the form or On Change of the caller on the form?

2) Second question what do you want to do after fetching the Role of Caller on the Incident form?

Below approach you can follow to get the details like I have done this in an On Change of Caller on Incident form as below:

A) Create a Client Callable Script Include and use the script as below:

Script Include:

var getRoles = Class.create();
getRoles.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	fetchRole : function(){
		var arr = [];
		var getCaller = this.getParameter('sysparm_caller');
		var gr = new GlideRecord('sys_user_has_role');
		gr.addQuery('user',getCaller.toString());
		gr.query();
		while(gr.next()){
			arr.push(gr.getDisplayValue('role').toString());
		}
		return arr.toString();
	},

    type: 'getRoles'
});

find_real_file.png

2) Now create a Client Script, you can use the same code in On Load or On Change script as well:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (newValue) {
        var ga = new GlideAjax('getRoles');
        ga.addParam('sysparm_name', 'fetchRole');
        ga.addParam('sysparm_caller', newValue);
        ga.getXML(getRoles);
    }

    function getRoles(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
    }

    //Type appropriate comment here, and begin script below

}

find_real_file.png

Result:

find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

thank you