我看过使用 De-Duping 工具的传统方法,但是因为我为一个多联盟的组织工作,所以当我实际上需要查看数据库的各个部分时,De-duping 工具会查看整个数据库,所以我决定尝试创建我自己的重复数据删除工具。
到目前为止,我已经创建了以下 apex 。顶点当前查看潜在客户上的公司名称,如果与数据库中的另一个公司名称完全匹配,它会向用户提供错误消息“另一个新潜在客户具有相同的公司名称”</p>
如果公司名称准确,那就太好了,但是我需要它更灵活。
例如,如果“汉堡王有限公司”是 2012 年创建的潜在客户,而卖家决定在 2013 年创建一个名为“汉堡王有限公司”的潜在客户,这与 2012 年创建的潜在客户是同一家公司。
我想建立一个模糊逻辑,查看新线索,如果有轻微相似之处,则忽略新线索
Trigger DuplicateLeadPreventer on Lead
(before insert, before update) {
//Get map of record types we care about from Custom Setting
Map<String, Manage_Lead_Dupes_C__c> leadrtmap = Manage_Lead_Dupes_C__c.getAll();
//Since only certain leads will match, put them in a separate list
List<Lead> LeadstoProcess = new List<Lead> ();
//Company to Lead Map
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : Trigger.new) {
//Only process for Leads in our RecordTypeMap
if (leadrtmap.keyset().contains(lead.RecordTypeId) ) {
// Make sure we don't treat an Company name that
// isn't changing during an update as a duplicate.
if (
(lead.company != null) &&
(Trigger.isInsert ||
(lead.company != Trigger.oldMap.get(lead.Id).company))
)
{
// Make sure another new lead isn't also a duplicate
if (leadMap.containsKey(lead.company)) {
lead.company.addError('Another new lead has the '
+ 'same company name.');
} else {
leadMap.put(lead.company , lead);
LeadstoProcess.add(lead);
}
}
} //end RT If Check
} //End Loop
/*
Using a single database query, find all the leads in
the database that have the same company address as any
of the leads being inserted or updated.
*/
Set<String> ExistingCompanies = new Set<String> ();
for (Lead l: [Select Id, Company from Lead WHERE Company IN :leadMap.keyset()
AND RecordTypeId IN :leadrtmap.keyset()]) {
ExistingCompanies.add(l.Company);
}
//Now loop through leads to process, since we should only loop if matches
for (Lead l : LeadstoProcess) {
if (ExistingCompanies.contains(l.company) ) {
l.company.addError('A lead with this company '
+ 'name already exists.');
}
}
}