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’, doc.name, ‘fps’)
pbs = frappe.db.get_value(‘Skala Derajat Kualitas LOA’, doc.name, ‘pbs’)
klb = frappe.db.get_value(‘Skala Derajat Kualitas LOA’, doc.name, ‘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 (http://course.hackingofgod.com/assets/js/form.min.js?ver=1641190730.0:1:60267)
    at _f.Frm.setup (http://course.hackingofgod.com/assets/js/form.min.js?ver=1641190730.0:1:13114)
    at _f.Frm.refresh (http://course.hackingofgod.com/assets/js/form.min.js?ver=1641190730.0:1:19032)
    at frappe.views.FormFactory.load (http://course.hackingofgod.com/assets/js/form.min.js?ver=1641190730.0:1:11600)
    at http://course.hackingofgod.com/assets/js/form.min.js?ver=1641190730.0:1:11326
    at Object.callback (http://course.hackingofgod.com/assets/js/desk.min.js?ver=1641190730.0:1:115855)
    at Object.success [as success_callback] (http://course.hackingofgod.com/assets/js/desk.min.js?ver=1641190730.0:1:49254)
    at 200 (http://course.hackingofgod.com/assets/js/desk.min.js?ver=1641190730.0:1:49509)
    at Object. (http://course.hackingofgod.com/assets/js/desk.min.js?ver=1641190730.0:1:52001)
    at i (http://course.hackingofgod.com/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

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', frm.doc.name, 'fps');
        var pbs = frappe.model.get_value('Skala Derajat Kualitas LOA', frm.doc.name, 'pbs');
        var klb = frappe.model.get_value('Skala Derajat Kualitas LOA', frm.doc.name, '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