Trim value in custom field on Sales Order

We need to trim a value which we scan into a custom sales order field (tracking number) as the postal service has more information in the barcode than we need.

Example when we scan the 3d barcode we get this in the field:

ABCDEFG-Useful Value-LKKJHK89792873

We want only the part “Useful Value” to save in that field - the length of the useful data we don’t need is always consistent (ie from position 10 to position 22 in the data string).

Can anyone tell me the custom script syntax to do this?

Thanks.

@Mattsnow Subscribed for having the same problem.

You could use a validate hook to process that field. If the format is consistent like that, and will remain so, try the following.

If the string has a “-” character to demarcate the useful value, you could use:

tracking_number = tracking_number.split("-")
if len(tracking_number) > 1:
    tracking_number = tracking_number[1]  # to retrieve the "Useful Value"

otherwise,

tracking_number = tracking_number[10:22]  # this will not include the 22nd character

will work.

Although you should consider adding another read-only field that stores the “Useful Value” part of the code, instead of modifying the tracking number in-place.

Hey Rohan,

Thank you for this information. Just so everyone knows Auspost is moving to QR Codes hence why i am asking the original question as i am still learning ERP.

Here is a tracking code from Auspost 500g Bag
01993126509999989100030400318640060009998008180612153033

The digits within the stars is the actual tracking number, the addition digits must be for internal use.

Thanks Matt

Hi @RohanB I tried the following script in my sales order custom script:

frappe.ui.form.on('Sales Order', 'before_save', function(frm){
    if len(tracking_no) > 18{
       cur_frm.set_value("tracking_no", tracking_no[19:38]());
    }
});

But it doesn’t do anything - I put a value in sales_order.tracking_no which is 50 characters long but even when saved it doesn’t change. I also tried ‘validate’ in the frappe.ui.form.on but that doesn’t work either.

Could you give me some pointers please.

@Mattsnow

Sorry I wasn’t clear about this. The suggestion I made was to be done on the server-side (in a Python file). Client-side (the Javascript that you wrote) doesn’t have the len function or the ability to slice strings like that.

If you DO want to do this in Javascript, you could write something like:

frappe.ui.form.on('Sales Order', 'validate', function(frm){
    if (frm.doc.tracking_no.length > 18) {
       frm.set_value("tracking_no", frm.doc.tracking_no.slice(19, 38));
    }
});

Hope this helps.