Business rule not working on Software Catalogs

MayrelCerero
Tera Expert

Hello, I created a business rule to run async, insert on the Software Catalog table. The goal is to auto-set the assigned topics, catalogs and categories on new software catalogs.

(function executeRule(current, previous /* null */ ) {
    var TECHNICAL_CATALOG = '5bb50f658765e21472d20f28cebb35dc';
    var IT_CATALOG = '47cc2f2b87b4e2904edf20a8cebb3575';

    // --- Taxonomy ---
    var CONTENT_TYPE_CI = '98f9a16553622010069addeeff7b1248';
    var TOPIC_IT = '35705cc41b666d50d89420ecac4bcba2';
    var TOPIC_ASSET_MAN = 'ce705cc41b666d50d89420ecac4bcbd4';
    var TOPIC_NEW_SOFTWARE = '97321fe18729e21472d20f28cebb35ff';

    function categoryMapping(catSysId, label) {
        var m2m = new GlideRecord('sc_cat_item_category');
        m2m.addQuery('sc_category', catSysId);
        m2m.addQuery('sc_cat_item', current.sys_id);
        m2m.setLimit(1);
        m2m.query();

        if (m2m.next()) {
            gs.info('[MC - BR:After] category mapping exists; skipping. label=' + label + ', cat=' + catSysId);
            return;
        }
        m2m.initialize();
        m2m.setValue('sc_category', catSysId);
        m2m.setValue('sc_cat_item', current.sys_id);
        var id = m2m.insert();
        var err = m2m.getLastErrorMessage();
        if (id) gs.info('[MC - BR:After] added category mapping. label=' + label + ', id=' + id);
        else gs.error('Category insert failed. label=' + label + ', err=' + err);
    }

    function topicMapping(topicSysId, label) {
        var tax = new GlideRecord('m2m_connected_content');
        tax.addQuery('catalog_item', current.sys_id);
        tax.addQuery('content_type', CONTENT_TYPE_CI);
        tax.addQuery('topic', topicSysId);
        tax.setLimit(1);
        tax.query();

        if (tax.next()) {
            gs.info('[MC - BR:After] taxonomy exists; skipping. topic=' + label + ' (' + topicSysId + ')');
            return;
        }

        tax.initialize();
        tax.setValue('catalog_item', current.sys_id);
        tax.setValue('content_type', CONTENT_TYPE_CI);
        tax.setValue('topic', topicSysId);
        var id = tax.insert();
        var err = tax.getLastErrorMessage();
        if (id) gs.info('[MC - BR:After] added taxonomy. topic=' + label + ', id=' + id);
        else gs.error('Taxonomy insert failed. topic=' + label + ', err=' + err);
    }

    try {
        gs.info('[MC - BR:After] start for item=' + current.sys_id);

        var primary = current.getValue('category');
        gs.info('[MC - BR:After] primary category chosen in modal=' + primary);
        categoryMapping(TECHNICAL_CATALOG, 'technical_software');
        categoryMapping(IT_CATALOG, 'it_software');
        topicMapping(TOPIC_IT, 'IT');
        topicMapping(TOPIC_ASSET_MAN, 'IT Asset Management');
        topicMapping(TOPIC_NEW_SOFTWARE, 'New Software');
        gs.info('[MC - BR:After] complete for item=' + current.sys_id);
    } catch (e) {
        gs.error('error: ' + e.message);
    }
})(current, previous);

 

On the Software Model's related links we click 'Publish to Software Catalog' then a modal appears to select the category to add the catalog item to. All of these new catalogs are to be included in both the Software IT Catalog and Software Technical Catalog. In some cases, if I select Software (IT Catalog) on the modal, all three items are applied correctly. But if I select Software (Technical Catalog), the categories and catalogs are not applied as expected. I've reviewed other Business Rules that might be causing this issue, but nothing stands out so far. On the log table, I can see that everything is correctly applied, but I can't figure out why I don't see the same results for all catalog items. 

 

 

1 REPLY 1

Itallo Brandão
Tera Guru

Hi Mayrel,

The logs are creating a false positive. You are successfully creating the Category M2M records, but you are failing to update the Item itself.

For those categories to be visible, the Item must explicitly belong to the corresponding Catalogs in the sc_catalogs field. If the Item is only linked to "Technical Catalog", the system will likely hide the "IT Catalog" categories you just added via script.

Since this is an Async rule, changes to current are not saved automatically. You need to append the missing Catalog IDs to the sc_catalogs field and force a save.

Add this before your category mapping logic:

 
// ... existing constants ...

// Force update the Catalogs field on the item
var currentCatalogs = current.getValue('sc_catalogs') || '';
if (currentCatalogs.indexOf(IT_CATALOG) == -1 || currentCatalogs.indexOf(TECHNICAL_CATALOG) == -1) {
    current.setValue('sc_catalogs', currentCatalogs + ',' + IT_CATALOG + ',' + TECHNICAL_CATALOG);
    current.setWorkflow(false); // Avoid recursive loops
    current.update();
}

// ... rest of your code ...

If this fixes the visibility, please mark it as Accepted Solution.

Best regards, Brandão.