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

sparkles
Tera Contributor

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 

 

 

sparkles_0-1714334651932.png

 

The client script I have: 

function onChange(control, oldValue, newValue, isLoading) {

 

    var ga = new GlideAjax('ABCD');
    ga.addParam('sysparm_name''limitCategories');
    ga.addParam('sysparm_userid', newValue.toString());
    ga.getXML(limitCategories);

 

}

 

function limitCategories(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var categoriesObj = JSON.parse(answer);    
    g_form.clearOptions("category");
    g_form.addOption('category''''--Select--''');
    if (categoriesObj.showRisk == 'true')
        g_form.addOption('category''1''2'10);
    if (categoriesObj.showMarketing == 'true')
          g_form.addOption('category''1/ 2''3)'20);
     if (categoriesObj.showReporting == 'true')
        g_form.addOption('category''A'40);
    if (categoriesObj.showFinance == 'true')
        g_form.addOption('category''B''C'50);
    if (categoriesObj.showBilling == 'true')
        g_form.addOption('category''D''E'60);
 
====================================================================================
the script include
 

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;

 

    }

 

});

7 REPLIES 7

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

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)

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.