User Criteria

Marcelo Pedrey1
Tera Contributor

Hello,

 

Brief background: We support multiple companies within a single SNOW instance, and most of the companies have their individual catalog items. For that, we utilize "User Criteria".  We also have an L1 desk that handles all of the companies, and this group has access to all of the catalog items in our instance regardless of the company.  We are having issues when they open a request for Company A for Computer A that is only available for Company B.  Is there a way to restrict the creation of a request using the "requested for" user's company name to prohibit that request from being submitted?

 

Thanks in advance

5 REPLIES 5

I have created the following business rule and a script include, but it does not seem to do what I would like it to. Does anyone have any suggestions?  

 

Business Rule

(function executeRule(current, previous /* Previous is not used here */) {
    var requestedForCompany = current.request.requested_for.company.getDisplayValue(); // Assuming requested_for is a reference field to the user and company is a field on the user table

    // Query the user criteria table to get the available_for value for the catalog item
    var userCriteria = new GlideRecord('sc_cat_item_user_criteria_mtom');
    userCriteria.addQuery('sc_cat_item', current.cat_item); // Assuming cat_item is the reference field to the catalog item on the request item
    userCriteria.query();
    var availableFor = '';
    while (userCriteria.next()) {
        availableFor = userCriteria.company.getDisplayValue(); // Assuming company is the field name for the company in the user criteria table
        break; // Assuming there is only one matching record
    }

    // Validate if the requested_for company aligns with the available_for value
    if (requestedForCompany !== availableFor) {
        current.setAbortAction(true); // Prevents submission of the request item
        current.setAbortError('This catalog item is not available for the requestors PortCo. Please reference the PortCo Catalog Item Matrix dashboard.'); // Displays an error message to the user
    }
})(current, previous);

 

Script Include

var PortCoValidation = Class.create();
PortCoValidation.prototype = Object.extend(new AbstractAjaxProcessor(), {

    validateUser: function() {
        var userSysId = this.getParameter('sys_id');
        var catalogItemSysId = this.getParameter('catalog_item_sys_id');

        var response = {
            isValid: false,
            message: 'This catalog item is not available for the requestors PortCo. Please refer to the PortCo Catalog Item Matrix dashboard.'
        };

        // Get user's company
        var grUser = new GlideRecord('sys_user');
        if (grUser.get(userSysId)) {
            var userCompany = grUser.getDisplayValue('company');
            gs.info('User Company: ' + userCompany);

            // Get catalog item's available_for PortCo from the sc_cat_item_user_criteria_mtom table
            var grCriteria = new GlideRecord('sc_cat_item_user_criteria_mtom');
            grCriteria.addQuery('sc_cat_item', catalogItemSysId);
            grCriteria.query();
            var portCoList = [];
            while (grCriteria.next()) {
                portCoList.push(grCriteria.getValue('user_criteria'));
            }
            gs.info('PortCo List: ' + portCoList.join(','));

            /// PortCo to Company mapping
                var portCoCompanyMapping = {
                    'PortCo: aaa': ['aaa'],
					'PortCo: bbb': ['bbb'],
					'PortCo: ccc': ['ccc'],
					'PortCo: ddd': ['ddd'],
				// Add more mappings as needed
                };

            // Validate if the user's company is in the allowed list for any of the PortCos
            for (var i = 0; i < portCoList.length; i++) {
                var portCo = portCoList[i];
                if (portCoCompanyMapping[portCo] && portCoCompanyMapping[portCo].indexOf(userCompany) >= 0) {
                    response.isValid = true;
                    response.message = 'User is valid';
                    break;
                }
            }
        }

        return JSON.stringify(response);
    }

});