Javascript Code not working

Hi Everyone,
I have 2 fields of type data.

Below is my code, which executes whenever I change the value in the field but I am not able to find the reason that why the second code runs only once I change the value in the textbox whereas the first runs every time I change the value in the textbox.

frappe.ui.form.on("Sales Order", "club_uf_mod_x_qty", function(frm, doctype, name)
{
msgprint("Mod X");
if(frm.doc.club_uf_mod_x_qty<0)
{
msgprint("Please enter a non-negative value");
cur_frm.set_value("club_uf_mod_x_qty",0);
cur_frm.refresh(); 
validated=false;
}


  for (x = 0; x <clubuf_mod_x_data.length; x++)
  {    
     var tbl = frm.doc.items || []; 
      var i = tbl.length;
      //for(var i = 0; i < tbl.length; i++)
    while (i--)
      {
            if(tbl[i].product_type == "CLUB (U.F.) - Mod ^")
            {
               cur_frm.get_field("items").grid.grid_rows[i].remove()
            }
     }
 } 
cur_frm.refresh(); 
clubuf_mod_x_data=[];
frm.trigger("club_uf_mod_x");

});

frappe.ui.form.on("Sales Order", "club_uf_mod_y_qty", function(frm, doctype, name)
{
msgprint("Test");
if(frm.doc.club_uf_mod_y_qty<0)
{
msgprint("Please enter a non-negative value");
cur_frm.set_value("club_uf_mod_y_qty",0);
cur_frm.refresh(); 
validated=false;
}

  for (x = 0; x < clubuf_mod_y_data.length; x++)
  {       
     var tbl = frm.doc.items || []; 
     var i = tbl.length;
     while (i--)
      {
            if(tbl[i].product_type == "CLUB (U.F.) - Mod v")
            {
               cur_frm.get_field("items").grid.grid_rows[i].remove()
            }
     }
 } 

cur_frm.refresh(); 
clubuf_mod_y_data=[];
frm.trigger("club_uf_mod_y");
});

Any idea about this?

Regards
Ruchin Sharma

Try wrapping the second code with the below code

frappe.after_ajax(function() 
{ //your code }

@Muthu
You mean under this:

frappe.after_ajax(function() 
{ //your code })

instead of this:

frappe.after_ajax(function() 
{ //your code }

right?

@Muthu
I am not sure, I followed in the same way that you told but it is still not working:

frappe.after_ajax(function() 
{ 

frappe.ui.form.on(“Sales Order”, “club_uf_mod_y_qty”, function(frm, doctype, name)
{
msgprint(“Mod Y”);
if(frm.doc.club_uf_mod_y_qty<0)
{
msgprint(“Please enter a non-negtive value”);
cur_frm.set_value(“club_uf_mod_y_qty”,0);
cur_frm.refresh();
validated=false;
}
for (x = 0; x < clubuf_mod_y_data.length; x++)
{
var tbl = frm.doc.items || [];
var i = tbl.length;
//for(var i = 0; i < tbl.length; i++)
while (i–)
{
if(tbl[i].product_type == “CLUB (U.F.) - Mod v”)
{
cur_frm.get_field(“items”).grid.grid_rows[i].remove()
}
}
}
cur_frm.refresh();
clubuf_mod_y_data=[];
frm.trigger(“club_uf_mod_y”);
});
})

@Muthu
Thanks for your efforts its done now.

My bad, I missed something in my main code.

Regards
Ruchin Sharma

1 Like