ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

How to store the data from custom field and show it in specific pattern in the list

Hey ERPNEXT Fans,

I have one custom field and i would like to store and show the data in different pattern.

I am from Supply Chain Industry. For air transport, airlines issue an AWB number.
AWB number consist of 11 Digit. Usually its shown in below pattern.

eg. 607-22894152

First 3 digits are Airline Prefix, its assigned to one specific airline worldwide (It represents the airline name).
Next 7 digits are the sr. number of AWB.
Last 1 digit is a check digit which validates the AWB number (correct / incorrect).
Formula to calculate the check digit is here https://en.wikipedia.org/wiki/Air_waybill

My Requirements:
I have made a custom field “MAWB#” (type:int) and i want user to put the MAWB number in this. Once user put this number in to field (regardless of pattern xxx-xxxxxxxx or xxxxxxxxxxx), script should validate if this is the correct AWB number and store/show in the given pattern (xxx-xxxxxxxx). If the MAWB number is incorrect, it should throw an error that MAWB number is incorrect.

Upon successful validation, it should populate the “Airline name” in the next custom field “Airline name” (field type: You suggest) automatically (without saving the docs)

Let me know how to achieve this.

Hi @Himanshu_Gupta

you can achieve this with a custom/client-script and a call to python function for calculation/validation.

example custom/client-script:

frappe.ui.form.on("DocType", "MAWB#”", function(frm){
    if ((frm.doc.MAWB#”).length === 12) {
        frappe.call({
            method: 'path.to.function',
            args: { 'number': frm.doc.MAWB#”},
        });
    }
}); 

in python:

@frappe.whitelist()
def air_waybill(number):
    # calculation/validation here

in the python function you can throw a message with frappe.throw() if the number is wrong
or if the number is correct then you could select the airline name and return it to frontend and do frm.set_value("target_field", air_line_name) in the callback of the custom/client-script

hope this can help you

1 Like

Thanks a lot, Let me check this Manu.