Script to check if logged in user not the same as requested for

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 01:11 PM - edited 04-28-2024 01:18 PM
Hi,
I want script to check if "logged-in user" is not the same as requested for user then display the category that "logged-in user" eligible for and the "requested for" as well. Currently it display whatever is the "requested for" is eligible for
The client script I have:
var ABCD_RequestUtils= Class.create();
ABCD_RequestUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Getting Group name by send group type
getGroupsByCategory: function(category) {
var groupTypeName = '';
var groupTypeObj = new GlideRecord("sys_user_group_type");
groupTypeObj.addQuery("name", category);
groupTypeObj.query();
if (groupTypeObj.next()) {
groupTypeName = groupTypeObj.sys_id.toString();
}
var groupArr = [];
var group = new GlideRecord("sys_user_group");
group.addQuery("type", groupTypeName);
group.query();
while (group.next()) {
groupArr.push(group.sys_id.toString());
}
return groupArr;
},
getuserdetailsbyuserid: function() {
var userdetail;
var access = false;
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", gs.getUserID());
user.query();
if (user.next()) {
userdetail = user.title;
}
if ((userdetail.toString().indexOf('Manager') >= 0) || (userdetail.toString().indexOf('Director') >= 0) || (userdetail.toString().indexOf('Partner') >= 0)) {
access = true;
}
//Checking for non-management/designated folks who can view the request form
var designatedUsers = gs.getProperty('req_designated_users');
if (designatedUsers.indexOf(gs.getUserName()) != -1)
access = true;
return access;
},
// Getting Approver name by send group
getGroupByDetails: function() {
var groupDetails = {};
groupDetails.approver = '';
groupDetails.klw = 'false';
//var details = [];
var groupname = this.getParameter('sysparm_group');
var group = new GlideRecord("sys_user_group");
group.addQuery("sys_id", groupname);
group.query();
if (group.next()) {
groupDetails.approver = group.u_owner.toString();
if ((group.type.getDisplayValue().toString().indexOf('test1') >= 0)) {
groupDetails.klw = 'true';
}
}
return JSON.stringify(groupDetails);
},
limitCategories: function() {
var userCostCenter = '';
var categoryObj = {};
var userId = this.getParameter('sysparm_userid');
var user = new GlideRecord("sys_user");
user.addQuery("sys_id",userId);
user.query();
if (user.next()) {
userCostCenter = user.department.id.toString();
}
//Creating a flag for each category (Rik, Mrk, etc...)
categoryObj.showRik = this._user_from_rik_cost_centre(userCostCenter);
categoryObj.showMrk = 'true';
categoryObj.showEng = 'true';
categoryObj.showRept = 'true';
categoryObj.showFin = this._user_from_finance_cost_centre(userCostCenter);
categoryObj.showBilling = this._user_from_billing_cost_centre(userCostCenter);
return JSON.stringify(categoryObj);
},
_user_from_rik_cost_centre: function(userCostCenter) {
var result = 'false';
if (userCostCenter == '12345')
result = 'true';
return result;
},
_user_from_finance_cost_centre: function(userCostCenter) {
var result = 'false';
var financeCostCenterList = '28110123,28110125,28110126,28110127,28110129,28110130,28110131,28110132,28110133,28110134,28110135,28110393,28110396';
if (financeCostCenterList.split(',').indexOf(userCostCenter) != -1)
result = 'true';
return result;
},
_user_from_billing_cost_centre: function(userCostCenter) {
var result = 'false';
var billingCostCenterList = '28110353,28110354,28110355,28110356';
if (billingCostCenterList.split(',').indexOf(userCostCenter) != -1)
result = 'true';
return result;
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 12:52 PM
Hi @sparkles,
If I understood it correctly, the script is populating the Category variable depending on the requested for's department. This is all working fine but you want to limit it so that only the approvers can request on other's behalf.
If this is the case, can't you just create a catalog UI Policy to make the requested for variable as read-only if the current user is not an approver?
Also, what did you mean by the 'hidden' variable? What does this do?
Cheers

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 08:57 AM
Hi James,
Currently the category display option based on the "requested for" department. But there are cases where the approver can submit for another person, in this case I want the approver to see all categories.
I have a variable that holds all approver names (we have 5 approvers)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 01:20 PM
Hi @sparkles,
Something like the below then?
function onChange(control, oldValue, newValue, isLoading) {
var userId = g_user.userID; //get current user's sys_id
//Check if the current user is an approver and is requesting on behalf
if (newValue != userId && g_form.getValue('your_hidden_variable').indexOf(userId) > -1) {
//Add all options in the variable
} else {
var ga = new GlideAjax('ABCD');
ga.addParam('sysparm_name', 'limitCategories');
ga.addParam('sysparm_userid', newValue.toString());
ga.getXML(limitCategories);
}
}
Note that I am assuming that the hidden variable is a list type with reference to the user table.