Custom Script is not working for me. How to solve it?

I have followed the tutorial found here.

I have built two Custom Scripts.

  1. Client Script
  2. Server Script

But the scripts are not working for me and there are no errors generated either on server or in browser.

Here is the code of Client Script

frappe.ui.form.on('My WOrk Order', {
    bomtemp: function(frm){

    let bomtemp = frm.doc.bom_template;
    console.log(bomtemp);
    if(bomtemp){
        frappe.call({
         method: "mymfg.api.get_processes",
         args: {bomtemp: bomtemp}
    }).done((r) => {
    
            frm.doc.operations = []
            
            $.each(r.message, function(_i, e){
            let entry = frm.add_child("operations");
            entry.sequence_id = e.sequence_id;
            entry.operation = e.operation;
        
        })
        refresh_field("operations")
    
    })
 }

}
})

Here is the code of Server Script

@frappe.whitelist()
def get_processes(bomtemp):
    bom_operations = frappe.db.sql(f""" SELECT name FROM `tabMy BOM Operation` WHERE `parent`='{bomtemp}' """, as_dict=True)
    return bom_operations

Can someone spot my mistake and help correct it?

Regards,

1 Like

Hi @yogeshvachhani,

Please try it.

frappe.ui.form.on('My Work Order', {
    bomtemp: function(frm){
        let bomtemp = frm.doc.bom_template;
        if(bomtemp){
            frappe.call({
                method: "mymfg.api.get_processes",
                args: {bomtemp: bomtemp}
            }).done((r) => {
                // frm.doc.operations = [] //if not worked then remove the comment.
                $.each(r.message, function(i, e){
                    let entry = frm.add_child("operations");
                    frappe.model.set_value(entry.doctype, entry.name, 'sequence_id', e.sequence_id);
                    frappe.model.set_value(entry.doctype, entry.name, 'operation', e.operation);
                });
            })
        }
    }
});

Check the field name and doctype name

Thank You!

@NCP ,

Thanks for the suggestion.

My Client Script has error. The 2nd line should be bom_template instead bomtemp.

The second mistake was that I was putting the Server Script in the Server Script section in ERPNext. Actually I have to put this code in a .py file on the server.

What I did was create a file names api.py in mymfg folder and put the code of Server Script in it and how things are working properly.

Here is the complete code of Client Script and Server Script that is working.

Client Script

frappe.ui.form.on('My WOrk Order', {
    bom_template: function(frm){
    console.log(frm);
    let bomtemp = frm.doc.bom_template;
    //console.log(bomtemp);
    if(bomtemp){
        frappe.call({
         method: "mymfg.api.get_processes",
         args: {bomtemp: bomtemp}
    }).done((r) => {
    
            frm.doc.operations = [];
            $.each(r.message, function(_i, e){
            let entry = frm.add_child("operations");
            entry.sequence_id = e.sequence_id;
            entry.operation = e.operation;
            entry.source_warehouse = e.source_warehouse;
            entry.target_warehouse = e.target_warehouse;
            entry.workstation = e.workstation;
            entry.completed_qty = 0;
            entry.time_in_mins = 1;
        })
        refresh_field("operations")
    
    })
 }

}
})

Here is the Server Script saved inside api.py file on server.

from __future__ import unicode_literals
import frappe
import json
from frappe.utils import floor, flt, today, cint
from frappe import _

@frappe.whitelist()
def get_processes(bomtemp):
 bom_operations = frappe.db.sql(f""" SELECT sequence_id, operation, workstation, source_warehouse, target_warehouse, perform_qc, quality_inspection_template FROM `tabMy BOM Operation` WHERE `parent`='{bomtemp}' ORDER BY idx """, as_dict=True)
 return bom_operations

Once again thank you for lending a helping hand.

Regards,

2 Likes