Hi Community !!
My client wants ID name in lead doctype like AAB24AD001-0001(AAB.YY.AD.###-####).
AAB - common letter.
24- year last 2 digit.
AD- first 2 letter of lead_owner in Capital.
001- lead owner generation number.(include only that particular lead)
0001 - universal number (include all lead)
This below code generating like this :
For eg:
- Admin created 2 lead AAB24AD001-0001 & AAB24AD002-0002.
- Syed created 2 lead AAB24SY001-0003 & AAB24SY002-0004.
My issue :
If i delete the syed created doc AAB24SY001-0003 and Syed try to create new one it generate a sequence AAB24SY002-0004 instead of AAB24SY003-0005.
So anyone give me suggestion.
frappe.ui.form.on('Lead', {
onload: function(frm) {
if (!frm.doc.custom_name) { // Check if the custom_name field is empty
generate_custom_name(frm);
}
}
});
function generate_custom_name(frm) {
const year = new Date().getFullYear().toString().slice(-2); // Last 2 digits of the year
const ownerInitials = frm.doc.lead_owner ? frm.doc.lead_owner.substr(0, 2).toUpperCase() : 'XX'; // First 2 letters of lead_owner
const prefix = 'AAB';
// Construct the base name
const base_name = `${prefix}${year}${ownerInitials}`;
// Fetch the next sequence number for this base_name
frappe.db.count("Lead", {
filters: {
custom_name: ["like", `${base_name}%`] // Use the custom_name field
}
}).then(count => {
const seqNumber = count + 1; // Increment for the next number
// Get the global unique number based on existing leads
return frappe.db.count("Lead").then(globalCount => {
const uniqueNumber = globalCount + 1; // Unique number across all leads
// Set the custom_name field directly
const generatedName = `${base_name}${String(seqNumber).padStart(3, '0')}-${String(uniqueNumber).padStart(4, '0')}`;
frm.set_value('custom_name', generatedName);
});
}).catch(err => {
console.error('Error fetching count:', err);
});
}