Datamapper replicate row data based on qty field

Hi there,

I have a detail table based on a locationId in the data to group all records from location but I also have a qty field in each row.

With this qty field I need to replicate that row of data by the value in the qty field.

Is this something that could be possible?

I have the boundaries set to change on the locationId.
I have an initial repeat step that is Repeat Type: Until no more elements.
Extracting at this point works.

But what sort of repeat do I need to use inside the initial repeat?

I thought I could just subtract 1 from the qty until it equals 0 but I’m not sure how to set this up.

Cheers

Michael

Yep. Take a look at table.addRow(), which allows you to do just that.

Hi Phil,

I had a look at that I’m not really sure how to use it though.

image

I’ve tried inside the repeat loop after the extraction but I can’t work out how to get access to the current extracted row of data to add in as json for addRow.

I would need to access the whole row to duplicated as well as the qty field to create a loop to add qty x times extra rows

image

cheers

Michael

I’ve made some progress on this today but it’s still not working how I need it to.

If I add this as an action inside the repeat section it doens’t seem to always addRow when it should.

var i = steps.currentLoopCounter - 1;
var detailRow = record.tables.detail[i];

var qty = detailRow['Qty'];

logger.info("Index: " + i + ", Initial Qty: " + qty);
while (qty > 1) {
    var tmp = {
        "ItemOrd": detailRow["ItemOrd"],
        "LocationID": detailRow.LocationID,
        "Stock category": detailRow["Stock category"],
        "Stock subcategory": detailRow["Stock subcategory"],
        "Barcode description": detailRow["Barcode description"],
        "SKU": detailRow["SKU"],
        "RetailPrice": detailRow.RetailPrice,
        "FirstSalesPrice": detailRow.FirstSalesPrice,
        "PriceCode": detailRow.PriceCode,
        "Qty": 1, // Set Qty explicitly for new rows
        "Artwork": detailRow.Artwork
    };

    record.tables.detail.addRow(tmp); // Add a new row
    qty--; // Decrement remaining quantity
    logger.info("Index: " + i + ", Duplicate row added. Remaining Qty: " + qty);
}

but if I add the action after the repeat and use this code it duplicates the records as required but the duplicates are at the end of the detail table and not next to record being duplicated . I really need them next to the records being replicated.

// Iterate through all rows in the detail table
for (var i = 0; i < record.tables.detail.length; i++) {
    var detailRow = record.tables.detail[i];
    var qty = detailRow['Qty'];

    logger.info("Index: " + i + ", Initial Qty: " + qty);

    // Process rows with Qty > 1
    while (qty > 1) {
        var tmp = {
            "ItemOrd": detailRow["ItemOrd"],
            "LocationID": detailRow.LocationID,
            "Stock category": detailRow["Stock category"],
            "Stock subcategory": detailRow["Stock subcategory"],
            "Barcode description": detailRow["Barcode description"],
            "SKU": detailRow["SKU"],
            "RetailPrice": detailRow.RetailPrice,
            "FirstSalesPrice": detailRow.FirstSalesPrice,
            "PriceCode": detailRow.PriceCode,
            "Qty": 1, // New rows always have Qty set to 1
            "Artwork": detailRow.Artwork
        };

        record.tables.detail.addRow(tmp); // Add a new row
        qty--; // Decrement remaining quantity
        logger.info("Index: " + i + ", Duplicate row added. Remaining Qty: " + qty);
    }
}


Is it obvious why the addRow within the repeat is behaving erratically ?

Well each time you add a row, you are extending the tables.detail length, which throws off your loop since it is based on the current iteration.

Change the first line of your script to this, instead:

var i = record.tables.LineItems.length-1;

That should do the trick.

Hi Phil,

Thanks for this. I had that thought about the loop being out about an hour after I finished work last night.

Just to clarify LineItems is the detail table?

This seems to be working for me now inside the loop.

var i = record.tables.detail.length -1;

Cheers
Michael

Oops, yes! Sorry, I blindly copied test code from my machine and forgot to change the data table name before posting.