- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 08:29 PM - edited 01-01-2024 08:33 PM
Hi,
I have a requirement that I need to add the sum of column field from multirow variable set and display it on the form. For this I have created a check box(Calculate total amount) field on form and onChange client script based on the checkbox.
I have two fields as credit(credit_amount) and debit(debit_amount) on the multirow variable set and two more fields on form as Total debit and total credit. System allows user to enter only one field(credit or debit )in one row. After entering multiple data user need to click on the checkbox(Calculate total amount). Onchange of checkbox the sum of credit should reflect on the total credit field on form and sum of debit should reflect on total debit field on form . for this I have used the following script, Please suggest me the best approach to achieve it.
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '' || isLoading) {
return;
}
var multiRowVariableSet = JSON.parse(g_form.getValue('details'));
var totaldebit = 0;
if (multiRowVariableSet){
for (var i = 0; i < multiRowVariableSet.length; i++) {
totaldebit += parseFloat(multiRowVariableSet[i].debit_amount);
g_form.setValue('total_debit', totaldebit);
}
}
var multiSet = JSON.parse(g_form.getValue('details'));
var totalcredit = 0;
if (multiSet) {
for (var j = 0; j < multiSet.length; j++) {
totalcredit += parseFloat(multiSet[j].credit_amount);
g_form.setValue('total_credit', totalcredit);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 10:37 PM
Please try this :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
//alert(g_form.getValue('IO:fc1031a82fe735902b86d4a72799b6b4'));
// Please enter the Sys_ID of your MVRS Variable below
var multiRowVariableSet = JSON.parse(g_form.getValue('IO:fc1031a82fe735902b86d4a72799b6b4'));
var totalDebit = 0;
var totalCredit = 0;
if (multiRowVariableSet) {
for (var i = 0; i < multiRowVariableSet.length; i++) {
var row = multiRowVariableSet[i];
if (row.debit_amount) {
totalDebit += parseFloat(row.debit_amount);
}
if (row.credit_amount) {
totalCredit += parseFloat(row.credit_amount);
}
}
}
g_form.setValue('totalDebit', totalDebit.toFixed(2));
g_form.setValue('totalCredit', totalCredit.toFixed(2));
}
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 10:41 PM
variables debit_amount and credit_amount are of what type?
try this and it will work if your 2 above variable names are correct and details is the name of MRVS
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '' || isLoading) {
return;
}
var multiRowVariableSet = JSON.parse(g_form.getValue('details'));
var totaldebit = 0;
var totalcredit = 0;
if (multiRowVariableSet.length > 0){
for (var i = 0; i < multiRowVariableSet.length; i++) {
if(multiRowVariableSet[i].debit_amount)
totaldebit += parseInt(multiRowVariableSet[i].debit_amount);
if(multiRowVariableSet[i].credit_amount)
totalcredit += parseInt(multiRowVariableSet[i].credit_amount);
}
}
if(totaldebit)
g_form.setValue('total_debit', totaldebit);
if(totalcredit)
g_form.setValue('total_credit', totalcredit);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 10:41 PM
variables debit_amount and credit_amount are of what type?
try this and it will work if your 2 above variable names are correct and details is the name of MRVS
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '' || isLoading) {
return;
}
var multiRowVariableSet = JSON.parse(g_form.getValue('details'));
var totaldebit = 0;
var totalcredit = 0;
if (multiRowVariableSet.length > 0){
for (var i = 0; i < multiRowVariableSet.length; i++) {
if(multiRowVariableSet[i].debit_amount)
totaldebit += parseInt(multiRowVariableSet[i].debit_amount);
if(multiRowVariableSet[i].credit_amount)
totalcredit += parseInt(multiRowVariableSet[i].credit_amount);
}
}
if(totaldebit)
g_form.setValue('total_debit', totaldebit);
if(totalcredit)
g_form.setValue('total_credit', totalcredit);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 10:37 PM
Please try this :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
//alert(g_form.getValue('IO:fc1031a82fe735902b86d4a72799b6b4'));
// Please enter the Sys_ID of your MVRS Variable below
var multiRowVariableSet = JSON.parse(g_form.getValue('IO:fc1031a82fe735902b86d4a72799b6b4'));
var totalDebit = 0;
var totalCredit = 0;
if (multiRowVariableSet) {
for (var i = 0; i < multiRowVariableSet.length; i++) {
var row = multiRowVariableSet[i];
if (row.debit_amount) {
totalDebit += parseFloat(row.debit_amount);
}
if (row.credit_amount) {
totalCredit += parseFloat(row.credit_amount);
}
}
}
g_form.setValue('totalDebit', totalDebit.toFixed(2));
g_form.setValue('totalCredit', totalCredit.toFixed(2));
}
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.