Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Show Catalog variable to a particular User criteria

rohanbhatia
Tera Contributor

I have a catalog item, 

There is a variable named cateogry and i am having 3 User criterias - critera x, y, z. I want to make category variable visible only to users in user criteria z. How can i achieve this without making a seperate catalog item.

2 REPLIES 2

Medi C
Giga Sage

@rohanbhatia 

The following code on Server side can give you list of the user criteria for the current logged in user:

SNC.UserCriteriaLoader.getAllUserCriteria()

 

You can give it a try on "Scripts - Background" first To check the output.
You can then use this function in a script include which you may call from a client script from your catalog item. Based on the results, you can determine if to make the category variable visible.


Thanks & Best regards,
Medi

Chaitanya ILCR
Giga Patron

Hi @rohanbhatia ,

 

You can create a Client callable script include and a catalog client script for this

SI

 

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

    checkUserClient: function() {
        return this.checkUser(this.getParameter('arrayOfUserCriteria').split(','), this.getParameter('userId'));
    },
    checkUser: function(arrayOfUserCriteria, user) {
        user = user ? user : gs.getUserID();
        return sn_uc.UserCriteriaLoader.userMatches(user, arrayOfUserCriteria);
    },
    type: 'testMeetsUserCriteria'
});

 

Client Script

 

var a = new GlideAjax('testMeetsUserCriteria');
a.addParam('arrayOfUserCriteria', 'ebb789d3ff4131003f07ffffffffff3f,d2d2e20557130300d873ac71ef94f9aa,fd8f94c3b58b8110f8779c9460b514ce' /*Comma separated usercriteria sysids*/ )
a.addParam('sysparm_name', 'checkUserClient');
// a.addParam('userId',g_user.userID)
a.getXMLAnswer(function(ans) {
    if (ans == 'false') {
        g_form.setDisplay('variable_name', false)
    }

});

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya