logo

NJP

Creating records or a summary from Multi-Row Variable Set (2)

Import · May 20, 2021 · article

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

Hi there,

Once in a while, a Community thread passes by like how to create records out of the Multi-Row Variable Set rows entered? Or how to create a summary out of the Multi-Row Variable Set rows entered? All kinds of solutions are mentioned, huge scripts, business rules applied, scripting in workflows, etcetera. I did write an earlier article with a simple small solution also, unfortunately though… that solution did not work if you would actually like to obtain display values!

For two recent Community questions, I came up with a nice solution for this.

There is one catch though, this is not rock solid for internationalization!

Multi-Row Variable Set

If we would use a small test Multi-Row Variable Set containing a Single Line Text variable "Short description" and Reference variable "User", after submitting the Catalog Item or Record Producer the available Multi-Row Variable Set code would look like:

[ {
  "short_description" : "iphone8",
  "user" : "73c3572e1b451010d01143f6fe4bcb04"
}, {
  "short_description" : "galaxy8",
  "user" : "b983d0bfdb4478d0d82ffb2439961983"
} ]

So how did we actually obtain this JSON? Depending on your situation, just by writing to the log gr.variables.mobile_devices_set, or current.variables.mobile_devices_set, or producer.mrvs_internal_name.

Creating records for each row

So how can we use the value obtained, to create records for each row, and in such a way that we don't have to hardcode every field/variable? In this example I'll assume you are submitting a Record Producer on the Incident table, using the Script field in the Record Producer to create Incident Tasks.

(function() {

    var mrvs = producer.mrvs_test;

    var l = mrvs.getRowCount();
    for(var i = 0; i < l; i++) {
        var row = mrvs.getRow(i);
        var cells = row.getCells();

        var grIncident = new GlideRecord('incident_task');
        grIncident.initialize();
        grIncident.setValue('incident', current.getUniqueValue());
        grIncident.setValue('short_description', cells[0]);
        grIncident.setValue('assigned_to', cells[1]);
        grIncident.insert();
    }

})();

This would already create a record (for this example Incident Tasks), for every row submitted. If you would have a mismatch in variable/field type, like the short_description variable being a Select Box, then you could apply:

cells[0].getDisplayValue()

Output would be something like:

image

Creating a summary

Another often-heard question, creating a summary based on a Multi-Row Variable Set. It does depend a bit on how you would like the summary presentation. Though for this example, let's assume all variables would be listed line by line, with a blank line between the rows. The summary should be added to the description of a scripted Catalog Task, which will be created within a Workflow that runs for the submitted Catalog Item.

(function() {

    var description = [];
    var mrvs = current.variables.mrvs_test;

    var l = mrvs.getRowCount();
    for(var i = 0; i < l; i++) {
        var row = mrvs.getRow(i);
        var cells = row.getCells();

        var m = cells.length;
        for(var j = 0; j < m; j++) {
            description.push(cells[j].getLabel() + ': ' + cells[j].getCellDisplayValue());
        }

        if(i < l) {
            description.push('');
        }
    }

    current.description = description.join('\n');
    current.update();

})();

Output would be something like:

image

---

And that's it, not much more to it! If any questions or remarks, let me know.

Kind regards,

Mar k Roethof

ServiceNow Technical Platform Architect @ Quint Technology

2x ServiceNow Developer MVP

2x ServiceNow Community MVP

---

View original source

https://www.servicenow.com/community/developer-articles/creating-records-or-a-summary-from-multi-row-variable-set-2/ta-p/2323993