We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Want to Show and hide choice options from a variable in Catalog item

diptishade2
Tera Contributor

I am working on a requirement in which we have one catalog item and inside this we have a field named 'Please select the category of task completed '(select box type). This field contains options such as Account Driver Support ,Diary Management , Documents ,Email Management ,Events ,Expenses ,Finance Support,
New to firm SH onboarding , Project Management , Salesforce ,SH 121 meeting ,SH meeting/Management,
SH Tech Support ,Social Media Support ,Timesheets ,Travel. 

 

Now as per the requirement provided we have seven groups in total. Out of those 5 groups ( Tech A, Tech B, Tech C , Tech D , Tech E)who have full access to the variable stated above and other two groups(L1 and L2) can view limited options from the list above.

 

For this I have created a Script Include and an On-load Client script .

 

Script Include:

var SolhubGroupUtils = Class.create();
SolhubGroupUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isCurrentUserInGroup: function() {
        try {
            var groupSysId = this.getParameter('sysparm_group');
            if (!groupSysId)
                return 'false';

            var userSysId = gs.getUserID();
            if (!userSysId)
                return 'false';

            var gr = new GlideRecord('sys_user_grmember');
            gr.addQuery('user', userSysId);
            gr.addQuery('group', groupSysId);
            gr.setLimit(1);
            gr.query();

            return gr.hasNext() ? 'true' : 'false';
        } catch (e) {
            gs.error('SolhubGroupUtils.isCurrentUserInGroup error: ' + e);
            return 'false';
        }
    },

    type: 'SolhubGroupUtils'
});
 
 
And the On-Load Script :
 
function onLoad() {
    // --- CONFIGURATION ---
    var fullAccessGroups = [
        'd8e497043bcfba90e944d59a04e45abb',
        'ff189f4c3bcfba90e944d59a04e45a6a',
        '258d5b8c3b03fa90e944d59a04e45acf',
        '81792f803bc3fa90e944d59a04e45a44',
        '0d2ae7043bc3fa90e944d59a04e45af7'
    ];

    var restrictedGroups = [
        'd6afa2f52bc57a103b70fef56e91bfe1',
        'f06a9fd42bd9ae103b70fef56e91bf43'
    ];

    var changeTypeVar = 'please_select_a_category_of_task_completed'; // field name
    var fullList = [
        'account_driver', 'diary_management', 'documents', 'email_management',
        'events', 'expenses', 'finance_support', 'new_firm', 'project_management',
        'salesforce', 'sh_meeting', 'sh_management', 'tech_support', 'social_media',
        'timesheets', 'travel'
    ];

    var restrictedAllowed = [
        'travel', 'events', 'salesforce', 'tech_support',
        'documents', 'sh_management', 'sh_meeting', 'new_firm'
    ];

    // Combine all groups to check membership
    var allGroups = fullAccessGroups.concat(restrictedGroups);

    // --- AJAX CALL TO CHECK USER GROUPS ---
    var ga = new GlideAjax('SolhubGroupUtils');
    ga.addParam('sysparm_name', 'getUserMatchingGroups');
    ga.addParam('sysparm_groups', allGroups.join(','));
    ga.getXMLAnswer(function(answer) {
        if (!answer) {
            removeAllOptions();
            return;
        }

        var userGroups = answer.split(',');

        var hasFullAccess = userGroups.some(function(g) {
            return fullAccessGroups.indexOf(g) !== -1;
        });

        var hasRestrictedAccess = userGroups.some(function(g) {
            return restrictedGroups.indexOf(g) !== -1;
        });

        if (hasFullAccess) {
            // Full access: show all options, do nothing
            return;
        }

        if (hasRestrictedAccess) {
            // Restricted access: remove options not allowed
            fullList.forEach(function(option) {
                if (restrictedAllowed.indexOf(option) === -1) {
                    g_form.removeOption(changeTypeVar, option);
                }
            });
            return;
        }

        // No access: remove all options
        removeAllOptions();
    });

    // --- HELPER FUNCTION TO REMOVE ALL OPTIONS ---
    function removeAllOptions() {
        fullList.forEach(function(option) {
            g_form.removeOption(changeTypeVar, option);
        });
    }
}


But the script isn't working . Please help me out on this please at the earliest and please provide the correct code snippet to achieve the functionality stated above.

1 REPLY 1

Craig Gruwell
Mega Sage

Hi @diptishade2 ,

 

You are calling this:

 

var ga = new GlideAjax('SolhubGroupUtils');
ga.addParam('sysparm_name''getUserMatchingGroups');
ga.addParam('sysparm_groups', allGroups.join(','));
 
... but within your SolhubGroupUtils script include, I'm not seeing a function for getUserMatchingGroups.  Did you mean to use isCurrentUserInGroup instead?
 
Also, you refer to "sysparm_groups" in your GlideAjax call but in your script include you are referencing "sysparm_group"
 
The other issue I see is that you are passing in multiple group sys_ids in your GlideAjax call but within isCurrentUserInGroup, it is querying for a group against the entire collection 
 
gr.addQuery('group', groupSysId);
 
do you want this instead?
 
gr.addQuery('group', 'IN', groupSysId);