Service catalog user criteria - Not Available For Catalog Items

Ankita9793
Tera Contributor

Hi All,

 

Can someone please suggest why the below criteria's script is not working. 
my requirement is, ABC catalog should not be available/ visible for 'User criteria country' and for users with Job Code 'J04437 or J15505' and having ISO Code as 'MEX'. 

 

Ankita9793_0-1767946418040.png

answer();

function answer() {
    var user = gs.getUserID();

    var grUser = new GlideRecord('sys_user');
    grUser.addQuery('sys_id', user);
    grUser.query();
    if (grUser.next()) {
        var code = grUser.getValue('u_job_code');
        var iso = grUser.getValue('u_iso_code');
       
        if ((code == "J04437" && iso == "MEX") || (code == 'J15505' && iso == "MEX")) {
            return true;
        } else {
            return false;
        }


    }
}
5 REPLIES 5

Chaitanya ILCR
Mega Patron

Hi @Ankita9793 ,

try this code instead

 

/** Scripted User Criteria is not cached, and evaluated everytime, so performance is dependent on the script.
 * Populate `answer` with true/false or evaluate to true/false
 * The script is evaluated in the scope the user criteria is defined
 * Don't use `current` in the script or populate the variable
 * Don't use `gs.getUser()` or  `gs.getUserID()`, 
 * instead use `user_id` which contains the user sys_id against whom the evaluation is happening.
 */

answer = false;

var grUser = new GlideRecord('sys_user');
if (grUser.get(user_id)) {
    var code = grUser.getValue('u_job_code');
    var iso = grUser.getValue('u_iso_code');

    answer = ((code == "J04437" && iso == "MEX") || (code == 'J15505' && iso == "MEX"))
}

 

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

Regards,
Chaitanya

TejasSN_LogicX
Tera Contributor

Hi @Ankita9793 ,

Your script logic was reversed. In a user criteria script, returning true means the catalog item is visible, and returning false means it is hidden. Your script returned true for the users you wanted to block (with Job Code J04437 or J15505 and ISO Code MEX), which made the catalog visible to them instead of hiding it. The corrected script now returns false for those users, so the catalog is hidden only for them and visible for everyone else.

 

 answer();

function answer() {
    var user = gs.getUserID();  

    var grUser = new GlideRecord('sys_user');
    grUser.addQuery('sys_id', user);
    grUser.query();

    if (grUser.next()) {
        var jobCode = grUser.getValue('u_job_code');
        var isoCode = grUser.getValue('u_iso_code');

         
        if ((jobCode == "J04437" || jobCode == "J15505") && isoCode == "MEX") {
            return false; 
        }

        return true; 
    }

    return true;  d
}

 

 

 

 

If you found my solution helpful, please mark it as Helpful or Accepted Solution...!

thanks,

tejas

Email: adhalraotejas1018@gmail.com

LinkedIn: https://www.linkedin.com/in/tejas1018

 

Bhavya11
Kilo Patron

Hi @Ankita9793 ,

 

can you try this code






    var code = gs.getUser().getRecord().getValue('u_job_code'); // if it is reference value then use getDisplayValue instead of getValue()
    var iso = gs.getUser().getRecord().getValue('u_iso_code');
    if ((code == "J04437" && iso == "MEX") || (code == 'J15505' && iso == "MEX")) {

        answer = true;

    } else {

       answer = false; 

    }


 

If this information proves useful, kindly mark it as helpful or accepted solution.

 

Thanks,

BK

GlideFather
Tera Patron

Hi @Ankita9793,

 

what do you receive in the logs?

 

Be aware that you have marked "Match all" checkbox, which means that not only the script must be evaluated but also the conditions above it... try to uncheck this field and retest if you see any difference.

 

If it will be still the same behaviour, then ignore this suggestion, if it will start working, then you might need to change the logics, I miss the context behind it and am not familiar wiht the data processing 

_____
No AI was used in the writing of this post. Pure #GlideFather only