Trying to explain strange behaviour

Hi

I have created a little script to populate a “custom_po_barcode” field after save. And
in fact, after the save, the barcode does appear on the PO. However, there is also a
work-flow linked to PO and after the first save, when the custom_po_barcode field is
populated , the document must be saved again since new data has been input. The workflow “save” is then performed and this clears the custom_po_barcode field again ???

My little script is below. The reason why I populate the custom_po_barcode field
“after_save” is to allow the system to assign a document number.

Why would the workflow-save clear the “custom_po_barcode” field?

Edit: OOps forgot my code

frappe.ui.form.on('Purchase Order', {
    after_save: function(frm) {
        debugger;
        if (!frm.doc.custom_po_barcode) {
            debugger;
            frm.set_value("custom_po_barcode", frm.doc.name);
            frm.refresh_field("custom_po_barcode");
        }
    }
});

Please try it.

frappe.ui.form.on('Purchase Order', {
    before_workflow_action: function(frm) {
        if (!frm.doc.custom_po_barcode) {
            frm.set_value("custom_po_barcode", frm.doc.name);
            frm.refresh_field("custom_po_barcode");
        }
    }
});

Thank you @NCP

I have tried it. The custom_po_barcode field briefly populates with the barcode when I click the “save” in workflow, but the field is then cleared again. I am also monitoring the
database and the database table is not updated at any point.

It seems to be the workflow-logic that is clearing it ???

I then tried something else … just add a frm.save() …???

frappe.ui.form.on('Purchase Order', {
    after_save: function(frm) {
        debugger;
        if (!frm.doc.custom_po_barcode) {
            debugger;
            frm.set_value("custom_po_barcode", frm.doc.name);
            frm.refresh_field("custom_po_barcode");
            frm.save();
        }
    }
});

Is this acceptable way to do it ?

I tried it once and it seem to work. Will test again to check.

I am just wondering why the workflow logic would interfere with a field on a doc.
a Bit of a worry because it means that , if you save a doc that has workflow and after
the 1st save, you modify a field, it may not save that last modification ???
Will have to check it. Is this a bug ???

Thank you for your time @NCP

For that case, you have to use the server script instead of the client script. please check and apply it.

Thank you @NCP for your message.

I tried your suggestion, but I get the same result : the field clears when I do the workflow “save”.

I had another look at my workflow-definition to see if there could be anything wrong
but it is a rather straighforward workflow ?

BTW I updated the server recently…

ERPNext: v15.40.0 (version-15)

Frappe Framework: v15.46.0 (version-15)

Payments: v0.0.1 (develop)

You’ve mixed up the flow. If you’re using a workflow, you need to set it according to the workflow state. If you’re using the regular approach, you can set the logic without needing the workflow.

I used “after save”, and checked so the value update in the field, no need to save multiple times.

Thank you @NCP for your help.
Let me review my script and my workflow.
Thank you