CSM cases with different channel NEED TO BE RESTRICTED for different account

Patiljuned
Tera Contributor

Below mentioned requirement:
Control what type on Cases a client can see. Ex: Alert cases- some clients need to see the Alert Cases for other clients we do not want them to see Alerts.

The Idea:
A field on the Account table ( Case View Restriction) will need to be created. This field would be a dropdown(multi Select) with the list that matches the Channel field that is on the cases. the field on the Account table would control what cases a client would see based on what the Channel field on the Case is set to.
Default value is all existing accounts can see (ALL) cases. Managers will update the field ( Case View Restriction) to restrict what should not be able to see.
Field Name = Case View Restriction . We would like to see this on the form/list view
Channel fields:
Web
Phone
Email
Chat
social
community
Alert
Virtual Agent
eBonding
Project

What i did for this- 
created list type field on the account table 'case view restriction'
under reference selected choice table
filter condition Table - case table
element - contact_type (channel) 

 

In this way i have used same choices of channel on the case table to newly added field case view restriction (list field)

 

created  business rule on case table before query but not working ....

    var channel = current.contact_type;

 

    // Check if there's an associated account

    if (current.account) {

        // Query the associated account to get the "Case view restriction" field value

        var account = new GlideRecord('Customer_account');

        if (account.get(current.account)) {

            // Check if the current contact matches the contact on the account

            if (current.contact == account.contact) {

                // Check the value of the "Case view restriction" field on the associated account

                var accountRestriction = account.u_case_view_restriction;

 

                // Check if the selected channels match the restriction

                if (isChannelRestricted(channel, accountRestriction)) {

                    current.can_read = false; // Restrict access

                } else {

                    current.can_read = true; // Grant access for other channels

                }

            }

        }

    }

 

    function isChannelRestricted(caseChannel, accountRestriction) {

        // Check if the selected channels match the restriction

        var restrictedChannels = accountRestriction.split(','); // Assuming values are comma-separated

        return restrictedChannels.includes(caseChannel);

    }

 

Let me know what could be reason or any other solution 

It is for CSM PORTAL case view restriction 

 

Thanks in Advance !

 

 

2 REPLIES 2

Patiljuned
Tera Contributor

created script include still not working 

 

var CaseViewFilterUtils = Class.create();

 

CaseViewFilterUtils.prototype = {

    initialize: function() {},

 

    doesNotHaveValidCaseViewRestriction: function(currentCase) {

        // Assuming 'account' is the reference field on the Case table pointing to the Account table

        var account = currentCase.account;

 

        // Check if the account reference is valid and has a non-empty u_case_view_restriction field

        if (account && account.u_case_view_restriction) {

            // Get the selected channels from the account's u_case_view_restriction field

            var selectedChannels = account.u_case_view_restriction.split(',');

 

            // Check if the case's contact type is NOT in the selectedChannels array.

            return !selectedChannels.includes(currentCase.contact.contact_type);

        }

 

        // No restriction, user should see the case.

        return true;

    }

};

 

 

 

any suggestions how to call this in csm portal widget filter

customer service portal filter.PNG

Portal filter.PNG

  

i have created below script include 

// Script Include: CaseVisibilityUtils

var CaseVisibilityUtils = Class.create();
CaseVisibilityUtils.prototype = {

    // Function to filter case sys IDs based on u_case_view_restriction field
    getFilteredCaseSysIds: function(accountGR) {

        // Step 1: Get the sys ID of the currently logged-in user
        var currentUser = gs.getUser();
        if (!currentUser) {
            // If user is not found, return an empty array
            return 'sys_id=12345';
        }

        // Step 2: Get the sys ID of the related account
        var contact = currentUser.getPreference('contact');
        if (!contact) {
            // If contact is not found, return an empty array
            return 'sys_id=12345';
        }

        // Step 3: Get u_case_view_restriction values from the associated Customer Account
        var caseViewRestriction = accountGR.u_case_view_restriction;

        // Step 4: Check if the caseViewRestriction is not empty
        if (caseViewRestriction) {

            // Step 5: Split the comma-separated values into an array
            var choices = caseViewRestriction.split(',');
            var validSysIds = [];

            // Step 6: Loop through each choice
            for (var i = 0; i < choices.length; i++) {
                // Step 7: Get a choice and remove leading and trailing white spaces using trim()
                var choice = choices[i].trim();

                // Step 8: Check if choice is not empty
                if (choice) {

                    // Step 9: Query cases based on existing filter conditions
                    var caseGR = new GlideRecord('sn_customerservice_case');
                    caseGR.addQuery('sys_id', choice);
                    caseGR.addQuery('active', true);

                    // Step 10: Order by sys_updated_on in descending order
                    caseGR.orderByDesc('sys_updated_on');
                    caseGR.setLimit(10);
                    caseGR.query();

                    // Step 11: If the case matches the filter conditions, add its sys_id to the array
                    if (caseGR.next()) {
                        validSysIds.push(choice);
                    }
                }
            }

            // Step 12: Return the array of sys_ids formatted for GlideRecord query
            return 'sys_idIN' + validSysIds.toString();
        }

        // Step 13: Return an empty array if no valid choices
        return 'sys_id=12345';
    },

    type: 'CaseVisibilityUtils'
};


and called in portal filter with 

javascript&colon;CaseVisibilityUtils().getFilteredCaseSysIds(current.customer_account)


but still not working....anybody servicenow expert help me out in this
portal filter script.PNG