Populate field outside MRVS with a total sum of fields inside MRVS

Suryansh Verma
Tera Contributor

Hello Community,

I want to populate a field called total sum on a record producer form with a total sum of the fields inside an MRVS.

I was able to achieve this using an onSubmit catalog client script, but it only populates the data when user submits the form using submit button.

Is there any way that when user clicks on ADD button on MRVS then it populates the field outside MRVS.

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

hello @Suryansh Verma ,

yes its possible .Follow the below steps to do it 

1)Create simple widget like below and use the below code in client controller of the widget

In the if loop replace testing with your MVRS internal name and count with your variable back end name where you want to store the count 

sum = parseInt(sum)+parseInt(rows[i].your_field_name_to be_summed)

in the above line just replace your_field_name_to be_summed with your field name where your count values to be summed are stored

api.controller=function($scope,$rootScope) {
	/* widget controller */
	var c = this;
	$rootScope.$on("field.change", function(evt, parms) {
		var g_form = $scope.page.g_form;
		if(parms.field.name=="testing") // replace testing with the your  MVRS internal name
		{
			var rows = JSON.parse(g_form.getValue('testing'));  // replace testing with the your  MVRS internal name
var sum=0;
 for(var i=0; i<rows.length; i++)
{
sum = parseInt(sum)+parseInt(rows[i].your_field_name_to be_summed) // replace your_field_name_to be_summed with your field name where your numbers to be added are stored
}
			g_form.setValue('count',parseInt(sum)); // replace count with the variable back end name where you want to store the count of the MVRS rows
		}
		
	});
};

find_real_file.png

2)Then go to your catalog item and create. variable type called "custom" and tag your widget like below in widget field 

find_real_file.png

Then try adding the rows and giving the number in the field and see if its working 

Hope this helps 

please mark my answer correct if this helps you

 

View solution in original post

11 REPLIES 11

Kalyani Jangam1
Mega Sage
Mega Sage

Hi Suryansh,

I don't think it is possible. Try onsubmit variable set catalog client script but not able to populates the field outside MVRS. You have to use OnSubmit client script like below code

var mrvsRows = g_form.getValue("mrvs_name");
//The above will return a string in the form of array and objects. Now you will have to parse it

var oMrvs = JSON.parse(mrvsRows);
var sum = 0;
for(var i in oMrvs) {
	sum += oMrvs[i]["cost_field_name"]
}

g_form.setValue("total_cost_field_name", sum);

Mohith Devatte
Tera Sage
Tera Sage

hello @Suryansh Verma ,

yes its possible .Follow the below steps to do it 

1)Create simple widget like below and use the below code in client controller of the widget

In the if loop replace testing with your MVRS internal name and count with your variable back end name where you want to store the count 

sum = parseInt(sum)+parseInt(rows[i].your_field_name_to be_summed)

in the above line just replace your_field_name_to be_summed with your field name where your count values to be summed are stored

api.controller=function($scope,$rootScope) {
	/* widget controller */
	var c = this;
	$rootScope.$on("field.change", function(evt, parms) {
		var g_form = $scope.page.g_form;
		if(parms.field.name=="testing") // replace testing with the your  MVRS internal name
		{
			var rows = JSON.parse(g_form.getValue('testing'));  // replace testing with the your  MVRS internal name
var sum=0;
 for(var i=0; i<rows.length; i++)
{
sum = parseInt(sum)+parseInt(rows[i].your_field_name_to be_summed) // replace your_field_name_to be_summed with your field name where your numbers to be added are stored
}
			g_form.setValue('count',parseInt(sum)); // replace count with the variable back end name where you want to store the count of the MVRS rows
		}
		
	});
};

find_real_file.png

2)Then go to your catalog item and create. variable type called "custom" and tag your widget like below in widget field 

find_real_file.png

Then try adding the rows and giving the number in the field and see if its working 

Hope this helps 

please mark my answer correct if this helps you

 

@Mohith Devatte Thank you for the above response

I tried the above solution but it doesn't work for me.

Am i doing something wrong.

find_real_file.png

 

find_real_file.png

find_real_file.png

 

 

find_real_file.png

@Suryansh Verma can you try putting alerts like below and let me know what is coming in the alert ?

it worked for me in my instance .Lets try to debug in your instance

with your code place the below alerts after this line 

var rows = JSON.parse(g_form.getValue('testing'));  // replace testing with the your  MVRS internal name

alert('JSON'+rows);
alert('Length'+rows.length);

 

api.controller=function($scope,$rootScope) {
	/* widget controller */
	var c = this;
	$rootScope.$on("field.change", function(evt, parms) {
		var g_form = $scope.page.g_form;
		if(parms.field.name=="testing") // replace testing with the your  MVRS internal name
		{
			var rows = JSON.parse(g_form.getValue('testing'));  // replace testing with the your  MVRS internal name
alert('JSON'+rows);
alert('Length'+rows.length);
var sum=0;
 for(var i=0; i<rows.length; i++)
{
sum = parseInt(sum)+parseInt(rows[i].your_field_name_to be_summed) // replace your_field_name_to be_summed with your field name where your numbers to be added are stored

}
alert(sum);
			g_form.setValue('count',parseInt(sum)); // replace count with the variable back end name where you want to store the count of the MVRS rows
		}
		
	});
};