Question / Problem
I cannot override the calculated field using JavaScript.
Current Situation
I created a Sales app that manages deals. The calculated field "total" calculates "unit_price*quantity". This works fine.
The app has these fields:
- Number field (unit_price)
- Number field (quantity)
- Calculated (total, formula is unit_price * quantity)
- Blank space (discount_space)
I want to add a discount feature with JavaScript. On the record add/edit screen, I placed two buttons, "10% discount" and "25% discount". When the user clicks a button, I get the record data with kintone.app.record.get(). I calculate the new "total" value, and set it back with kintone.app.record.set().
But nothing changes on the screen when the button is clicked.
Code / Attempts
(function() {
'use strict';
const events = ['app.record.create.show', 'app.record.edit.show'];
kintone.events.on(events, function(event) {
let spaceElement = kintone.app.record.getSpaceElement('discount_space');
let btn10 = document.createElement('button');
btn10.textContent = '10% discount';
btn10.onclick = function() {
let record = kintone.app.record.get();
let currentTotal = Number(record.record.total.value);
record.record.total.value = currentTotal * 0.9;
kintone.app.record.set(record);
};
let btn25 = document.createElement('button');
btn25.textContent = '25% discount';
btn25.onclick = function() {
let record = kintone.app.record.get();
let currentTotal = Number(record.record.total.value);
record.record.total.value = currentTotal * 0.75;
kintone.app.record.set(record);
};
spaceElement.appendChild(btn10);
spaceElement.appendChild(btn25);
return event;
});
})();
Error Message
No error message
Desired Outcome / Expected Behavior
The discount is applied to the Calculated value.