Based on the requested for role(itil) hide the field in order guide
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 01:21 AM
Hi All,
Based on the selected requested_for user contain role (itil) hide field in order guide.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var hasRole = g_user.hasRole('itil');
if (hasRole == true) {
g_form.setVisible('esm', true);
} else{
g_form.setVisible('esm', false);
}
}
is not working any suggestion to achieve the requirement.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 01:54 AM
Hello @Community Alums,
I think "hasRole('whatever')" always returns true if you are the admin, no wonder if the role really exists or not.
Instead use g_user.hasRoleExactly('itil') which will resolve your issue.
Hope it helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 02:04 AM
Hello @Community Alums
Please check this link-
Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 02:14 AM
Hi @Community Alums
You said you need to do it based on the selected Requested for user role, but you are using g_form.hasRole(), this will check for the roles of the user who is filling the form. To Achieve this requirement you need to use ClientCallable Script Include and Glide Ajax in your onChange client script.
I'm assuming that you wrote your onChange catalog client script on the requested for variable.
Client Callable Script Include: Navigate to System Definition -> Script Includes and create a new record like the one below.
var CustomUserUtilsClient = Class.create();
CustomUserUtilsClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkUserHasITIL: function(){
var user = this.getParameter("sysparm_user");
var roleGr = new GlideRecord("sys_user_has_role");
roleGr.addEncodedQuery("role.name=itil^user=" + user);
roleGr.query();
if(roleGr._next()){
return 'true';
}
return 'false';
},
type: 'CustomUserUtilsClient'
});
onChange Catalog Client Script: Change your catalog client script to the one below.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("CustomUserUtilsClient");
ga.addParam("sysparm_name", "checkUserHasITIL");
ga.addParam("sysparm_user", newValue);
ga.getXMLAnswer(processResp);
function processResp(hasRole){
if (hasRole == 'true') {
g_form.setVisible('esm', false);
} else{
g_form.setVisible('esm', true);
}
}
}
If your onChange client script is not on Requested for user field, use the following script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getValue("YOUR_REQUESTED_FOR_VARIABLE_NAME");
var ga = new GlideAjax("CustomUserUtilsClient");
ga.addParam("sysparm_name", "checkUserHasITIL");
ga.addParam("sysparm_user", newValue);
ga.getXMLAnswer(processResp);
function processResp(hasRole){
if (hasRole == 'true') {
g_form.setVisible('esm', false);
} else{
g_form.setVisible('esm', true);
}
}
}
If you want this to work on onLoad also, create a new onLoad client script with the following code.
function onLoad() {
var user = g_form.getValue("YOUR_REQUESTED_FOR_VARIABLE_NAME");
var ga = new GlideAjax("CustomUserUtilsClient");
ga.addParam("sysparm_name", "checkUserHasITIL");
ga.addParam("sysparm_user", newValue);
ga.getXMLAnswer(processResp);
function processResp(hasRole){
if (hasRole == 'true') {
g_form.setVisible('esm', false);
} else{
g_form.setVisible('esm', true);
}
}
}
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 03:08 AM
Dear @Community Alums
Check the below code it might help you