Dinesh Kumar11
Kilo Sage
Kilo Sage

Requirement : 

Implement User criteria for the choice list variable in the catalog item and not for the entire catalog item.

Based on the logged in user's Region/Country/Location/Groups , a particular choice in the choice list variable should be made visible. The data mapping of the choice has to be made visible based on user's Region/Country/Location/Groups will be stored in the custom table.

Solution :

1) Create records in the custom table which holds the information of the user's Region/Country/Location/Groups to whom the choice should be visible. Please refer the below screenshot for sample record. 

find_real_file.pngNote: When a choice has to be made visible to all the user's then configure "ALL" in one of the field in custom table (Sample Field in above screenshot : String 5).

2)

a) Create a catalog client script for the catalog where user criteria has to be implemented for a choice list variables. 

 

function onChange(control, oldValue, newValue, isLoading) {      
  var itemID = g_form.getUniqueValue();
    var caller = g_form.getReference('requested_for', callback);

    function callback(caller) {
        
    var ga = new GlideAjax('User_criteria_through_custom_table'); //Custom script include
        ga.addParam('sysparm_name', 'fetch_Val');
        ga.addParam('sysparm_caller', caller.sys_id);
        ga.addParam('sysparm_itemid', itemID);
        ga.getXML(getChoices);

        function getChoices(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            var responseJONObject = JSON.parse(answer);
            g_form.clearOptions('Choice_list_variable_name', true);
            g_form.addOption("Choice_list_variable_name", '', '--None--');
            for (var i = 0; i < responseJONObject.length; i++) {
                g_form.addOption("Choice_list_variable_name", responseJONObject[i].Value, responseJONObject[i].Question);

            }
        }
    }
}

 

b) Create the custom script include which reads the users information and catalog item sys_id from the above catalog client script and returns the list of choices which has to be visible for a user by reading the custom table records. 

var User_criteria_through_custom_table = Class.create();
User_criteria_through_custom_table.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    fetch_Val: function() {
        var catItem = '';
        var finalArray = [];
        var user_id = this.getParameter('sysparm_caller');
        var usrRegion;
        var groups = [];
        var userLoc;
        var userCountry;
		var userLanguage;


        var usr = new GlideRecord('sys_user');
        usr.addEncodedQuery('sys_id=' + user_id);
        usr.query();
        if (usr.next()) {
            usrRegion = usr.u_employment_country.u_it_region;
            userLoc = usr.location;
            userCountry = usr.u_employment_country;
			userLanguage = usr.preferred_language;
        }
        var grp = new GlideRecord('sys_user_grmember');
        grp.addEncodedQuery('user=' + user_id);
        grp.query();
        while (grp.next()) {
            groups.push(grp.group.sys_id);
        }
        var rec = new GlideRecord('sc_cat_item');
        rec.addQuery('sys_id', this.getParameter('sysparm_itemid'));
        rec.query();
        if (rec.next()) {
            catItem = rec.name;
            var helper = new GlideRecord('u_custom_table');
            helper.addQuery('u_index_2', catItem);
            helper.addQuery('u_index_1', "User Criteria Mapping");
			helper.addQuery('u_index_3', userLanguage);
            helper.query();
            while (helper.next()) {
                var loca = helper.u_location.toString().split(',');
                var region = helper.u_region.toString().split(',');
                var grps = helper.u_groups.toString().split(',');
                var cuntry = helper.u_countries.toString().split(',');
				var string5 = helper.u_string_5;
                if (loca.length != 0) {
                    for (var i = 0; i < loca.length; i++) {
                        if (userLoc == loca[i]) {
                            var str1 = helper.u_string_3.toString(); //label
                            var str = helper.u_string_4.toString(); // backend_value
                            var typeOfReq = {};
                            typeOfReq.Question = str1;
                            typeOfReq.Value = str;
                            finalArray.push(typeOfReq);

                        }
                    }
                }
                if (region.length != 0) {
                    for (var i = 0; i < region.length; i++) {
                        if (usrRegion == region[i]) {
                            var str1 = helper.u_string_3.toString(); //label
                            var str = helper.u_string_4.toString(); // backend_value
                            var typeOfReq = {};
                            typeOfReq.Question = str1;
                            typeOfReq.Value = str;
                            finalArray.push(typeOfReq);

                        }
                    }
                }
                if (cuntry.length != 0) {
                    for (var i = 0; i < cuntry.length; i++) {
                        if (userCountry == cuntry[i]) {
                            var str1 = helper.u_string_3.toString(); //label
                            var str = helper.u_string_4.toString(); // backend_value
                            var typeOfReq = {};
                            typeOfReq.Question = str1;
                            typeOfReq.Value = str;
                            finalArray.push(typeOfReq);
                        }
                    }
                }
                if (grps.length != 0) {
                    for (var i = 0; i < grps.length; i++) {
                        var arrayUtil = new ArrayUtil();
                        var grpValid = arrayUtil.contains(groups, grps[i]);
                        if (grpValid == true) {
                            var str1 = helper.u_string_3.toString(); //label
                            var str = helper.u_string_4.toString(); // backend_value
                            var typeOfReq = {};
                            typeOfReq.Question = str1;
                            typeOfReq.Value = str;
                            finalArray.push(typeOfReq);

                        }
                    }

                }
				
				
			if (string5 == "ALL"){
				
                            var str1 = helper.u_string_3.toString(); //label
                            var str = helper.u_string_4.toString(); // backend_value
                            var typeOfReq = {};
                            typeOfReq.Question = str1;
                            typeOfReq.Value = str;
                            finalArray.push(typeOfReq);
				}

                var json = new JSON(); // move your json setup outside the while loop
                var data = json.encode(finalArray); //JSON formatted string

            }
        }
        return data;
    },

    type: 'User_criteria_through_custom_table'
});

 

 

Hope you will find it as helpful. Don’t forget to Mark it Helpful and Bookmark article so you can easily find on your profile.

Thank you,
Dinesh Kumar Raghu,
Capgemini India Pvt Ltd.

GmailID : dineshkumar.raghu9426@gmail.com

Version history
Last update:
‎04-07-2022 03:58 AM
Updated by: