Use custom calculations

Introduction

This article introduces how to insert custom calculation results into fields.

Custom calculations are needed when the Calculated field in Kintone is not capable of achieving the necessary calculation. For example, the following calculation flow cannot be achieved by using the Calculated field in Kintone:

  1. Calculating a total from a product's unit price and number.
  2. Discounting 10% if the total is more than $50.00.
  3. Discounting 20% if the total is more than $100.00.

Try the following example to calculate this using a JavaScript customization.

Preparing the App

Prepare an App with the following fields.
custom1.png

Field Type Field Name Field Code Note
Text (single-line) Product Name Product_Name -
Number Unit Price Unit_Price -Use thousands separators
-Required Field
-Limits of Value: Minimum 0
-Number of Decimal Places:0
-Currency: $(Prefix)
Number Amount Amount -Required Field
-Limits of Value: Minimum 0
-Number of Decimal Places:0
Number Total Total -Use thousands separators
-Limits of Value: Minimum 0
-Number of Decimal Places:0
-Currency: $(Prefix)

Rewriting the value of a field

This tutorial uses the app.record.create.submit event, the app.record.edit.submit event, and the app.record.index.edit.submit event that are triggered when users save their record. These events allow fields to be overwritten by changing the values inside it. Note that the change will not take effect, unless the event object is returned.

When the record is saved with this code in the App, the calculation should be entered into the Total field.

custom2.png

Check that the calculation and the discount is working with other calculations as well.

custom3b.png

Disabling the editing function of the total field

For this code to be more practical, add one more step of disabling the editing function of the total field while the record is in edit mode. The app.record.edit.show event, the app.record.create.show event and the app.record.index.edit.show event have functions to disable edits.


custom4.PNG

Finally

This tutorial introduced a JavaScript customization that would calculate fields differently depending on their condition. Note that the calculations take place before any field validations take place - meaning that if text is placed instead of numbers for the number fields, this will result in an error. To avoid this error, have the script check the contents of the fields first, and skip the calculation if it does not contain numbers.

Was this article helpful?
0 out of 0 found this helpful
Do you have any questions or issues related to this article?
Please share your views with us in the Community forums!