Script Include - function to return Category Reference Qualifier in Record Producer

kandulek
Tera Contributor

Hi all,

 

We use the following Script Include that is used to return reference qualifier for Categorization field (We have a customized table to store Categories). On the Category form there is a glide-list field called "Show for these countries" (u_location). Currently, when there are any countries listed there, then this category is visible only for users from those countries. If there are no countries listed in that field - then the visibility is global. The new requirement is for countries specified in Service Offering to take lead. Service Offering is a reference field, it has a related list called "Available for Country" (service_subscribe_location).

 

The new requirement is to always show the category if user's country matches the country specified in the Service Offering, no matter if this country is listed or not in "Show for these countries" (u_location) field.

However, if there is a country listed in "Show for these countries" (u_location) field, but this country is not listed in the related Service Offering, then this category should not be visible for the user from that country.

 

kandulek_0-1749229342104.png

This is the Script Include we use:

    /**
     * Function: getBackendCategoryRefQualRP
     * Purpose: function that is used to return reference qualifier for Categorization field
     * Input parameter: User's SysId, User's Domain, User's Country code
     * Output: Encoded Query that is used to Category visibilty as per User criteria
     */

 getBackendCategoryRefQualRP: function(sUserID, sDomains, sCountryCode) {
        try {
            var sEncodedQuery = "";
            // Added Encoded query as per User's Domain
            if (JSUtil.nil(sDomains))
                sDomains = this.sDefaultDomain; //Assign Default Domain
 
            if (sDomains.indexOf(this.sGlobalDomain) == -1)
                sDomains += "," + this.sGlobalDomain; //Every User can access Data in Global Domain. sDomains should have at least one domain at this stage
 
            var aDomains = sDomains.split(",");
            sEncodedQuery = "u_domainsLIKE" + aDomains.join("^ORu_domainsLIKE");

            // Added Encoded query as per  User's Country
            if (JSUtil.notNil(sCountryCode)) {
                var sCountryID = this.getUserCountry(sCountryCode);
                if (JSUtil.notNil(sCountryID)) {
                    sEncodedQuery = sEncodedQuery + "^u_locationLIKE" + sCountryID + "^ORu_locationISEMPTY" +
                        "^u_parent.u_locationLIKE" + sCountryID + "^ORu_parent.u_locationISEMPTY" +
                        "^u_hide_locationsNOT LIKE" + sCountryID + "^ORu_hide_locationsISEMPTY" +
                        "^u_parent.u_hide_locationsNOT LIKE" + sCountryID + "^ORu_parent.u_hide_locationsISEMPTY"; // Show all categories where Location/hide Locations is empty
                } else
                    sEncodedQuery = sEncodedQuery + "^u_locationISEMPTY^u_hide_locationsISEMPTY^u_parent.u_locationsISEMPTY^u_parent.u_hide_locationsISEMPTY"; // Show all categories where Location/hide Locations is empty    
            }
            return this.queryFilterValidatedWithServiceOfferings(sEncodedQuery, sCountryID);
 
        } catch (oErrMsg) {
            new GSLog().logError("Script Include: CategoryUtils. Function: getBackendCategoryRefQualRP. Error message received from Try/Catch: " + oErrMsg);
        }
    },
 
    /**
     * Function: queryFilterValidatedWithServiceOfferings
     * Purpose: filter out service offerings that are not available for a given country (sCountryID)
     * Input parameter: encodedQuery, sCountryID
     * Output:Returns an updated encoded query (String) that excludes unavailable service offerings for the given country
     */
 
    queryFilterValidatedWithServiceOfferings: function(encodedQuery, sCountryID) {
        try {
            var aServiceOfferingIds = [];
            var oGRCat = new GlideRecord("u_category");
            oGRCat.addNotNullQuery("u_service_offering");
            oGRCat.addEncodedQuery(encodedQuery);
            oGRCat.query();
            while (oGRCat.next()) {
                aServiceOfferingIds.push(oGRCat.getValue("u_service_offering"));
            }
 
            var aNotAvailableSO = this.isServiceOfferingSubscribedOrEmpty(aServiceOfferingIds, sCountryID);
 
            if (JSUtil.notNil(aNotAvailableSO)) {
                encodedQuery += "^u_service_offeringNOT IN" + aNotAvailableSO;
            }
            return encodedQuery;
        } catch (oErrMsg) {
            new GSLog().logError("Script Include: CategoryUtils. Function: queryFilterValidatedWithServiceOfferings. Error message received from Try/Catch: " + oErrMsg);
        }
    },
 
    /**
     * Function: isServiceOfferingSubscribedOrEmpty
     * Purpose: determines which SO from a given list are not available in sCountryID
     * Input parameter: aServiceOfferingIds, sCountryID
     * Output:Returns list of service offering IDs that are NOT available in sCountryID
     */
 
    isServiceOfferingSubscribedOrEmpty: function(aServiceOfferingIds, sCountryID) {
        try {
            var oSOwithCountry = [];
            var oSOOnly = [];
            var oGRServiceLocation = new GlideRecord("service_subscribe_location");
            oGRServiceLocation.addEncodedQuery("service_offeringIN" + aServiceOfferingIds +
                "^cmn_location.u_active=true^cmn_location.u_type!=SAL");
            oGRServiceLocation.query();
            var sSOid;
            while (oGRServiceLocation.next()) {
                var oTmp = {};
                sSOid = oGRServiceLocation.getValue("service_offering");
                oTmp.so = sSOid;
                oTmp.country = oGRServiceLocation.getValue("cmn_location");
                oSOwithCountry.push(oTmp);
                oSOOnly.push(sSOid);
            }
 
            // get SO that does not have any service_subscribe_location
            var aAvailableSO = this._difference(aServiceOfferingIds, oSOOnly);
 
            // get SO that have cmn_location matching with sCountryID
            aAvailableSO = aAvailableSO.concat(oSOwithCountry.filter(function(item) {
                    return item.country === sCountryID;
                })
                .map(function(item) {
                    return item.so;
                }));
 
            var aNotAvailableSO = this._difference(aServiceOfferingIds, aAvailableSO);
            return aNotAvailableSO;
 
        } catch (oErrMsg) {
            new GSLog().logError("Script Include: CategoryUtils. Function: isServiceOfferingSubscribedOrEmpty. Error message received from Try/Catch: " + oErrMsg);
        }
    },
 
    /**
     * Function: _difference
     * Purpose: return elements in array A that are NOT present in array B
     * Input parameter: A (the main array of elements), B (the array containing elements to exclude)
     * Output:Returns a filtered version of A with elements that are not in B
     */
 
    _difference: function(A, B) {
        return A.filter(function(item) {
            return B.indexOf(item) === -1;
        });
    },


Could somebody guide me on how this Script Include should be modified. I'd appreciate any suggestions.

Thank you in advance for any hints.

0 REPLIES 0