Create a script to create slot records in ETL

RupaliJ
Tera Contributor

I work for TNI. I want to upload Rack data in Dev using Excel data. I have one field for slot count. I want to create slot records as per the count  in 'cmdb_ci_container_slot' table. 

Please suggest a script to create the slot records in ETL.

 

Thanks

1 REPLY 1

Itallo Brandão
Tera Guru

Hi RupaliJ,

Since you are working with TNI (Telecommunications Network Inventory) and uploading data, this is a common requirement.

Technical Strategy: Standard ETL tools (like Integration Hub ETL) are designed to map 1 Source Row to 1 Target Record. Generating N child records (Slots) based on a single integer field (Slot Count) is difficult to do inside the visual mapping logic.

The Best Practice Solution: Instead of trying to force this loop inside the ETL processor, you should use an Async Business Rule on the Rack table (cmdb_ci_rack). This creates a robust architecture:

  1. ETL: Simply inserts the Rack and sets the u_slot_count (or your custom field).

  2. Business Rule: Detects the new Rack and automatically generates the slots.

Here is the script you need:

Step 1: Create the Business Rule

  • Table: Rack [cmdb_ci_rack] (or your specific TNI class)

  • When: Async (Recommended for performance) or After Insert

  • Advanced: Checked

  • Condition: Slot Count Changes AND Slot Count GreaterThan 0

Step 2: The Script

(function executeRule(current, previous /*null when async*/) {

    var count = parseInt(current.u_slot_count, 10); // Replace with your actual field name
    var rackID = current.sys_id;

    // Loop to create the exact number of slots
    for (var i = 1; i <= count; i++) {
        
        var grSlot = new GlideRecord('cmdb_ci_container_slot');
        grSlot.initialize();
        
        // Naming Convention: "RackName-Slot-01" or just "Slot 1"
        grSlot.name = 'Slot ' + i; 
        
        // RELATIONSHIP: This is the most important part.
        // In TNI/CMDB, slots usually link to the Rack via 'equipment_holder'
        grSlot.equipment_holder = rackID; 
        
        // Optional: Set specific TNI attributes like position, order, etc.
        grSlot.position = i; 
        
        grSlot.insert();
    }

    gs.info('TNI Import: Generated ' + count + ' slots for Rack: ' + current.name);

})(current, previous);


Alternative: Using Transform Map (Legacy)

If you are strictly using a Legacy Transform Map (not Integration Hub ETL) and want to do it immediately during the load:

  • Go to Transform Scripts.

  • Create an onAfter script.

(function runTransformScript(source, map, log, target /*GlideRecord*/) {

    var count = parseInt(source.u_slot_count, 10); // Source Excel column

    if (count > 0 && target.sys_id) {
        for (var i = 1; i <= count; i++) {
            var grSlot = new GlideRecord('cmdb_ci_container_slot');
            grSlot.initialize();
            grSlot.name = 'Slot ' + i;
            grSlot.equipment_holder = target.sys_id; 
            grSlot.insert();
        }
    }

})(source, map, log, target);


Summary:
The Business Rule approach is cleaner because it works even if you create a Rack manually or via API later.

If this script solves your Rack/Slot generation requirement, please mark it as Accepted Solution.

Best regards,
Brandão.