[Newbie] My custom script does not seem to trigger anything :(

Hi there,

My goal is to compute a sub-total in the custom field “software_sub_total”.

As a first step, I just want to set a new value of the field “software_sub_total” when an item is changed in the items table of the quotation.

So I’ve added a new custom script on “Quotation” Doc Type:

frappe.ui.form.on("Quotation", {
    items: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        frappe.model.set_value(d.doctype, d.name, "software_sub_total", 37);
    }
});

But the field remains 0 (both in the form view and in the print command {{doc.software_sub_total}} )

I’ve looked on the forum and the docs, but something is wrong… and I need some help!

Your field, software_sub_total is in the Item table or the Parent table?

Anyway, am assuming the field is in the Parent form, try this

frappe.ui.form.on("Quotation Item", {
    item_code: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        frm.set_value('software_sub_total', 37)
    }
});

Hello,

If you’re trying to detect the change of the items child table, the code should be:

frappe.ui.form.on('The Child Table Doctype', 'the_child_table_field/trigger', function(frm, cdt,cdn){
	//code goes here
});

or in your case, the working code might be something like this:

frappe.ui.form.on("Quotation Item", 'amount', function(frm, cdt, cdn){
	var d = locals[cdt][cdn];
	//if software_sub_total is in the TABLE/Child Table
	frappe.model.set_value(d.doctype, d.name, "software_sub_total", 37);
	frm.refresh_field("items");//this will tell the UI to update the items table in your form
	//if it's in the MAIN/PARENT form/doctype
	frm.set_value("software_sub_total", 37);
});

Hope it helps.

try this one i think it will work

frappe.ui.form.on("Quotation Item ", {
    item_code : function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
      frappe.model.set_value(cdt, cdn, "software_sub_total",37);
    }
});

Hi,

Doesn’t work. But I’m wondering if the code is getting triggered:

I’ve add this code:

frappe.ui.form.on("Quotation Item", 'amount', function(frm, cdt, cdn){

  frappe.publish_realtime(event='eval_js', message='alert("{0}")'.format("foo"), user=frappe.session.user);
});

And the popup doesn’t show :frowning:

My code is in a new Custom Script with DocType=Quotation

My code is in a new Custom Script with DocType=Quotation

And I try to add/remove/change a row in the items table of my quotation, nothing happen there:

Remove comment from frm.refresh_field(‘items’); and try

No, I’ve tried each of:

frappe.model.set_value(d.doctype, d.name, "software_sub_total", 37);
frm.refresh_field("items");//this will tell the UI to update the items table in your form

or

frm.set_value("software_sub_total", 37);

can try using the rate field, instead of amount. Not sure if the amount field value change triggers event as its read only.

Yes! This is it! Thanks!