模糊逻辑/匹配公司名称(线索)

时间:2013-04-16 12:14:21

标签: apex fuzzy-logic

我已经研究过使用De-Duping工具的常规方法,但是因为我为一个多联盟的组织工作,重复数据删除工具查看整个数据库,当我实际需要查看数据库的各个部分时,所以我决定尝试创建自己的重复数据删除工具。

到目前为止,我创建了以下顶点。 apex目前查看了公司名称,如果与数据库中的其他公司名称完全匹配,则会向用户提供错误消息“另一个新的公司名称具有相同的公司名称”

如果公司名称确切,这很好,但我需要它更灵活。

  
    

例如,如果“汉堡王有限公司”是2012年创建的领导者,并且卖方决定在2013年创建一个名为“Burger King LTD”的领导者,该公司与2012年创建的领导公司相同。< / p>   

我想建立一个模糊逻辑,看看新的领导,如果有轻微的相似性,那么忽视新的领导

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.');
        }
    }
}

0 个答案:

没有答案