Custom Calculation Script - Multiplying two fields
I have an issue calculating a value correctly. But this involves a few fields to get to the answer.
- I provide four fields for the user to input numbers (A_TOTAL, B_TOTAL, C_TOTAL and D_TOTAL). I then take the sum of all these four numbers and it is provided in field G_TOTAL. All values are in this format ($x,xxx.xx)
- I first want the user to provide 3 dates (I have 3 date fields); StartDate, EndDate, and DateWithdrawal
- Based on the 3 dates provided in step 1, created 3 additional fields where I determine a value by using a calculation script.
For example:
- CompletedDays: I take the StartDate and DateWithdrawal to determine the difference in number of days
// Custom calculate script
(function () {
var sStart = getField("StartDate").valueAsString;
var sEnd = getField("DateWithdrawal").valueAsString;
var dStart, dEnd, diff;
if(sStart && sEnd) {
dStart = util.scand("m/d/yyyy", sStart);
dEnd = util.scand("m/d/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
//Add one day to the difference
event.value = Math.floor(diff / 864e5)+1;
} else {
event.value = "";
}
})();
- TotalDays: I calculate the total number of days by taking the difference between the StartDate and EndDate fields
// Custom calculate script
(function () {
var sStart = getField("StartDate").valueAsString;
var sEnd = getField("EndDate").valueAsString;
var dStart, dEnd, diff;
if(sStart && sEnd) {
dStart = util.scand("m/d/yyyy", sStart);
dEnd = util.scand("m/d/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
//Add one day to the difference
event.value = Math.floor(diff / 864e5);
} else {
event.value = "";
}
})();
- PercentEnrollCompleted: I take the CompletedDays field and divide that by the TotalDays (CompletedDays/TotalDays). The value reports a percent (example: 19.6%)
// Calculation script
(function () {
// Get field values as numbers
var numerator = +getField("CompletedDays").value;
var denominator = +getField("TotalDays").value;
// Perform calculation
if (denominator !== 0){
event.value = (numerator / denominator);
} else {
event.value = "";
}
})();
3. I have another calculated field called H_VALUE that evaluates the PercentEnrollCompleted field. If the percent is > 0.60, I assign a 100% , otherwise I report the percent if it's less than or equal to 0.60 (or 60%)
// Calculation script
(function () {
// Get field values as numbers
var v4 = +getField("PercentEnrollCompleted").value;
// Perform calculation
if (v4 > 0.60) {event.value = 1;
} else {
event.value = v4;
}
})();
So far it is working fine until I get to step 5 (next step):
5. This is where I'm having an issue, and it may be because of the various fields I'm using to determine this. In this step, I determine field I_TOTAL. Basically, I take the percent determined in Step 4 (H_VALUE) and multiply that by the number in Step 1 (G_TOTAL), but the number is not calculating correctly. As I update the date in step 3, the value in I_TOTAL is recalculated but again, it's not correct.
For example, when it multiplies these two fields, H_VALUE is 19.8% and G_TOTAL is 9,783.00, it calculates I_TOTAL as 2,030.43, but it should be 1,937.03. I can't seem to figure out where I am going wrong. Here is the calculated script I am using:
var f = this.getField("H_VALUE").value; // variable for the estimated field object
var g = this.getField("G_TOTAL").value; // variable for the actual field object
event.value = f * g; // multiply both values
