Having trouble settings field values in new doc

Hello, I have been trying to set two field values using a frappe.call to get the values. However, only one of the two values gets submitted to the db on save.

frappe.ui.form.on(cur_frm.doctype,"folder_name", function(frm) {
	frappe.call({
		method: "get_next_revision",
		doc: frm.doc,
		callback: function(r){
			frappe.model.set_value(frm.doc.doctype, frm.doc.name, "revision", r.message);
		}
	});
	frappe.call({
		method: "get_next_tab",
		doc: frm.doc,
		callback: function(r){
			frappe.model.set_value(frm.doc.doctype, frm.doc.name, "tab", r.message);
		}
	});
	show_alert('Updating', 5);
})

This does get called when the folder_name changes. The values show-up on the new doc, but only one value is saved to the db.

Please advise. Thank you,

This is because of asynchronous behaviour of javascript. You should set both values in a single server call, it will also save time. Otherwise, you have to write 2nd frappe.call on callback of the 1st one.

You can consolidate the two function into one and call it in one frappe.call to avoid asynchronous problem in Javascript.

Thank you, I will investigate. I did see the variables updates after the call. Seemed like it was being deferred.

Try this:

frappe.ui.form.on(cur_frm.doctype,"folder_name", function(frm) {
	frappe.call({
		method: "get_next_revision",
		doc: frm.doc,
		callback: function(r){
			frappe.model.set_value(frm.doc.doctype, frm.doc.name, "revision", r.message);
		}
	});

        // call this after the first request ends
        frappe.after_ajax(function() {
    	  frappe.call({
		method: "get_next_tab",
		doc: frm.doc,
		callback: function(r){
			frappe.model.set_value(frm.doc.doctype, frm.doc.name, "tab", r.message);
		}
	  });
	  show_alert('Updating', 5);
        });
})
1 Like

@rmehta Thank you,

Does the frappe.after_ajax get fired after every frappe.call? Could I put more calls after “get_next_tab”?

Due to speed and efficiency I probably would not do that but just curious. Thanks again,

Yeah, it maintains its internal queue

Sure!

@rmehta Thank you again, I consider this question closed.