Server Script / Client Script for Fetching details

Hello,

I created two doctypes and one child table “PCode”, “Products”, and “PCode Child Table” respectively.

Please find below SS for reference.

Now, I want to fetch “P Code” in child table inside doctype “Products” associated with exact matching Fileds One Two and Three.

Example : PCode created (PCODE-0005) using Material 1 , Product 1 and Name 1.

Now When I select same fileds inside child table of doctype “Product”, id or title of P Code should be fetch.

@NCP Please guide if its possible.

I tried below script.

use something like this

frappe.ui.form.on("Quotation", "refresh", function(frm) {
  frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
      var child = locals[cdt][cdn];
      //console.log(child);
      return {
          filters: [
              ['Item', 'item_group', '=', child.item_groups]
          ]
      };
  };
});
1 Like

Hi @falahtech,

Please check it.

frappe.ui.form.on('Products', {
    before_save: function(frm) {
        $.each(frm.doc.pcode_table,  function(i, d) {
            frappe.db.get_value("P Code", {"field_one": d.field_one, "field_two": d.field_two, "field_three": d.field_three}, "name", function(value) {
                d.p_code = value.name;
            });
        });
    }
});

Check the doctype, doctype field, and table field name your according.

Thank You!

3 Likes

Thanks for reply @NCP
It’s working absolutely fine.

But I have a small query, you used “before_save” but its getting update after clicking on “Save” button or after pressing “TAB” to go to next row in table.

Is there any method to fetch exactly after selecting values.

Hi @falahtech,

Please try it.

frappe.ui.form.on('Products', {
    before_save: function(frm) {
        update_vals(frm);
    }
});

frappe.ui.form.on('PCode Child Table',  {
    field_one: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        update_vals(frm);
    },
    field_two: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        update_vals(frm);
    },
    field_three: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        update_vals(frm);
    }
});

function update_vals(frm) {
    $.each(frm.doc.pcode_table,  function(i, d) {
        frappe.db.get_value("P Code", {"field_one": d.field_one, "field_two": d.field_two, "field_three": d.field_three}, "name", function(value) {
            d.p_code = value.name;
        });
    });
}

Check the doctype, child doctype name, doctype field, and table field name your according to.

Thank You!

2 Likes