Hi All,
I have four fields, Type, Serial Number, Part Number and Model on Item master. Now, I wrote a client script that validates the uniqueness of these fields. The user cannot save the Item master if the values in each field has been entered for another item. The user can only save the Item master if (a) the value does not exist for another item or (b) the value is ‘None’.
Here is the client script below. The problem is it works for one field and not for others, or another time it doesn’t just work. Please help me:
frappe.ui.form.on(‘Item’, {
validate: function (frm) {
const fields = [‘type’, ‘serial_number’, ‘part_number’, ‘model’];
frappe.call({
method: 'frappe.client.get_list',
args: {
doctype: 'Item',
fields: fields,
filters: [
['name', '!=', frm.doc.name]
]
},
callback: function (response) {
if (response.message) {
const items = response.message;
const errorMessages = [];
fields.forEach(field => {
if (frm.doc[field] && frm.doc[field] !== 'None') {
const isDuplicate = items.some(item => item[field] === frm.doc[field]);
if (isDuplicate) {
errorMessages.push(${field.replace(/_/g, ' ').toUpperCase()} must be unique.);
}
}
});
if (errorMessages.length > 0) {
frappe.msgprint({
title: __('Validation Error'),
message: errorMessages.join('<br>'),
indicator: 'red'
});
frappe.validated = false;
}
}
}
});
}
});