asifnoor
Kilo Patron

Hello,

In this article, i will show you how you can add the rows dynamically in MRVS based on the input value of a catalog item.

For this, i have created 1 Record producer, in which there is one field "No. of Companies worked". And i have 1 MultiRow variable set "Employment Details", which user has to fill the details of employer.

So once user enter the value in the "No. of Companies worked" field, we auto populate the Multi row variable set (MRVS) with that many no. of rows.

For this, we will require 1 catalog client script OnChange on the field (No. of Companies worked).

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	if(isNaN(newValue)) {
		g_form.showFieldMsg('no_of_companies','Please enter only numeric values','error',true);
	}
	//populate the multi row variable set based on the experience.
	var mrvs_data = {};
	var aDetails = [];
	mrvs_data = fillMRVS(newValue);
	var oDetails = mrvs_data;
	for(var sKey in oDetails){
		if (sKey == "employment_details") {
	              g_form.setValue(sKey, JSON.stringify(oDetails[sKey])); //This will set the multirow once the key is matched.
		}
	}
}
function fillMRVS(num) {
	var oDet = {};
	var aDet = [];
	var total_rows=0;
	for(var i=1;i<=num;i++){
		oDet = {"company": "",
			"position": "",
			"start_date": "",
			"end_date": ""
		};
	        aDet.push(oDet);
	}
	return {"employment_details": aDet};
}

If you want to populate the variable set with the data that is already filled earlier, check my other article.

https://community.servicenow.com/community?id=community_article&sys_id=86971c8bdb4108106064eeb5ca961...

Mark the article as helpful and bookmark if you found it useful.

Comments
Rolf Nyhus
Tera Guru

Hi asifnoor,

I found your article of great use, as I too need to dynamically add rows to a MRVS in a Record Producer. However, in addition, my requirement is to also prefill the rows with values based on a template from a table.

Where I run into an issue, is after the script has written the values it seems that the MRVS row data is not committed. Therefore, if I try to submit the RP without any further updates, I get a REST failure. However, if I simply click EDIT on the row and then save it (without any modifications to the data), I'm able to successfully submit the RP. If my MRVS has multiple rows, I have to do this mock operation once per row for a successful submission. This becomes an issue, because my users would like to apply a template to the RP that prefills the data and should then not need to go in an confirm the data before submitting.

Are you able to offer any insight on how I can get around this issue? In many cases there could be other fields outside the MRVS that the user needs to enter data into before submitting as well, so I cannot use a g_form.save(). Any assistance you can offer would be of great help!

Regards,
Rolf

asifnoor
Kilo Patron

Hi Rolf,

Did you get a chance to check this article. This seems to meet your requirement.

https://community.servicenow.com/community?id=community_article&sys_id=86971c8bdb4108106064eeb5ca961...

Rolf Nyhus
Tera Guru

Hi,

Yes, I did. And as mentioned, I have no problem prefilling the mrvs, the issue is that the system does not seem to acknowledge that the data is present. After the script runs data is filled in, but if I then immediately try to submit I get the REST failure, see screen shot.

find_real_file.png

However, if I go in an edit the rows and save them (even without altering any of the data), it submits without any issues.

asifnoor
Kilo Patron

Okay. Let say you added 5 rows dynamically.. If you edit 1 row, can you submit or is it submitting only after editing all 5 rows? Also, can you share the error log that you are getting when you submit.

Rolf Nyhus
Tera Guru

Yes, if I add 5 rows, I have to update all 5 rows before submitting.

Attached is the sys log.

Roberto Hidalgo
Tera Expert

Hi Rolf, were you able to make it work? I'm having the same issue.

asifnoor
Kilo Patron

Hi Robert were you also unable to update after editing 1 row?

Community Alums
Not applicable

Just wonder if anyone get resolved this issue. I am having the same problem.

 

 

 

 

RiyazAhamed
Tera Contributor

Hi,

I have similar requirement but it is reverse. I need to display the count of the records submitted in MRVS.

find_real_file.png

As of above image, the Total Number of USers in Request should be 1, it should be dynamically change when ever new record is added in MRVS or remove existing record from MRVS.

any help would be appreciated.

Thanks

Trevor Roberts
ServiceNow Employee
ServiceNow Employee

Hey I'm 3 years late to this, but I was also getting the "Rest Failure" issue like Rolf. After looking at asifnoor's code I realized that the values were all strings. I converted my numeric values to strings before JSON.stringifying, and sure enough it worked.

 

Hope this helps anyone in the future!

bhavana10
Tera Contributor

Hi Trevor,

 

Could you please eloborate the solution which worked for you?  What do you mean you have converted all numeric values to strings before JSON.stringifying?

Trevor Roberts
ServiceNow Employee
ServiceNow Employee

Hi bhavana10,

 

Instead of 

{
  "item": "ABC",
  "quantity": 4
}

do

{
  "item": "ABC",
  "quantity": "4"
}
imkhan
Tera Contributor

Hi,

 

I am working on a similar kind of work on MRVS; we are getting the label and value after parsing and showing the label while choosing select box variables, but after submitting, the showing value is on the MRVS summary, how we can show label after submitting.

 

Thanks

Version history
Last update:
‎12-25-2019 11:24 PM
Updated by: