Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

confirmed that they do not keep the same IO:sys_id. This is a hard one to figure out.

Oh, just kidding, it does keep the IO:sys_id as the original one.

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





Nice, glad it worked!