Trying to limit visibility of a catalog item using user criteria script

afielden
Tera Contributor

I'm attempting to restrict the visibility of a catalog item with a user criteria script. I've followed the example given in this question

 

Solved: How to restrict catalog item for user country - ServiceNow Community

 

However the presence of the script makes the item invisible to everyone except admin user. It doesn't seem to make a difference what I put in this script. I've also tried running the script the debugger with a breakpoint, and the breakpoint is never hit. 

 

Is there any other logging/tracing I can use to find out what's going on?

 

6 REPLIES 6

Ratnakar7
Mega Sage
Mega Sage

Hi @afielden ,

 

If the user criteria script is causing the catalog item to be invisible to everyone except the admin user, it suggests that there may be an issue with the logic or implementation of the script. Here are some troubleshooting steps and additional logging/tracing options you can try to diagnose the problem:

  1. Check Script Syntax: Ensure that the user criteria script does not contain any syntax errors or typos. Even a small mistake can cause the script to behave unexpectedly. Double-check the script for accuracy and correctness.

  2. Verify Script Logic: Review the logic of the user criteria script to confirm that it accurately evaluates the user's country or any other criteria you are using to restrict visibility. Test the script with different user profiles to see if it behaves as expected.

  3. Debugging in Script Debugger: Use the Script Debugger in ServiceNow to debug the user criteria script. Set breakpoints within the script and test the catalog item visibility with different user profiles. This can help you identify if the script is being executed and where any issues may lie.

  4. Logging Statements: Insert logging statements (e.g., gs.log()) at various points in the user criteria script to output information to the system logs. This can help you track the execution flow of the script and identify any errors or unexpected behavior.

  5. Check Script Execution Context: Ensure that the user criteria script is executing in the correct context and has access to the necessary data. User criteria scripts run in the context of the GlideRecord sc_cat_item_user_criteria, so verify that the script is accessing the correct fields and data.

  6. Review User Criteria Record: Double-check the user criteria record associated with the catalog item to ensure that it is configured correctly. Verify that the script field contains the correct script and that the record is active and applied to the appropriate catalog item.

  7. Test with Different User Profiles: Test the catalog item visibility with different user profiles to see if the issue is specific to certain users or affects all users. This can help you determine if the problem lies with the user criteria script or with the user profiles themselves.

Thanks,

Ratnakar

afielden
Tera Contributor

So in the spirit of keeping this as simple as possible, here's my script. This doesn't work because the CI is still visible on the portal. I would expect this script to hide the item, but it's doesn't.

 

var user = GlideUser.getUserByID(user_id);
answer = false;