Validation while adding rows and updating the existing rows in MRVS

Brijmohan
Tera Contributor

Hi All, I have one requirement related to MRVS. In mrvs I have two variables one is cost center and second is percentage where in this variable I am storing value of how much percentage costing should be done with this respective cost center.
Requirement is whenever user add any new row it should calculate the amount in percentage variable and if total is greater then 100 then it should give error and clear the value. Same if we update the existing record and if total amount exceed more then 100 then it should give error.

For example below total of percentage field is 110 so it should not allow to add new row.

 

Brijmohan_0-1724081277814.png


So basically it should count the all previous rows percentage value and validate on time of new row addition. I am not sure it can be possible or if possible then how it will be. If anyone work on requirement like this please give your valuable suggestion.

 

Thanks and regards,
Brij

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

A script like this that applies to the MRVS, onChange of the percentage variable will do that

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	var totalPercent = 0;
    var mrvs = g_service_catalog.parent.getValue('mrvs_internal_name');

	if (mrvs.length>2) { //native UI empty MRVS = '[]' which causes a parsing error
		var obj = JSON.parse(mrvs);
		//get the total percentage of the existing rows, except if editing an existing cost center/row
		for (var i = 0; i < obj.length; i++) {
			if (obj[i].cost_center != g_form.getValue('cost_center')) { //replace with your MRVS variable name
				totalPercent += parseInt(obj[i].percent); //replace with your MRVS variable name
			}
		}

		if (totalPercent + newValue > 100) {
            alert('Total Percentage cannot exceed 100.');
            g_form.clearValue('percent');
        }
	}
}

 

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

A script like this that applies to the MRVS, onChange of the percentage variable will do that

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	var totalPercent = 0;
    var mrvs = g_service_catalog.parent.getValue('mrvs_internal_name');

	if (mrvs.length>2) { //native UI empty MRVS = '[]' which causes a parsing error
		var obj = JSON.parse(mrvs);
		//get the total percentage of the existing rows, except if editing an existing cost center/row
		for (var i = 0; i < obj.length; i++) {
			if (obj[i].cost_center != g_form.getValue('cost_center')) { //replace with your MRVS variable name
				totalPercent += parseInt(obj[i].percent); //replace with your MRVS variable name
			}
		}

		if (totalPercent + newValue > 100) {
            alert('Total Percentage cannot exceed 100.');
            g_form.clearValue('percent');
        }
	}
}

 

Hi @Brad Bowman , Thanks for you quick response. This code is working fine while adding a new row, but in some case I am automatic upload some rows in MRVS from purchase request line record. Suppose I already have two rows in mrvs while loading a form and we have 50 -50 percentage in both rows. Now it is already 100, if I am updating existing record and change from 50 to 40 but still it will give error because in total percentage we have 100 so how we can handle this situation. It is related to update existing rows?

Have you tested this scenario? The obj[i].cost_center != g_form.getValue('cost_center') if statement is meant to address this situation by not adding that existing MRVS row to the percentage total.

Hi @Brad Bowman  , Sorry for late response. Also, thanks for your help. After some changes in code, it is working fine. I am attaching the final code below. 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	var totalPercent = 0;
    var mrvs = g_service_catalog.parent.getValue('test');

	if (mrvs.length>2) { //native UI empty MRVS = '[]' which causes a parsing error
		var obj = JSON.parse(mrvs);
		//get the total percentage of the existing rows, except if editing an existing cost center/row
		for (var i = 0; i < obj.length; i++) {
			if (obj[i].cost_center != g_form.getValue('cost_center')) { 
				totalPercent += parseInt(obj[i].percentage); 
			}
		}
	}
		//alert('totalPercent'+ totalPercent);
		var newPercentageValue = parseInt(newValue);
		totalPercent = totalPercent + newPercentageValue;
		//alert('totalPercent'+ totalPercent);

		if (totalPercent > 100) {
			//alert('totalPercent '+totalPercent);
            alert('Total Percentage cannot exceed 100.');
            g_form.clearValue('percentage');
        }
	}