- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 08:28 AM
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.
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 08:49 AM
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');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 08:49 AM
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');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2024 10:54 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2024 05:02 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2024 09:02 AM
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');
}
}