Duplicating rows in an MRVS

The user story:

As a service catalog user, I have an easy way to duplicate selected rows in a Multi Row Variable Set (MRVS) so that I can quickly create additional similar rows.

Execution

Each MRVS is an array of objects stored as JSON. Each object represents a row in the MRVS, and the properties of the object represent the values of each variable. To interact with the array it needs to be parsed using JSON.parse

After parsing the MRVS into a JavaScript array we can use array functions to duplicate rows based on their properties. We will be using .filter, .concat, and .forEach in this script.


//Retrieve the Multi Row Variable Set and parse it.
var mrvsarr = JSON.parse(g_form.getValue('variables.copy_rows'));

//Create a new array that contains the rows that are marked for duplication.
var copies = mrvsarr.filter(function (e) {
    return e.duplicate == 'true';
});

//Append the new array to the original array.
var finalarr = mrvsarr.concat(copies);

//Update all rows to no longer be marked for duplication.
finalarr.forEach(function (e) {
    e.duplicate = 'false';
});

//Write the merged array to the MRVS after converting it back to JSON formatted string.
g_form.setValue('variables.copy_rows', JSON.stringify(finalarr));

I've shared an example widget that uses this strategy on the Developer Share here.

Comments

Popular Posts