|
| 1 | +# CheckCriteria Script Include |
| 2 | + |
| 3 | +This script include is used to check if a user has access to a specific catalog item based on "Available for" and "Not Available for" user criteria in ServiceNow. It supports admin overrides and custom user checks. |
| 4 | + |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +The `CheckCriteria` script include provides a method `itemCriteria` which checks if a user meets the criteria to access a catalog item. |
| 9 | + |
| 10 | +### Syntax |
| 11 | + |
| 12 | +```javascript |
| 13 | +var check = new CheckCriteria(); |
| 14 | +var result = check.itemCriteria(item, adminOverride, userToCheck); |
| 15 | +``` |
| 16 | + |
| 17 | +### Parameters |
| 18 | + |
| 19 | +1. **`item`** (string): |
| 20 | + - The sys_id of the catalog item you want to check access for. |
| 21 | + - This parameter is **required**. |
| 22 | + |
| 23 | +2. **`adminOverride`** (boolean, optional): |
| 24 | + - Specifies whether admin override should be taken into account. |
| 25 | + - If `true`, users with the `admin` role will always have access to the item, even if they do not match the user criteria. |
| 26 | + - Defaults to `false` if not provided. |
| 27 | + |
| 28 | +3. **`userToCheck`** (string, optional): |
| 29 | + - The user ID of the user you want to check access for. |
| 30 | + - If not provided, the currently logged-in user (`gs.getUser()`) will be used by default. |
| 31 | + |
| 32 | +### Return |
| 33 | + |
| 34 | +- **`true`** if the user has access to the catalog item. |
| 35 | +- **`false`** if the user does not have access to the catalog item. |
| 36 | + |
| 37 | +### Example |
| 38 | + |
| 39 | +```javascript |
| 40 | +var check = new CheckCriteria(); |
| 41 | + |
| 42 | +// Example 1: Check if the current user has access to the catalog item |
| 43 | +var hasAccess = check.itemCriteria('12345abcdef'); // '12345abcdef' is the sys_id of the catalog item |
| 44 | + |
| 45 | +// Example 2: Check access for a specific user with an admin override |
| 46 | +var hasAccess = check.itemCriteria('12345abcdef', true, 'abc123user'); // 'abc123user' is the user ID of the user |
| 47 | +``` |
| 48 | + |
| 49 | +In the first example, the script checks if the current user can access the specified catalog item. In the second example, it checks if the specified user can access the item and allows admin override. |
| 50 | + |
| 51 | +## Error Handling |
| 52 | + |
| 53 | +- If the `item` parameter is not provided or is `null`, an error message will be logged in the system logs. |
| 54 | +- The script also logs errors when unable to retrieve user criteria for the catalog item. |
0 commit comments