Automating UI Policy Creation

josgarcia
Tera Expert

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.

Screenshot 2024-11-06 at 10.58.23 AM.png

Screenshot 2024-11-06 at 10.59.09 AM.png

 

Any help is much appreciated! Thank you

1 ACCEPTED SOLUTION

josgarcia
Tera Expert

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');





View solution in original post

8 REPLIES 8

jcmings
Mega Sage

What is the business case for this? 

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.

@josgarcia 

 

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.

 

jc21_0-1730920475057.png

 

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?

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.