- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 10:00 AM - edited 11-06-2024 10:04 AM
Hello, I'm trying to automate the creation of a certain UI policy every time a certain variable set is added to a catalog item. It works, but the only issues I have been having is getting the conditions to add properly.
Here is my business rule: CreateNewRole Varset UI Policy to Parent
Table: Catalog Variable Set [io_set_item]
Advanced: True
When to apply: After Insert or Update
(function executeRule(current, previous /*null when async*/) {
// Step 1: Check if the variable_set in the inserted record matches the specified sys_id
if (current.variable_set == 'c7f2ce6d336d1e101bc9a7273e5c7b68') {
// Retrieve the catalog item sys_id from the inserted io_set_item record
var catalogItemId = current.sc_cat_item.sys_id.toString();
// Step 2: Check if the catalog item has the additional variable set
var hasAdditionalVarSet = false;
var grCatItem = new GlideRecord('io_set_item');
grCatItem.addQuery('sc_cat_item', catalogItemId);
grCatItem.addQuery('variable_set', 'bc2b68a447ac8e1040f5f855516d431a');
grCatItem.query();
if (grCatItem.next()) {
hasAdditionalVarSet = true;
}
// Step 3: Query the catalog_ui_policy table for the UI policy with the specified short description
var grCatItemPolicy = new GlideRecord('catalog_ui_policy');
grCatItemPolicy.addQuery('short_description', 'set cb to true if create_new_role');
grCatItemPolicy.query();
// Step 4: If the UI policy is found, create a copy by updating only the catalog_item and short_description fields
if (grCatItemPolicy.next()) {
// Temporarily change the catalog_item and short_description for the new record
grCatItemPolicy.setValue('catalog_item', catalogItemId); // Set to the new catalog item
if (hasAdditionalVarSet) {
grCatItemPolicy.setValue('short_description', 'set cb to true if create_new_role w/ standard var set');
}
// Insert the modified record as a new entry (clone)
grCatItemPolicy.insert(); // This duplicates the record with all fields and conditions intact
// No need to revert fields since we're inserting a copy, leaving the original untouched
} else {
// Log an error if the specified UI policy is not found
gs.error("UI Policy with short description 'set cb to true if create_new_role' not found in catalog_ui_policy table.");
}
}
})(current, previous);
What I need it to do is what is shown in the first screenshot. What it does is shown in the 2nd screenshot.
Any help is much appreciated! Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 12:03 PM
I was able to resolve this. Here is the updated script
(function executeRule(current, previous /*null when async*/) {
// Step 1: Check if the variable_set in the inserted record matches the specified sys_id
if (current.variable_set == 'c7f2ce6d336d1e101bc9a7273e5c7b68') {
// Retrieve the catalog item sys_id from the inserted io_set_item record
var catalogItemId = current.sc_cat_item.sys_id.toString();
// Step 2: Check if the catalog item has the additional variable set
var hasAdditionalVarSet = false;
var grCatItem = new GlideRecord('io_set_item');
grCatItem.addQuery('sc_cat_item', catalogItemId);
grCatItem.addQuery('variable_set', 'bc2b68a447ac8e1040f5f855516d431a');
grCatItem.query();
if (grCatItem.next()) {
hasAdditionalVarSet = true;
}
// Step 3: Query the catalog_ui_policy table for the UI policy with the specified short description
var grCatItemPolicy = new GlideRecord('catalog_ui_policy');
grCatItemPolicy.addQuery('short_description', 'set cb to true if create_new_role');
grCatItemPolicy.query();
// Step 4: If the UI policy is found, create a copy by updating only the catalog_item and short_description fields
if (grCatItemPolicy.next()) {
// Temporarily change the catalog_item and short_description for the new record
grCatItemPolicy.setValue('catalog_item', catalogItemId); // Set to the new catalog item
if (hasAdditionalVarSet) {
grCatItemPolicy.setValue('short_description', 'set cb to true if create_new_role w/ standard var set');
grCatItemPolicy.setValue('catalog_conditions', 'IO:9b7b64a447ac8e1040f5f855516d43ea=Create new role^EQ');
}
// Insert the modified record as a new entry (clone)
grCatItemPolicy.insert(); // This duplicates the record with all fields and conditions intact
// No need to revert fields since we're inserting a copy, leaving the original untouched
} else {
// Log an error if the specified UI policy is not found
gs.error("UI Policy with short description 'set cb to true if create_new_role' not found in catalog_ui_policy table.");
}
}
})(current, previous);
Adding this line fixed the issue:
grCatItemPolicy.setValue('catalog_conditions', 'IO:9b7b64a447ac8e1040f5f855516d43ea=Create new role^EQ');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 10:33 AM
What is the business case for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 10:42 AM
The business case is to speed up time for us to be able to bulk add this catalog variable set on many items without having to manually create the UI policy for each one afterwards. So it's to save time and to ensure that the UI policy isn't forgotten after having added the variable set.
This UI policy has to be on the catalog item and not on the variable set.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 11:17 AM
And you are trying to set the same field in the catalog condition each time? Your first screenshot shows "Standard Catalog Item Var Set >> ..." as the field. I am not sure if variable sets retain the same IO:sys_id on every catalog item. I am guessing that's why you are running into issues. See my screenshot below, where I'm using SNUtils to see the values of each variable within a catalog UI policy.
How many catalog items are you trying to apply the policy to? Can you just change the catalog_item field on the UI Policy and Insert and Stay?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 11:23 AM
Yeah, insert and stay is the type of action I was hoping to perform but not sure how to do that since I thought 'insert' would do it.
I'm not sure if they have different IO:sys_id either. Thats a good catch though, I'd have to look into that.