ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

Example updating fetch_from after submission

It’s really annoying that you can’t update fetched values after a document is submitted… you can now!

This example updates fetched images on existing BOMs.

Server Script: Script Type = API, name = update_all_bom_images:

def fetch_from(doc):
    docstatus = doc.docstatus
    image = doc.get('image')
    doc.docstatus = 0
    doc.get_invalid_links()
    doc.docstatus = docstatus
    return image == doc.get('image')

for bom in frappe.get_all('BOM', filters={'docstatus': ['<=', 1]}):
    clean = True
    # Update TL BOM image
    bom_doc = frappe.get_cached_doc('BOM', bom['name'])
    bom_doc.flags.ignore_validate_update_after_submit = True
    clean = clean and fetch_from(bom_doc)
    # Update children
    for child in bom_doc.items:
        clean = clean and fetch_from(child)
    if not clean:
        bom_doc.save()
frappe.msgprint("Successfully updated BOMs")

Client Script for BOM Update Tool Form:

frappe.ui.form.on('BOM Update Tool', {
    refresh(frm) {
        frm.add_custom_button('Update BOM Images', function() {
            frappe.call({
                method: "update_all_bom_images",
                freeze: true
		    });
	    });
    },
})
2 Likes