Using custom calculations

Intro

In this article, we introduce how to insert custom calculation results into fields.

Custom calculations are needed, when the Calculated field in kintone is not capable of achieving the calculation that you need. 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 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

In this example, we will be using the app.record.create.submit event, the app.record.edit.submit 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 you return the event object.

(function () {
	"use strict";
	kintone.events.on(['app.record.create.submit', 'app.record.edit.submit','app.record.index.edit.submit'], function (event){
		var record = event.record;
		var price = record['Unit_Price'].value;
		var number = record['Amount'].value;

		// Calculating the total
		var subTotal = price * number;

		// Discounting 20% if the total is more than $100.00
		if (subTotal >= 100) {
			subTotal = subTotal / 100 * 80;

		// Discounting 10% if the total is more than $50.00
		} else if (subTotal >= 50) {
			subTotal = subTotal / 100 * 90;

		}

		record['Total'].value = subTotal;

		return event;
	});})();

When you save your record with this code in your 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 practical, we can add one more step by 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.

(function () {
	"use strict";
	kintone.events.on(['app.record.create.submit', 'app.record.edit.submit', 'app.record.index.edit.submit'], function (event){
		var record = event.record;
		var price = record['Unit_Price'].value;
		var number = record['Amount'].value;
		var subTotal = price * number;

		if (subTotal >= 100) {
			subTotal = subTotal / 100 * 80;
		} else if (subTotal >= 50) {
			subTotal = subTotal / 100 * 90;
		 }
		record['Total'].value = subTotal;

		return event;
	});

	// Events triggered while the record is in edit mode
	kintone.events.on(['app.record.edit.show', 'app.record.create.show', 'app.record.index.edit.show'], function (event) {
		// Restricting the input of the "Total" field
		event.record['Total'].disabled = true;

		return event;
	});
})();


custom4.PNG

Finally

We 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. You can avoid this error, by having the script check the contents of the fields first, and skipping the calculation if it does not contain numbers inside.

Was this article helpful?
0 out of 0 found this helpful
Comments
Please sign in to leave a comment.