[Custom Script] Calculation queries

Hello,

I have created a custom script for the addition calculation.

I am facing some issues in it.

When I am entering all the values in the field as “1” it will calculate properly and show the result as “9” as shown in the below screenshot.

But when I enter the values as “1” in some fields along with value as “0” in other fields, it will not calculate and show the result in SUM1.

Instead of showing the result as “6”, the SUM1 field is left blank as shown in the below screenshot.

Here is my custom script:

Can anyone help me in this issue.

Thanks & Regards,
Sujay

Any help?

If all field has value, it calculated correctly. If some don’t have, it returns 0. So there’re sth wrong with calculation, it could be sum1 = 1 + “” + undefined + NaN (just my guessing). You should log all the field value to see whether what’s wrong with the value.

So, how shall I make changes in the script.

Can you please help with the script changes.

Replace code with your function calculate_sum1. The code below will log the value of those fields every time any field changes value (see in browser console). You will get an idea why your calculation wrong when some fields don’t have value.

calculate_sum1: function(frm){
    console.log("threads ", frm.doc.threads);
    console.log("elastic_1", frm.doc.elastic_1);
    console.log("elastic_2", frm.doc.elastic_2);
    console.log("mobilon_tapes", frm.doc.mobilon_tapes);
    console.log("piping", frm.doc.piping);
    console.log("collar", frm.doc.collar);
    console.log("cuff", frm.doc.cuff);
    console.log("hanger_loops", frm.doc.hanger_loops);
    console.log("others", frm.doc.others);
}

It’s working now.

Thank you so much.

Can you tell me what caused the calculation wrong? How did you fixed it?
In case, someone has same problem as yours, so it will be helpful.

I just replaced “if function” code available in my script with your code.

calculate_sum1: function(frm){
console.log("threads ", frm.doc.threads);
console.log(“elastic_1”, frm.doc.elastic_1);
console.log(“elastic_2”, frm.doc.elastic_2);
console.log(“mobilon_tapes”, frm.doc.mobilon_tapes);
console.log(“piping”, frm.doc.piping);
console.log(“collar”, frm.doc.collar);
console.log(“cuff”, frm.doc.cuff);
console.log(“hanger_loops”, frm.doc.hanger_loops);
console.log(“others”, frm.doc.others);
}

My code didn’t do anything, just log the value for debug purpose.

I think your problem was your if condition which required all field need to have value.
so you can delete my code and delete if condition.

Just set the default value of all fields to 0