Simple Math Function Code

Hi, I would like to create a simple math function in ERPNext. Here are my details:

ERPNext: v11.1.49 (master)
Frappe Framework: v11.1.44 (master)

Doctype: Skala Derajat Kualitas LOA
Field 1: fps
Field 2: pbs
Field 3: klb

The function would be resulting = (fps + pbs) / 2 - klb

How can I create a code with the above and execute it in my doctype?

I have created a custom script below but it did not work:

import frappe

def rumus_kv(doc, method):
# retrieve the values of the fields
fps = frappe.db.get_value(‘Skala Derajat Kualitas LOA’,, ‘fps’)
pbs = frappe.db.get_value(‘Skala Derajat Kualitas LOA’,, ‘pbs’)
klb = frappe.db.get_value(‘Skala Derajat Kualitas LOA’,, ‘klb’)
# add the fields together
result = (fps + pbs) / 2 - klb
# set the value of a third field to the result of the addition
doc.nilai_kv = result

PS: I created the code based on ChatGPT AI hehehe…

This is my error code:

SyntaxError: Cannot use import statement outside a module
    at init.setup (
    at _f.Frm.setup (
    at _f.Frm.refresh (
    at frappe.views.FormFactory.load (
    at Object.callback (
    at Object.success [as success_callback] (
    at 200 (
    at Object. (
    at i (

V11 doesn’t have server script support afaik, also it’s EOL. You should upgrade to v13 where you can add server side script for computing values.

Or v14 which has computed fields support using simple formulas without any code.

Or create an app and hook into document before save.

Or write JS version but it will only execute on form.

Thank you. Is it safe to upgrade from V11 to V14?

Looking at your doctype, the fields are all in the same Form for the same document. Why don’t you use js instead? So it doesn’t need server script.

Hello. Do you have any clue on the code script would look like in jS?

I try ChatGPT for an answer and it worked…

1 Like

Hey Ankush!

I looked up the simple formulas in v14 and did not find documentation for it. As it is a new feature, could you guide me on how this feature can be used?

Thank you.

This is what ChatGPT AI script result:

frappe.ui.form.on('Skala Derajat Kualitas LOA', {
    refresh: function(frm) {
        var fps = frappe.model.get_value('Skala Derajat Kualitas LOA',, 'fps');
        var pbs = frappe.model.get_value('Skala Derajat Kualitas LOA',, 'pbs');
        var klb = frappe.model.get_value('Skala Derajat Kualitas LOA',, 'klb');
        frm.set_value('nilai_kv', [(fps + pbs) / 2] - klb);

Also adding the code in the doctype nilai_kv field:

def nilai_kv(doc):
    doc.nilai_kv = [(doc.fps + doc.pbs) / 2] - doc.klb