Child table value Duplication

Hi, I need a help.
Am having a table with check box while Am selecting the box and save it the data need to be fetched to the below table but the data is getting stored again and again for every save. So, now how to stop this duplication of data in child table?

	validate: function(frm,cdt,cdn) {
		
		
		var a = [];
		$.each(frm.doc.table_2, function(index, source_row){

	
			globalThis.add_child = frm.add_child("neww");
			globalThis.Blockno = "";
			if(source_row.check_6 == true){
				a.push(source_row.q_block_no + ",");
				
			}
			a.forEach(hello);
			function hello(va){
				Blockno += va  
			
			}

		})
		add_child.q_block_no = Blockno;

	},

Hi @VINOTH,

Please check it.

Maybe it’s helpful for you.

Thank You!

1 Like

Hi, @NCP
Nope this is not working in my case :frowning_face:
Could you help me out in my case please :raised_hand_with_fingers_splayed:.

Thank you.

Hi @VINOTH,

Please try it.

frappe.ui.form.on('Child Table Name', {
    duplicate_field_name: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        $.each(frm.doc.table_name, function(i, row) {
            if (row.duplicate_field_name === d.duplicate_field_name && row.name != d.name) {
               frappe.msgprint('You added already exists on the table.');
               frappe.model.remove_from_locals(cdt, cdn);
               frm.refresh_field('table_name');
               return false;
            }
        });
    }
});

Thank You!

2 Likes

I haven’t idea about your scenario.
I just share the code to stop duplication. so apply the code according to your requirement.

Thank You!

Okay.

Thank you.

How can we achieve this using Server Script?

Something similar logic has to be set up.

Hi @umarless,

Please apply the server script.
DocType Event → Before Save

Scenario 1: If you want to set the warning then you can apply it.

items = doc.get("items")
item_codes = set()

for row in items:
    if row.item_code in item_codes:
        frappe.throw(f"Duplicate entry found: {row.item_code}")
    item_codes.add(row.item_code)

Scenario 2: If you want to remove the duplicate row then you can apply it.

items = doc.get("items")
unique_items = []
item_codes = set()

for row in items:
    if row.item_code not in item_codes:
        item_codes.add(row.item_code)
        unique_items.append(row)

doc.set("items", unique_items)

Please set the table name and field name according to the scenario.

Hope this helps!

1 Like