Quality Inspection - Sales Order Reference

We are making outgoing inspection from sales invocie (as it is set that quality inspection must be made before invoice) with reference type - sales invoice, reference name as sales invoice.
However, we also need a custom fielf that must show the sales order reference of the same invoice number.

can someone guide by custom script ?

First add the custom field like sales_order_reference in the Quality Inspection and add the client and server script.

frappe.ui.form.on('Quality Inspection', {
    reference_name: function(frm) {
        if(frm.doc.reference_type == 'Sales Invoice' && frm.doc.reference_name) {
            frappe.call({
                method: 'custom_app.api.get_sales_order_from_invoice',
                args: {
                    sales_invoice: frm.doc.reference_name
                },
                callback: function(r) {
                    if(r.message) {
                        frm.set_value('sales_order_reference', r.message);
                    }
                }
            });
        }
    }
});
import frappe

@frappe.whitelist()
def get_sales_order_from_invoice(sales_invoice):
    sales_order = frappe.db.get_value('Sales Invoice Item', {'parent': sales_invoice}, 'sales_order')
    return sales_order

Please set the method path according to the app. code tested so it works properly.

1 Like

Many thanks for such a fast response. I will try this and post a response shortly

This is something i am not able to understand that whether to install new app as “custom app” or new doctype or change code in method ?

WE were able to solve this with below script :

frappe.ui.form.on(‘Quality Inspection’, {
refresh: function(frm) {
if(frm.doc.reference_type == ‘Sales Invoice’){
if(!frm.doc.sales_order){
frappe.db.get_value(‘Sales Invoice’, frm.doc.reference_name, ‘sales_order’)
.then(r => {
if(r.message.sales_order) {
frm.set_value(‘sales_order’, r.message.sales_order);
frm.refresh_field(‘sales_order’);
}
});
}
}
},
onload: function(frm) {
if(frm.doc.reference_type == ‘Sales Invoice’){
if(!frm.doc.sales_order){
frappe.db.get_value(‘Sales Invoice’, frm.doc.reference_name, ‘sales_order’)
.then(r => {
if(r.message.sales_order) {
frm.set_value(‘sales_order’, r.message.sales_order);
frm.refresh_field(‘sales_order’);
}
});
}
}
},