Set field value based on another field value (with javascript)
Hello,
I have a js function that help me calculate a value based on a eForm field value. I want that function on a button, so it puts that calculated value on another field in the form.
It's first time I m using Javascript and I can't get this done, basically the code doesn't do something when I press the button. ( I assigned the function to the button )
That's the code:
/////
function calculateAmount(number) {
// Round the number to one decimal place
var roundedNumber = Math.round(number * 10) / 10;
// Get the integer and decimal parts of the rounded number
var integerPart = Math.floor(roundedNumber);
var decimalPart = roundedNumber - integerPart;
// Multiply the integer part by 100 lei
var amount = integerPart * 100;
// Add the additional amount based on the decimal part
if (decimalPart >= 0.1 && decimalPart <= 0.5) {
amount += 50;
} else if (decimalPart > 0.5 && decimalPart <= 0.9) {
amount += 100;
}
return amount;
}
var options = {};
options.fieldId = 'Diferentadezilenplus1cheltronnnx';
eFormHelper.getFieldValue(options, function (result) {
if (result.isSuccess) {
var fieldValue = parseFloat(result.data);
var amount = calculateAmount(fieldValue);
options.fieldId = 'ValDiurnacalculataautomatcheltronx';
options.value = amount.toString();
eFormHelper.setFieldValue(options, function (result) {
if (result.isSuccess) {
console.log("Amount calculated and set successfully.");
} else {
console.log(result.error);
}
});
} else {
console.log(result.error);
}
});
////
-
Hi Ciprian,
My analysis of your code revealed a syntactical error where you missed wrapping the eFormHelper methods in a separate function.
function calculateAndSetAmountField() {
var options = {};
options.fieldId = 'Diferentadezilenplus1cheltronnnx';eFormHelper.getFieldValue(options, function (result) {
if (result.isSuccess) {
var fieldValue = parseFloat(result.data);
var amount = calculateAmount(fieldValue);options.fieldId = 'ValDiurnacalculataautomatcheltronx';
options.value = amount.toString();eFormHelper.setFieldValue(options, function (result) {
if (result.isSuccess) {
console.log("Amount calculated and set successfully.");
} else {
console.log(result.error);
}
});
} else {
console.log(result.error);
}
});}
Please ensure that the function wrapped for the eFormHelper methods (i.e. calculateAndSetAmountField()) is attached to the click event of the button in order to achieve the desired functionality.
サインインしてコメントを残してください。
コメント
1件のコメント