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?
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.
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.
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.
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: