Fuzzy Logic / matching on Company Name (leads)

六月ゝ 毕业季﹏ 提交于 2019-12-11 06:48:12

问题


I have looked at conventional methods of using De-Duping tools however because I work for an organisation which is multi alliance the de-duping tool looks at the entire database, when I actually need to look at segments of the database, so I have decided to try and create my own de-duping tool.

So far I have created the following apex . The apex currently looks at the company name on the lead and if there is an exact match with another company name in the database it provide the user the error message ‘another new lead has the same company name”

This is great if the company name is exact, however I need it to be more flexible .

For example if “Burger King Limited” was a lead created in 2012, and a seller has decided to create a lead called “Burger King LTD” in 2013 which is the same company as the lead which was created in 2012.

I want to build a fuzzy logic, that looks at the new lead and if there is Slight resemblance then disregard the new lead

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

来源:https://stackoverflow.com/questions/16036795/fuzzy-logic-matching-on-company-name-leads

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!