Creating records or a summary from Multi-Row Varia... - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

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:

 

find_real_file.png


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:

 

find_real_file.png

---


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

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Platform Architect @ Quint Technology

2x ServiceNow Developer MVP

2x ServiceNow Community MVP

---

LinkedIn

Comments
Manoj89
Mega Sage

Hi,

 

Any help please, I am getting the error from the screenshot when trying to create benefit plan from the multi row variable set in record producer

var mrvs = producer.benefit_plans;
var rcount = mrvs.getRowCount();

for (var k = 0; k < rcount; k++) {
	var row = mrvs.getRow(k);
	var cells = row.getCells();
	
    var bf = new GlideRecord('benefit_plan');
    bf.initialize();
	bf.setValue('task', current.getUniqueValue);
    bf.setValue('name', cells[1]); //update each CI with a serial number from the mrvs
    bf.setValue('u_description', cells[2]);
    bf.setDisplayValue('start_fiscal_period', 'FY19: M03');
    bf.setDisplayValue('end_fiscal_period', 'FY19: M03');
    bf.insert();
}
Frans Brus - No
Tera Guru

We have a form in our Service Portal which customers can use to order something.
We use a Script to populate different fields from the record producer.

For example:

current.u_external_reference = producer.external_reference;
current.company = producer.account;
current.requested_by = producer.contact;
current.type = 'normal';
current.assignment_group = '5683776bdbab0300bb3288805b961926';
current.contact_type = 'self-service';

var description =
	"Box order for: "+ producer.contact + "\n" +
	"Quantity: " + producer.box_specifications.getRowCount();

Now we want to use a Multi Row Variable set which contain 2 fields.

  • Color
  • Dimensions

I already managed to get the Quantity value in above example.

But how do I get the values for each field in the Multi Row Variable set?
I want to see the box specifications as part of the description field.

Like:

var description =

                "Box order for: "+ producer.contact + "\n" +
                "Quantity: " + producer.box_specifications.getRowCount()
                "Color and Dimensions: " + ??? + "\n" +
                "Color and Dimensions: " + ??? + "\n" +
                "Color and Dimensions: " + ??? ;
                Etc…

It would be nice if for every row the customer ordered something, the specifications will be shown on a new row.

I already did some testing but that did not give any result.

	var description1 = '';
	var mrvs = producer.variables.box_specifications;

	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++) {
			description1+=cells[j].getLabel() + ': ' + cells[j].getCellDisplayValue() + "\n" ;
		}
current.comments='test ' + mrvs;

var description =
                "Box order for: "+ producer.contact + "\n" +
                "Quantity: " + producer.box_specifications.getRowCount()  + "\n" +
                "Color and Dimensions: " + "\n" + description1;

Hope anyone can help.

Many Thanks

JWayner
Tera Contributor

This was so helpful! Thank you so much!

Chris Yang
Tera Sage

Very useful! Any way to do this in flow designer?

Version history
Last update:
‎08-17-2024 09:58 AM
Updated by:
Contributors