logo

NJP

Creating records from Multi Row Variable Set

Import · May 31, 2020 · article

Hi there,

Ones in a while a Community thread passes by like how to create records out of the Multi-Row Variable Set rows entered? All kinds of solutions are mentioned, huge scripts, business rules applied, scripting in workflows, etcetera. So I thought let's see if we can come up with a lean, understandable and correct solution myself.

Case

For example, from a Record Producer with a Multi-Row Variable Set, one might want to create a record in a custom application, and child records are created based on the Multi-Row Variable Set rows entered. We could simulate this by using out-of-the-box Incident (for the Record Producer) and Incident Task (for the Multi-Row Variable Set).

Multi-Row Variable Set

If we would set up a basic Multi-Row Variable Set, with only a Short description field, after submitting the Record Producer the available Multi-Row Variable Set code would look like:

[ {
    "short_description" : "Test Row 1"
}, 
{
    "short_description" : "Test Row 2"
} ]

Knowing this, the available code is just plain JSON.

Record Producer Script

Within the Record Producer Script field, we could parse the JSON value retrieved. For this example, we named our Multi-Row Variable Set "Incident Task" [incident_task].

The number of rows within the Multi-Row Variable Set:

producer.incident_task.getRowCount()

Knowing this, we could use the getRowCount within a for loop.

Individual fields within the Multi-Row Variable Set could be accessed through:

producer.incident_task.getRow(0).short_description

Translating this all into one script, and creating new records (Incident Tasks) based on the Multi-Row Variable Set rows, we could use basic scripting like:

(function() {

    var rowsInt = producer.incident_task.getRowCount();

    for(var i = 0; i < rowsInt; i++) {
        var grIncident = new GlideRecord('incident_task');
        grIncident.initialize();
        grIncident.setValue('incident', current.getUniqueValue());
        grIncident.setValue('short_description', producer.incident_task.getRow(i).short_description);
        grIncident.insert();
    }

})();

Result

image

---

And that's it actually. Hope you like this. If any questions or remarks, let me know!

image If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful.Interested in more articles, blogs, videos, and Share projects on Multi-Row Variable Set I published?- Multi-Row Variable Set

Kind regards,Mark

2020 ServiceNow Community MVP

2020 ServiceNow Developer MVP

---

LinkedIn

image

View original source

https://www.servicenow.com/community/developer-articles/creating-records-from-multi-row-variable-set/ta-p/2324569