Save Button doesn't show when Custom Script Updates field

Hello, I’m writting a custom script for a custom Doctype, this script adds a button, which makes a call to a server side method, whcih updates a Table field in the form. And It works, It updates the Table field with the data I want.
My problem is that It doesn’t show the Save button when I do it, so I can’t save it by clicking. If i do frm.save() It works, and the data is stored, but that’s not what I want, I want to update the field, the form should have (and actually does) unsaved changes and I should be able to save.

Here’s my code:

frappe.ui.form.on('Transportation Trip', {
    state(frm) {
        // frm.refresh()
    },
    onload_post_render(frm) {
        frm.get_field("stops").grid.set_multiple_add("stop");
        frm.get_field("packages").grid.set_multiple_add("package");
    },
    refresh(frm) {
        // Custom button to autofill the stops
        if (['planned', 'loaded'].includes(frm.doc.state)) {
            // This is the button causing the issues
            frm.add_custom_button(__("Autofill Stops"), () => {
                frm.call('autofill_stops')
                    .then(r => {
                        if (r.message) {
                            frm.refresh();
                            console.log("The response is: ", r);
                        };
                    })
            });
            frm.add_custom_button(__("Quick Package entry"), () => {
                dialog.show();
            });
        }

    },
})

The server side method being called:

    def autofill_stops(self):
        """Get the unique destination/stops for the packages
        in the Delivery Trip"""
        package_dest = {p.destination for p in self.packages}
        # If there's no stop set It will throw and exception
        try:
            stops = {s.stop for s in self.stops}
        except Exception as e:
            stops = set()
        mising_stops = package_dest - stops
        for stop in mising_stops:
            self.append('stops', {
                       'doctype': 'Transportation Trip Stop',
                       'stop': stop
                       })
        return True

I didn’t find out what the big was. But managed to show the save button by setting the form as dirty I red the docs and found frm.is_dirty() and also by playing in the console, since it’s not in the docs, you can set frm.dirty() and It will be set as dirty, and the save button will show up.

2 Likes