Custom Script is running but changing the status of form from Submitted to Update

Hi,

I have defined some custom fields, and have also checked on “allowed on submit” on them. And, I have this following custom script, which runs on the Salary Slip form. Though, the custom script is running and is setting the values on the submitted form, it changes the status of the document to “Not Saved” and requires an “Update” button to be pressed. What could be wrong?

frappe.ui.form.on(“Salary Slip”, “refresh”, function(frm, cdt, cdn) {

      frappe.call({
            method: "icp.api.get_leave_balance_on",
            args: {
               "employee": frm.doc.employee,
       "leave_type": "Casual Leave",
       "date": frm.doc.end_date
       
           },
               callback:function(r){
                       
                       if(r.message) {
             
                         frappe.model.set_value(cdt, cdn, "casual_leave_balance", flt(r.message));
             refresh_field("casual_leave_balance");
            }
                     }
         });


      frappe.call({
            method: "icp.api.get_leave_balance_on",
            args: {
               "employee": frm.doc.employee,
       "leave_type": "Privilege Leave",
       "date": frm.doc.end_date
       
           },
               callback:function(r){
                       
                       if(r.message) {
                                        
                         frappe.model.set_value(cdt, cdn, "earned_leave_balance", flt(r.message));
             refresh_field("earned_leave_balance");
            }
                     }
         });

frappe.call({
            method: "icp.api.get_approved_leaves_for_period",
            args: {
               "employee": frm.doc.employee,
       "leave_type": "Casual Leave",
       "from_date": frm.doc.start_date,
       "to_date": frm.doc.end_date
       
           },
               callback:function(r){
                       
                       if(r.message) {
             
                         frappe.model.set_value(cdt, cdn, "casual_leave_taken_this_month", flt(r.message));
             refresh_field("casual_leave_taken_this_month");
            }
                     }
         });

frappe.call({
            method: "icp.api.get_approved_leaves_for_period",
            args: {
               "employee": frm.doc.employee,
       "leave_type": "Privilege Leave",
       "from_date": frm.doc.start_date,
       "to_date": frm.doc.end_date
       
           },
               callback:function(r){
                       
                       if(r.message) {
             
                         frappe.model.set_value(cdt, cdn, "earned_leave_taken_this_month", flt(r.message));
             refresh_field("earned_leave_taken_this_month");
            }
                     }
         });

});

@UmaG

Try resubmitting document after frappe.model.set_value by cur_frm.save('submit');

Hi @Nick,

Thanks for responding back. I added this line and am getting this error now.
No permission to submit.

Regards
Uma

@UmaG

See whether you have permissions set to submit.

Try using “on_update” or “before_save” as trigger instead of “refresh”

frappe.ui.form.on("Salary Slip", "on_update", function(frm, cdt, cdn) {

Thanks @Mukesh_Variyani. It works. :slight_smile:
Though, the form is showing not saved after my custom script runs, if I press on save again, it goes to the submit option. So, it is expecting me to click on Save twice. Works for the time being.

Thanks
Uma

which one did you use “before_save” or with “on_update”?

I used before_save and also tried after_save.

after_save will certainly ask you to save again, before_save ideally shouldn’t ask to save again, unless there is some other calculation is getting triggered on refresh or on field change event of fields changed by the function.