Update Item in Quotation after JS using JS

Hi,
I would like to update an item description just after the other fields have been filed (item_name, description, price, amount, …). The purpose is to update the item description in the language of the customer.
This is the code I’ve done, which is working just fine, but my description is replaced by the ‘default’ right after (I suppose the Item JS haven’t finished running and overwrite my value when it’s done).

frappe.ui.form.on("Quotation Item", "item_code", function(frm, cdt, cdn) {
	var ct = locals[cdt][cdn]
	frappe.after_ajax(function() {
		frappe.call({
			method: "some/method/here",
			args:{
				customer_code: frm.doc.party_name,
				item_code: ct.item_code,
			},
          	callback: function(r) {
	       		frappe.model.set_value(cdt, cdn, "description", r.message);
	    	}
        })
	});
});

Does anyone have an idea?

@mel_erp What data would you like to filled in description field ?

Hi @avaiskhatri,
I’ve added a custom child table in Item like this:
Language - Link - Language | Description - Text Field
This way, I can change the description of an item based on the customer currency.
Let’s say the description is ‘Some desc 1234’. If my client is french, I want to change the description of the item for ‘Description XYZ 0987’ in this quote only.

Hi @mel_erp, You wanted to change the item description based on customer language right?

yes, and I don’t want to do it on validation or before_save()

How are you planning to change description based on language, will you use any language conversion tool or what?

I already have a child table in the item to manage that (see image). For now we added a custom button to do it and it’s working perfectly, but I would like to do it immediately without additional steps.

@mel_erp, hope this helps.

frappe.ui.form.on("Quotation Item", "before_save", function(frm,cdt, cdn) {
	var quotation_item = frappe.model.get_doc(cdt, cdn);
    var child_table = {};
    child_table.parent = quotation_item.item_name;
    child_table.language = quotation_item.language
    frappe.call({
            url: "/api/resource/[childtable]/",
            type: "GET",
            args: {
               data : child_table
            },
            callback: function(r) {
              //console.log(JSON.stringify(r));
              frappe.model.set_value(cdt, cdn, "description", r.item_description);
            }
       });
});

Thanks, but I would like to have a solution who can run right after choosing the item_code :slight_smile:
My variables and my python code work just fine.
The problem resides in the JS used to initialize some fields in the Item Row
ie
When you choose the item_code, it will fill automatically the item_name, the price, the amount, the description,… Now, when I try to change the description, it’s being overwritten by the JS used to do those things.
I was wondering if there were an option to allow me to change the description right after those changes have been made.

Without seeing actual code, Its dificult to qoute where you are making mistake.

I use the same JS as I gave in the description, as for the py method:

@frappe.whitelist()
def fetch_item_description(customer_code, item_code):
        description = "Hello World"
        return description

I got it with a timeout :slight_smile:

frappe.ui.form.on("Quotation Item", "item_code", function(frm, cdt, cdn) {
	var ct = locals[cdt][cdn];
	setTimeout(function(){
		frappe.call({
			method: "some/path/here",
			args:{
				customer_code: frm.doc.party_name,
				item_code: ct.item_code,
				},
        	callback: function(r) {
			frappe.model.set_value(cdt, cdn, "description", r.message);
			}
      	})
	}, 1000);
});