In product table once i add product that product not add again

I created customer doctype, on that doctype i link child table(customer selling)
in customer selling table,i have product name field. when i add product 1st time is add and 2’nd time is also add but i want that product add only one time. please give me suggestion how to do that or what script i have to use

Hello @Kaushal_Khute, welcome! It sounds like you may be duplicating development; in what way does the Customer Details table on the Item doctype not meet your needs?

It is likely that you’d need to implement this de-duplication logic in either case. Here’s how it’s implemented in ERPNext: erpnext/utils.py at develop · frappe/erpnext · GitHub

@tmatteson That product add multiple times i want that product add only 1 time i have write code in python file or JS file

@tmatteson i use this code frappe.ui.form.on(“Custom”, “validate”, function(frm) {
if(frm.doc.__islocal)
frappe.call({
method: “frappe.client.get_value”,
args: {
doctype: “Customer Product”,
fieldname: “name”,
filters: {
// item_code: frm.doc.item_code,
// price_list: frm.doc.price_list,
product_name: frm.doc.product_name
}
},
callback: function(response) {
var product_name = response.message;
if (product_name) {
frappe.msgprint(“The same Product with the same price list already exists”);
validated=false;
return false;

                 }
            }
});

});

@Kaushal_Khute The callback is the hard part, you’re going to be able to get this figured out. You won’t need it if the only data that is required for your validation is contained in this document (which is what I understand it to be). If that’s not the case, please expand the user story.

To solve this problem on the client side, here are the steps I would take to flesh it out:

  • de-duplicate your child table based on the required key (item_code in my example)
  • if the length of frm.doc.items is greater than the deduplicated Set (cast to an Array)
  • print the message like you are

I tested this with Sales Order items.

const item_codes = new Set(frm.doc.items.map(row => row.item_code))
if (Array(item_codes).length < frm.doc.items.length) {
    frappe.msgprint("Duplicate item codes")
}

can you plz send detail code this code not working

I can’t supply working code without more information. What I have provided is not intended to be a specific solution for your problem, but an example that you can adapt to your use case. If you just need a solution and learning the Frappe framework isn’t critical at this time, I would recommend getting it touch with a service provider.

In general, the more time you are willing to devote to describing your problem(s), the more likely that 1) you’ll find a solution through that process (rubber ducking) or 2) you’ve given an engaging enough story that someone is willing to volunteer their time to help. What is your goal at this time?

ok thank you

@tmatteson i want to fetch my child table value(item_name)and print console.log how to that can you plz help me

OK, I need some additional details to help further.
What doctype are you writing this feature in?
What is the name of the child table field?
What do you mean by fetch? Where is the data now? Where is it going (I think you answered this part - frm.doc.child_table_fieldname[idx].item_code)?

frappe.ui.form.on(“Customer”, {
validate: function(frm){
if(cur_frm.doc.refer_item_no){
console.log(“refer_item_no is true”);
console.log(“hello”)
var item_code = new Set(frm.doc.Corder.map(row => row.item_code))
console.log(“item_code”)
if (Array(item_code).length < frm.doc.Corder.length) {
frappe.msgprint(“Duplicate item codes”)
}
}

}

})

@tmatteson this is my client script Customer → parent doctype and Corder ->Child Doctype
item_code->is field of Child Doctype
i want if i add duble item code in table the showing error

i try to using console.log for what value they exactly getting but noting print in console

can you help me plz

Not knowing the structure of the refer_item_no child table, which contains the child doctype Corder, this is my best guess at a working script.

frappe.ui.form.on("Customer", {
	validate: frm => {
        console.log("validate")
		if (frm.doc.refer_item_no.length > 0) {
			console.log("entries exist in the the refer_item_no table");
			let item_codes = new Set(frm.doc.refer_item_no.map(row => row.item_code))
			console.log("Item codes in refer_item_no", item_codes)
			console.log(Array(item_codes).length, frm.doc.refer_item_no.length)
			if (Array(item_codes).length < frm.doc.refer_item_no.length) {
				frappe.msgprint("Duplicate item codes")
			}
		}
	}
})

frappe.ui.form.on(“Customer”, {
validate: frm => {
console.log(“validate”);
if (frm.doc.customer_pro.length > 0) {
// console.log(“entries exist in the the customer_pro table”);
let item_name = new Set(frm.doc.customer_pro.map(row => row.item_name));
console.log(“Item Name in customer_pro”, item_name);
console.log(Array(item_name).length, frm.doc.customer_pro.length);
if (Array(item_name).length < frm.doc.customer_pro.length) {

			frappe.msgprint("Duplicate Item Name",(item_name).length);
			frm.refresh_field("item_name");

		}else{
		    
		    frappe.msgprint("save data");
		}	
		
		
			
		
	}
}

})

@tmatteson plz help me if i delete duplicate value that record is save but when i add diff value they showing duplicate item name

frappe.ui.form.on(“Customer”, {
validate: frm => {
console.log(“validate”)
if (frm.doc.customer_selling_.length > 0) {
// console.log(“entries exist in the the customer_pro table”);
const product_name = new Set(frm.doc.customer_selling_.map(row => row.product_name))
console.log(“Item Name in customer_pro”, product_name)
console.log(Array(product_name).length, frm.doc.customer_selling_.length)
var prod = frm.doc.customer_selling_
let k = []
console.log(prod)
for (var i =0; i < frm.doc.customer_selling_.length; i++)
{
console.log(prod[i][‘product_name’])
k[i] = prod[i][‘product_name’]
}
const myset = new Set(k)
console.log(myset.size)
if (myset.size < frm.doc.customer_selling_.length) {

			frappe.msgprint("Duplicate Item Name")
			
			
		}else{
		    console.log(frm.doc.customer_selling_.length)
		    frappe.msgprint("save data")
		}	
		
		
			
		
	}
}

})