Custom script does not work sometimes

Hi Guys ,

The script below works only if I change the production_item field thrice , at sometimes it works instantly , could someone throw light on this ,whats wrong with the script ? I checked in the console , whenever there is a change in the production_item field it returns a value and I could see that in the console log , but it does not set the value in the dieno field , for which I have to change the production_item field thrice .

frappe.ui.form.on("Production Order", "production_item", function(frm, cdt, cdn){
frappe.call({
       "method": "frappe.client.get_value",
        args: {
		doctype: "Product master",
		fieldname: "dieno",
		filters: {
		name:["=", frm.doc.production_item]
			 }           			
	      },								
	      callback: function (data) {									
	      console.log(data);
              cur_frm.set_value("dieno", data.message.dieno);
				        }					
	     })

});

Thanks in advance

Maybe that are some other events being fired on change of Production Item that also involve server calls. The whichever event completes first, overrides the other. The way to do this would be either to add a timeout or wrap your event in

frappe.after_ajax(function() { 
  // second call 
});
1 Like

It works like a charm @rmehta . You are awesome . Many thanks for the help my friend .

hii @rmehta
what if im having many frappe.call attached to a single event ? can i wrap all those frappe.calls with frappe.after_ajax() ? What is it doing actually ? queuing those functions ? so all server calls are done linearly ? :slight_smile:

Thanks

Will just make sure there is no pending AJAX queries before the callback

So what will happen if there are multiple frappe.after_ajax() waiting ? each will be executed one by one ?

I’ve a set_query function for a child table field in my javascript. I’m also stuck with the same issue. It sometimes runs sometimes doesn’t. The solution here is not working for me.