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

  1. Probé un generador de notas para planificacion academica durante el semestre y me pareció excelente por la claridad de los resultados.

    ReplyDelete
  2. Muy interesante la explicación sobre cómo duplicar filas en un Multi Row Variable Set utilizando JavaScript y JSON.parse. El uso de funciones como **filter**, **concat** y **forEach** para manipular el arreglo es una forma muy clara y eficiente de manejar los datos dentro del MRVS.

    Este tipo de lógica también se utiliza en otras herramientas digitales donde es necesario procesar y calcular datos automáticamente. Por ejemplo, en mi sitio **Mi Promedio** utilizamos un "generador de promedio" que permite a los estudiantes calcular sus calificaciones de forma automática y entender mejor su rendimiento académico.

    Gracias por compartir este ejemplo tan útil para quienes trabajan con ServiceNow y desarrollo web.

    ReplyDelete

Post a Comment

Popular Posts