Currency conversion for multiple fields using an entered value
Copy link to clipboard
Copied
Hi,
I made a Form for monthly expenses and the user has to fill every row with the currency and the amount for every type of cost:
Basically I created hidden text fields for each row where there's the right change rate according to the Date/Currency entered; the total is calculated in the end using another table with the change rate and related date. I tried to use a Document level Java Script but it doesn't work, following the code:
function ChangeRate(){
// there are 24 rows and the fields I need are named VAL.0 (expense), DATE.0 (date), cVAL (number that needs to be multiplied to the cost to get the value in Euros)
var eVal = "VAL";
var eDate = "DATE";
var cVAL = "cVAL";
var N = 24;
// in the form is possible to enter 4 currency change values and I created a variable for each needed field.
// I didn't use the util.scand("dd/mm/yyyy", ___) function because I thought it could work without it but maybe that's the issue idk
var curDate0 = this.getField("cambioData0").value;
var curDate1 = this.getField("cambioData1").value;
var curDate2 = this.getField("cambioData2").value;
var curDate3 = this.getField("cambioData3").value;
var curVal0 = this.getField("cambioV0").valueAsString;
var curVal1 = this.getField("cambioV1").valueAsString;
var curVal2 = this.getField("cambioV2").valueAsString;
var curVal3 = this.getField("cambioV3").valueAsString;
for (var i = 0; i<N; i++){
// tk# are set with the combination of each row name field, t# for the actual values
var tk1 = eVal + "." + i;
var tk2 = eDate + "." + i;
var tk3 = cVAL + "." + i;
var t1 = this.getField(tk1).valueAsString;
var t2 = this.getField(tk2).value;
var t3 = this.getField(tk3);
// first it checks if the currency in the row is EUR so that the field used to calulcate is gonna be 1
if (tok1 == "EUR")
t3.value = 1;
// then it checks if the date and the chosen currency are the same. If so it does 1/changeRate (the value entered is 1€=___ ndr)
else if ((t2 == curDate0) && (t1 == curVal0))
t3.value = 1/this.getField("cRate0").value;
else if ((t2 == curDate1) && (t1 == curVal1))
t3.value = 1/this.getField("cRate1").value;
else if ((t2 == curDate2) && (t1 == curVal2))
t3.value = 1/this.getField("cRate2").value;
else if ((t2 == curDate3) && (t1 == curVal3))
t3.value = 1/this.getField("cRate3").value;
else if ((t2 == curDate4) && (t1 == curVal4))
t3.value = 1/this.getField("cRate4").value;
else t3.value = 1;
}
}
I probably did a mess but anyway.. thank you,
Silvia.
Copy link to clipboard
Copied
Hi Silvia,
This is something in which only our JavaScript experts can help you further. Moving your query to the relevant space JavaScript for them to look into this.
Thanks,
Akanchha
Copy link to clipboard
Copied
oops sorry thank you!!
Copy link to clipboard
Copied
Please explain what you mean by "it doesn't work"... Is there an error message when you use it? If so, what does it say?
Does it produce a result at all? Is it an incorrect result? If so, in what way?
Also, where did you place the call to this function?
Copy link to clipboard
Copied
no error message but the calculation doesn't work.. meaning "cVAL.#" = 1
Copy link to clipboard
Copied
I called the function in the Custom calculation script of "cVAL.#"
Copy link to clipboard
Copied
For starters, to apply the value to the calculated field you need to use event.value, like this:
event.value = ...
Copy link to clipboard
Copied
ok.. dumb question, how do I apply the t3 value (calculated in the function) to the event.value? do I have to use event.value instead of t3 in the code?
thank you again for your time btw,
Silvia.
Copy link to clipboard
Copied
Yes, exactly.

