ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

Customer name in child table field


#1

hello,

I have created new doctype named “Customer Items” and added table in “Customer”
which has following fields
I want the customer name fetched automatically in table data field"customer_full_name" whenever new row is added.

table name is “Customer Items”

I have written following script in “customer”
suggest the changes

frappe.ui.form.on("Customer", "refresh", function(frm, cdt, cdn){
   frappe.call({
        "method": "frappe.client.get_value",
         args: {
            doctype: "Customer",
   	        fieldname: "customer_name"		   			
            },								
         callback: function (data) {									 
                cur_frm.set_value("customer_full_name", data.message.customer_name);
   		}					
           })

});

thank you


#2

why don’t you make Customer full name field in your new DocType as ‘LINK’ field and attach it to customer name in the option window.


#3

thank You @FinForce
for reply

even when other fields of the table are different but customer full name field should be same as the customer name in which this table is created .

link field will give us drop down so every time i have to select the same customer name.
where as i want set it default and whenever new row is added the customer full name field should be same as customer name


#4

ok, I understand it now. I don’t know how to do it.

But, what is your UseCase for Customer Item under Customer docType? And what you plan to solve a problem by doing this?


#5

@FinForce
I want that table in quotation
for that perticular customer
lets consider if quotation for xyz customer is created then the table of that customer should be fetched in quotation
I have also created same table in quotation by creating new doctype.
bellow script is working but i have to place name of customer in customer doctype for each item
otherwise it will fetch all the rows of different different customer into quotation of that perticular customer.

  frappe.ui.form.on("Quotation","customer", function(frm, cdt, cdn) {   
   frappe.call({
    method: "frappe.client.get_list",
    args: {
       
        doctype: "Customer Items",
        filters:{
              "customer_name" : frm.doc.customer
        },


               fields: ["equipment", "serial_no", "manuf_year", "pressure"]
    },
  
    callback: function(r) {
            var items = [];
            frm.clear_table("customer_speacial");
            for(var i=0; i< r.message.length; i++) {
                var d = frm.add_child("customer_speacial");
	for (var key in r.message[i]) {
		    if (r.message[i].hasOwnProperty(key)) {           
		frappe.model.set_value(d.doctype, d.name, key, r.message[i][key]);
		    }
	}

                if(!d.qty) d.qty = 1;
            }
            frm.refresh_field("customer_speacial");
        }

})

})


#6

hi , you can try this :slight_smile:

i am writing this after bit of exploration

fabric_inspection_fabrics_add:function(frm,cdt,cdn){
var row=locals[cdt][cdn]
row.fabric=‘Akshay’
frm.refresh_field(“fabric_inspection_fabrics”)
},

actually every time any row gets added a particular method is triggered like above (adding screen shot )

fabric_inspection_fabrics --> this is my child table and “_add” if i suffix it and write method in a child table that method will be triggerd after adding row automatically you can do your hack with it like i did.

cheers :wink:


#7

thank you @AkshayJadhao

I am new for scripting could you please elaborate
with following details.

doctype: Customer
field to be fetch : customer_name

table name: customer_item (in customer)
doctype: Customer Items
field in which name to be fetched: customer_full_name(type=data)

customer itewm table is in customer doctype which has column named customer_full_name.
so in this field the customer_name should be fetched as shown in above screenshot.

I have tried the code you have suggested as follows.

add_new_row: functionidx, callback, show) {
 if(this.is_editable()) {
 if(this.frm) {
var d = frappe.model.add_child(this.frm.doc, this.df.options,this.df.customer_name, idx);
 d.__unedited = true;
this.frm.script_manager.trigger(this.df.customer_full_name + "_add", d.customer, d.name);  
 this.refresh();
   } else {
   this.df.data.push({name: "batch" + (this.df.data.length+1), idx: this.df.data.length+1});
   this.refresh();
 }

#8

hi try this

customer_item_add:function(frm,cdt,cdn){
    var row=locals[cdt][cdn]
    row.customer_full_name=frm.doc.customer
    frm.refresh_field(“customer_item”)
},

:slight_smile:


#9

thanks again @AkshayJadhao

I have tried the script, not working .
I want to pick the customer name and put it in the table it will same for all the items.
table is in “Customer Items” custom doctype, I have used it in customer form same as “Quotation Item” in “Quotation”.


#10

thank you guys

I got solution.

   frappe.ui.form.on("Customer Items", {
customer_item_add: function(frm) {
      var last_id = frm.doc.customer_item.length - 1;
frappe.model.set_value(frm.doc.customer_item[last_id].doctype, frm.doc.customer_item[last_id].name, "customer_name", frm.doc.customer_name);
   }
     });

#11