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

Bert_c1
Kilo Patron

Try:

 

var ga = new GlideAjax('ABCD_RequestUtils');


 in your client script is you want to access the script include you have posted. as:

 

    var ga = new GlideAjax('script include');

 

won't do anything unless you have a script include named 'script include'.

sparkles
Tera Contributor

Sorry, its the same name but I changed it here (for privacy) without noticing I used different name

James Chun
Kilo Patron

Hi @sparkles,

 

Sorry, I am not sure what the issue is.

You have stated that Script to check if logged in user not the same as requested for as the name of the question but you are showing some scripts to modify the list of choice via AJAX.

 

Are you having an issue populating the choice list or something else?

 

Cheers

Hi James,

Thanks for your reply! The way the request works is the category display options based on the login user's cost center. What I want if the logged-in user one of the approver and wants to submit request for someone else, the category to display options based on the logged-in cost center and the "requested for" cost center.

I have a variable (hidden) for the approver

 

Thanks!