• Dharmesh Maheshwari
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 3
    Replies
global class removeDuplicateRecords implements Database.Batchable<SObject> 
{ 
    Global Map<String,Lead> EmailLead = new Map<String,Lead>();
    global Map<String,List<String>> mapEmailName = new Map<String,List<String>>();
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Email,Name,Company,Description from Lead where Email != null';
        return Database.getQueryLocator(query); 
    }
    
    global void execute(Database.BatchableContext BC,List<Lead> scope)
    { 
        List<String> LeadInfo = new List<String>();
        List<Lead> duplicatelist = new List<Lead>();
        for(Lead objLead : scope)
        {
            if(!EmailLead.containsKey(objLead.Email))
            {
                EmailLead.put(objLead.Email,objLead);
            }
            else
            {
                duplicatelist.add(objLead);	
            } 
        } 
        System.debug('>>duplicatelist>>>'+duplicatelist);
        System.debug('>>>EmailLead>>'+EmailLead);
        for(Lead objLead1 : duplicatelist)
        {
            LeadInfo.add(objLead1.Name+','+objLead1.Company);
            if(mapEmailName.containsKey(objLead1.Email))
            {
                LeadInfo.clear();
                LeadInfo.add(objLead1.Name+','+objLead1.Company);
                mapEmailName.put(objLead1.Email,LeadInfo);
            }
            else
            {
                mapEmailName.put(objLead1.Email,new List<String>(LeadInfo));
            }
        }
        System.debug('>>mapEmailName>>>'+mapEmailName);
        System.debug('>>EmailLead.values>>>'+EmailLead.values());
        for(Lead updateLead : EmailLead.values())
        {
            System.debug('>>updateLead>>>'+updateLead.Email);
            System.debug('>>EmailLead.values>>>'+EmailLead.values());
            if(mapEmailName.containsKey(updateLead.Email))
            {
                String strLead;
                System.debug('>>mapEmailName.get(updateLead.Email)>>>'+mapEmailName.get(updateLead.Email));
                strLead = String.join(mapEmailName.get(updateLead.Email),';');
                System.debug('>>strLead>>>'+strLead);
                updateLead.Description = strLead;
            }
            updateLead.Description.removeEnd(';');
        }
        if(EmailLead.values().size()>0)
            update EmailLead.values();
        if(duplicatelist.size() > 0)
        {
            delete duplicatelist;
        }
    }
    global void finish(Database.BatchableContext BC)
    {
        
    }
}
Suppose I have 5 Lead
2 with a@gmail.com mail address
3 with b@gmail.com mail address
So I want to delete duplicate of both email addres lead. Result is:
1 with a@gmail.com
2 with b@gmail.com
So the name and company of a@gmail.com is come on that lead description field. and another name and company of b@gmail.com come on thanother lead description field.

Above code is perfectly run but I am not able to separate name and company on the basis of email it mix and match all name and company in all leads
Here is my code. It perfectly run on insert, it also update contact name and it also delete the contact if i remove tha name but it updates by duplicate name. there is field on contact i.e. No_of_Occurences. If I update t
public class CommaSeparatedContact 
{
    String strFirstName;
    String strLastName;
    String strName;
    List<Contact> lstContact;
    Map<String,Contact> mapUniqueContact = new Map<String,Contact>();
    List<Contact> lstContactToDelete = new List<Contact>();
    Map<String,List<Contact>> mapContactComparison = new Map<String,List<Contact>>();
    Set<Id> setAccId= new Set<Id>();   
    
    public void onInsert(List<Account> lstAccountTrigger)
    {   
        List<String> lstString = new List<String>();
        for(Account objAccountCon : lstAccountTrigger)
        {
            lstContact = new List<Contact>();
            if(objAccountCon.Comma_Separated_Values__c != null)
                lstString =  objAccountCon.Comma_Separated_Values__c.split(',');
            for(Integer i=0; i<lstString.size(); i++)
            {
                Contact objContact = new Contact();
                if(lstString[i].contains(' '))
                {
                    strFirstName = lstString[i].substringBefore(' ');
                    strLastName = lstString[i].substringAfter(' ');
                }
                else
                {
                    strFirstName = '';
                    strLastName = lstString[i];
                }
                objContact.No_of_Occurrence__c = 1;
                objContact.LastName = strLastName;
                objContact.FirstName = strFirstName;
                strname = objContact.FirstName+' '+ objContact.LastName;
                objContact.AccountId = objAccountCon.Id;
                
                if(mapUniqueContact.containsKey(strname))
                {
                    Contact objc = mapUniqueContact.get(strname);
                    objc.No_of_Occurrence__c +=1;  
                }
                else
                {
                    mapUniqueContact.put(strname,objContact);
                }
            }
        }
        
        for(String objCont : mapUniqueContact.keySet())
        {
            lstContact.add(mapUniqueContact.get(objCont));
        }
        if(lstContact.size()>0)
        {
            insert lstContact;
        }
    }
    
    public void onUpdate(List<Account> lstAccountTrigger, Map<Id,Account> mapOldAccount)
    {  
        List<Contact> lstContactToUpdate = new List<Contact>();
        for(Account objConAcc : lstAccountTrigger)
        {
            if(objConAcc.Comma_Separated_Values__c != mapOldAccount.get(objConAcc.id).Comma_Separated_Values__c)
            {
                setAccId.add(objConAcc.id);
            }
        }
        
        for(Contact objCon : [Select Id, Name, AccountId, FirstName, LastName, Phone, Email, Description From Contact Where AccountId in : setAccId])
        {
            if(mapContactComparison!=null && mapContactComparison.containsKey(objCon.AccountId))
            {
                List<Contact> conList = mapContactComparison.get(objCon.AccountId);
                conList.add(objCon);
                mapContactComparison.put(objCon.AccountId, conList);
            }
            else
                mapContactComparison.put(objCon.AccountId, new List<Contact>{objCon});
        System.debug('>>>>>>>>81'+mapContactComparison.get(objCon.AccountId));
        }
        System.debug('>>>>>>>>83'+mapContactComparison.keySet());
        List<String> lstString = new List<String>();
        
        for(Account objAccountCon : lstAccountTrigger)
        {  
            Integer i =0;
            lstContact = new List<Contact>();
            if(objAccountCon.Comma_Separated_Values__c != null)
                lstString =  objAccountCon.Comma_Separated_Values__c.split(',');
            if(mapContactComparison != null && mapContactComparison.containsKey(objAccountCon.Id))
            {
                for(Contact objCon : mapContactComparison.get(objAccountCon.id))
                { 
                    if(lstString.size() == 0)
                    {
                       if(mapContactComparison.containsKey(objCon.AccountId))
                            mapUniqueContact.put(objCon.Name,objCon); 
                    }
                    if(lstString.size()>i)
                    {
                        System.debug('>>>103'+lstString[i]);
                        System.debug('Name Contains : '+lstString[i].contains(' ')); 
                        if(lstString[i].contains(' '))
                        {
                            strFirstName = lstString[i].substringBefore(' ');
                            strLastName = lstString[i].substringAfter(' ');
                        }
                        else
                        {
                            strFirstName = '';
                            strLastName = lstString[i];
                        }
                        
                        objCon.LastName = strLastName;
                        objCon.FirstName = strFirstName;
                        strName = objCon.FirstName+' '+ objCon.LastName;
                        lstContact.add(objCon);
                        System.debug('>>>>>>>119'+lstContact);
                        System.debug('>>>>>>>120'+mapContactComparison.get(objCon.AccountId));
                        if(mapContactComparison.containsKey(objCon.AccountId))
                            mapUniqueContact.put(strname,objCon);
                        System.debug('>>>>>>>>122'+mapUniqueContact.get(strname));
                        System.debug('>>>>>>>>123'+mapUniqueContact.keySet());
                    }
                    i+=1;
                }
            }
        }
        System.debug('>>>>>>>>1325'+lstContact);
        for(String objCont : mapUniqueContact.keySet())
        {
            System.debug('>>>>>>>>>>>>>131'+objCont);
            System.debug('>>>>>>>>>132'+lstContact.contains(mapUniqueContact.get(objCont)));
            if(!lstContact.contains(mapUniqueContact.get(objCont)))
                lstContact.add(mapUniqueContact.get(objCont));
        }
        System.debug('List of Contact Under An Account'+lstContact);
        if(lstContact.size()>0)
        {
            update lstContact;
        }
        for(Contact objConToDelete : lstContact)
        {
            System.debug('>>>>>>>>>143'+objConToDelete.Name);
            if(!lstString.contains(objConToDelete.Name))
            {  
                if(!lstContactToDelete.contains(objConToDelete))
                { 
                    System.debug('>>>>>>>>>>>>>148'+objConToDelete.Name);
                    lstContactToDelete.add(objConToDelete);
                }    
            }
        }
        if(lstContactToDelete.size()==0 && lstContact.size()==0)
        {
            System.debug('>>>>>>>>>>55');
            for(Contact str : mapUniqueContact.values())
            {
                System.debug('>>>>>>>>>>158');
                lstContactToDelete.add(str);
            }
        }
        if(lstContactToDelete.size()>0)
        {
            System.debug('delete List'+lstContactToDelete);
            delete lstContactToDelete;
        }
    }
}

he account  comma separated field with same values then the total number of time the values are come is update on Contact field i.e. Number of Occurences. Please Help

On update please Insert following values on account comma separated field: Amit,Dharmesh,Amit,Ankit,Amit. and test.

So the no of occurrence contains on Amit Contact is 3 and if I update the value with Amit,Dharmesh,Amit,Amit,Amit then the Ankit name of contact will be deleted and under Amit account there is no of occurences field value updated by 3 to 4 and only 2 accounts will remains i.e. Amit and Dharmesh.
public class MergeContact 
{
    public static Boolean runOnce = true;
    
    public void onInsert(List<Contact> lstInsertContactTrigger)
    {
        InsertContact(lstInsertContactTrigger);
    }
    public void onUpdate(List<Contact> lstUpdateContactTrigger, Map<Id,Contact> oldMapTriggerContact)
    {
        UpdateContact(lstUpdateContactTrigger,oldMapTriggerContact);
    }
    private void InsertContact(List<Contact> lstContactInsert)
    {
        List<Contact> lstContactToInsert = new List<Contact>();
        //Set<Id> setContId = new Set<Id>();
        Map<Id,Contact> mapCont = new Map<Id,Contact>();
        for(Contact objContact : lstContactInsert)
        {
            if(objContact.AccountId != null)
            {
                mapCont.put(objContact.AccountId, objContact);
                //setContId.add(objContact.Id);
            }
        }                
        List<Contact> lstContactToCheck = [Select Id, Name, Email, Phone, AccountId, Birthdate, Description, Fax, FirstName, LastName, MailingAddress, MailingCity, MailingState, MailingCountry, MailingPostalCode 
                                           From Contact Where AccountId in : mapCont.keySet()];
        System.debug('List of Existing Contact'+lstContactToCheck);
        for(Contact objContact : lstContactInsert)
        {
            Contact objNewContact = new Contact();
            objNewContact.Id = objContact.Id;
            objNewContact.Email = mapCont.get(objContact.AccountId).Email;
            objNewContact.LastName = mapCont.get(objContact.AccountId).LastName;
            objNewContact.FirstName = mapCont.get(objContact.AccountId).FirstName;
            objNewContact.Phone = mapCont.get(objContact.AccountId).Phone;
            String Name =  objNewContact.FirstName+' '+objNewContact.LastName;
            if(objContact.Name == Name)
            {
                lstContactToCheck.add(objNewContact);
            }
        }
        if(lstContactToInsert.size()>0)
        {
            upsert lstContactToCheck;
        }  
    }
    private void UpdateContact(List<Contact> lstContactUpdate, Map<Id,Contact> oldMapContact)
    {
        List<Contact> lstContactToUpdate = new List<Contact>();
        for(Contact objCon : lstContactUpdate)
        {
            Contact objOldContact = new Contact();
            if(oldMapContact.containsKey(objCon.Id))
            {
                objOldContact.FirstName = oldMapContact.values().FirstName;
                objOldContact.LastName = oldMapContact.values().LastName;
                objOldContact.Email = oldMapContact.values().Email;
                objOldContact.Phone = oldMapContact.values().Phone;
                objOldContact.Birthdate = oldMapContact.values().Birthdate;
                objOldContact.Fax = oldMapContact.values().Fax;
                objOldContact.Description = oldMapContact.values().Description;
                objOldContact.AccountId = oldMapContact.values().AccountId;
                lstContactToUpdate.add(objOldContact);
            }
        }
        if(lstContactToUpdate.size()>0 && runOnce == false)
            update lstContactToUpdate;
    }
}

 

trigger WBSAttachment on Project__c (before insert, before update) 
{
    
    
    Project__c[] lstProject = [SELECT Id, WBS_Attached__c, (SELECT Id, Name, ContentType FROM Attachments)  FROM Project__c where id IN :Trigger.newMap.keySet()];
    for(Project__c objProject : lstProject)
    {
        Attachment[] lstAttc = objProject.Attachments;
        if(lstAttc.size()>0 && objProject.Attachments.Name.contains('WBS'))
        {
            System.debug('Need to set Syllabus Attached to true for Opportunity Id: ' + objProject.id);
            System.debug('just testing this: ' + Trigger.newMap.get(objProject.Id).Id);
            
            Trigger.newMap.get(objProject.Id).WBS_Attached__c = true;
        }
        else
        {
            Trigger.newMap.get(objProject.Id).WBS_Attached__c = false;
        }                  
    }
    
}
Here is my code. It perfectly run on insert, it also update contact name and it also delete the contact if i remove tha name but it updates by duplicate name. there is field on contact i.e. No_of_Occurences. If I update t
public class CommaSeparatedContact 
{
    String strFirstName;
    String strLastName;
    String strName;
    List<Contact> lstContact;
    Map<String,Contact> mapUniqueContact = new Map<String,Contact>();
    List<Contact> lstContactToDelete = new List<Contact>();
    Map<String,List<Contact>> mapContactComparison = new Map<String,List<Contact>>();
    Set<Id> setAccId= new Set<Id>();   
    
    public void onInsert(List<Account> lstAccountTrigger)
    {   
        List<String> lstString = new List<String>();
        for(Account objAccountCon : lstAccountTrigger)
        {
            lstContact = new List<Contact>();
            if(objAccountCon.Comma_Separated_Values__c != null)
                lstString =  objAccountCon.Comma_Separated_Values__c.split(',');
            for(Integer i=0; i<lstString.size(); i++)
            {
                Contact objContact = new Contact();
                if(lstString[i].contains(' '))
                {
                    strFirstName = lstString[i].substringBefore(' ');
                    strLastName = lstString[i].substringAfter(' ');
                }
                else
                {
                    strFirstName = '';
                    strLastName = lstString[i];
                }
                objContact.No_of_Occurrence__c = 1;
                objContact.LastName = strLastName;
                objContact.FirstName = strFirstName;
                strname = objContact.FirstName+' '+ objContact.LastName;
                objContact.AccountId = objAccountCon.Id;
                
                if(mapUniqueContact.containsKey(strname))
                {
                    Contact objc = mapUniqueContact.get(strname);
                    objc.No_of_Occurrence__c +=1;  
                }
                else
                {
                    mapUniqueContact.put(strname,objContact);
                }
            }
        }
        
        for(String objCont : mapUniqueContact.keySet())
        {
            lstContact.add(mapUniqueContact.get(objCont));
        }
        if(lstContact.size()>0)
        {
            insert lstContact;
        }
    }
    
    public void onUpdate(List<Account> lstAccountTrigger, Map<Id,Account> mapOldAccount)
    {  
        List<Contact> lstContactToUpdate = new List<Contact>();
        for(Account objConAcc : lstAccountTrigger)
        {
            if(objConAcc.Comma_Separated_Values__c != mapOldAccount.get(objConAcc.id).Comma_Separated_Values__c)
            {
                setAccId.add(objConAcc.id);
            }
        }
        
        for(Contact objCon : [Select Id, Name, AccountId, FirstName, LastName, Phone, Email, Description From Contact Where AccountId in : setAccId])
        {
            if(mapContactComparison!=null && mapContactComparison.containsKey(objCon.AccountId))
            {
                List<Contact> conList = mapContactComparison.get(objCon.AccountId);
                conList.add(objCon);
                mapContactComparison.put(objCon.AccountId, conList);
            }
            else
                mapContactComparison.put(objCon.AccountId, new List<Contact>{objCon});
        System.debug('>>>>>>>>81'+mapContactComparison.get(objCon.AccountId));
        }
        System.debug('>>>>>>>>83'+mapContactComparison.keySet());
        List<String> lstString = new List<String>();
        
        for(Account objAccountCon : lstAccountTrigger)
        {  
            Integer i =0;
            lstContact = new List<Contact>();
            if(objAccountCon.Comma_Separated_Values__c != null)
                lstString =  objAccountCon.Comma_Separated_Values__c.split(',');
            if(mapContactComparison != null && mapContactComparison.containsKey(objAccountCon.Id))
            {
                for(Contact objCon : mapContactComparison.get(objAccountCon.id))
                { 
                    if(lstString.size() == 0)
                    {
                       if(mapContactComparison.containsKey(objCon.AccountId))
                            mapUniqueContact.put(objCon.Name,objCon); 
                    }
                    if(lstString.size()>i)
                    {
                        System.debug('>>>103'+lstString[i]);
                        System.debug('Name Contains : '+lstString[i].contains(' ')); 
                        if(lstString[i].contains(' '))
                        {
                            strFirstName = lstString[i].substringBefore(' ');
                            strLastName = lstString[i].substringAfter(' ');
                        }
                        else
                        {
                            strFirstName = '';
                            strLastName = lstString[i];
                        }
                        
                        objCon.LastName = strLastName;
                        objCon.FirstName = strFirstName;
                        strName = objCon.FirstName+' '+ objCon.LastName;
                        lstContact.add(objCon);
                        System.debug('>>>>>>>119'+lstContact);
                        System.debug('>>>>>>>120'+mapContactComparison.get(objCon.AccountId));
                        if(mapContactComparison.containsKey(objCon.AccountId))
                            mapUniqueContact.put(strname,objCon);
                        System.debug('>>>>>>>>122'+mapUniqueContact.get(strname));
                        System.debug('>>>>>>>>123'+mapUniqueContact.keySet());
                    }
                    i+=1;
                }
            }
        }
        System.debug('>>>>>>>>1325'+lstContact);
        for(String objCont : mapUniqueContact.keySet())
        {
            System.debug('>>>>>>>>>>>>>131'+objCont);
            System.debug('>>>>>>>>>132'+lstContact.contains(mapUniqueContact.get(objCont)));
            if(!lstContact.contains(mapUniqueContact.get(objCont)))
                lstContact.add(mapUniqueContact.get(objCont));
        }
        System.debug('List of Contact Under An Account'+lstContact);
        if(lstContact.size()>0)
        {
            update lstContact;
        }
        for(Contact objConToDelete : lstContact)
        {
            System.debug('>>>>>>>>>143'+objConToDelete.Name);
            if(!lstString.contains(objConToDelete.Name))
            {  
                if(!lstContactToDelete.contains(objConToDelete))
                { 
                    System.debug('>>>>>>>>>>>>>148'+objConToDelete.Name);
                    lstContactToDelete.add(objConToDelete);
                }    
            }
        }
        if(lstContactToDelete.size()==0 && lstContact.size()==0)
        {
            System.debug('>>>>>>>>>>55');
            for(Contact str : mapUniqueContact.values())
            {
                System.debug('>>>>>>>>>>158');
                lstContactToDelete.add(str);
            }
        }
        if(lstContactToDelete.size()>0)
        {
            System.debug('delete List'+lstContactToDelete);
            delete lstContactToDelete;
        }
    }
}

he account  comma separated field with same values then the total number of time the values are come is update on Contact field i.e. Number of Occurences. Please Help

On update please Insert following values on account comma separated field: Amit,Dharmesh,Amit,Ankit,Amit. and test.

So the no of occurrence contains on Amit Contact is 3 and if I update the value with Amit,Dharmesh,Amit,Amit,Amit then the Ankit name of contact will be deleted and under Amit account there is no of occurences field value updated by 3 to 4 and only 2 accounts will remains i.e. Amit and Dharmesh.
public class MergeContact 
{
    public static Boolean runOnce = true;
    
    public void onInsert(List<Contact> lstInsertContactTrigger)
    {
        InsertContact(lstInsertContactTrigger);
    }
    public void onUpdate(List<Contact> lstUpdateContactTrigger, Map<Id,Contact> oldMapTriggerContact)
    {
        UpdateContact(lstUpdateContactTrigger,oldMapTriggerContact);
    }
    private void InsertContact(List<Contact> lstContactInsert)
    {
        List<Contact> lstContactToInsert = new List<Contact>();
        //Set<Id> setContId = new Set<Id>();
        Map<Id,Contact> mapCont = new Map<Id,Contact>();
        for(Contact objContact : lstContactInsert)
        {
            if(objContact.AccountId != null)
            {
                mapCont.put(objContact.AccountId, objContact);
                //setContId.add(objContact.Id);
            }
        }                
        List<Contact> lstContactToCheck = [Select Id, Name, Email, Phone, AccountId, Birthdate, Description, Fax, FirstName, LastName, MailingAddress, MailingCity, MailingState, MailingCountry, MailingPostalCode 
                                           From Contact Where AccountId in : mapCont.keySet()];
        System.debug('List of Existing Contact'+lstContactToCheck);
        for(Contact objContact : lstContactInsert)
        {
            Contact objNewContact = new Contact();
            objNewContact.Id = objContact.Id;
            objNewContact.Email = mapCont.get(objContact.AccountId).Email;
            objNewContact.LastName = mapCont.get(objContact.AccountId).LastName;
            objNewContact.FirstName = mapCont.get(objContact.AccountId).FirstName;
            objNewContact.Phone = mapCont.get(objContact.AccountId).Phone;
            String Name =  objNewContact.FirstName+' '+objNewContact.LastName;
            if(objContact.Name == Name)
            {
                lstContactToCheck.add(objNewContact);
            }
        }
        if(lstContactToInsert.size()>0)
        {
            upsert lstContactToCheck;
        }  
    }
    private void UpdateContact(List<Contact> lstContactUpdate, Map<Id,Contact> oldMapContact)
    {
        List<Contact> lstContactToUpdate = new List<Contact>();
        for(Contact objCon : lstContactUpdate)
        {
            Contact objOldContact = new Contact();
            if(oldMapContact.containsKey(objCon.Id))
            {
                objOldContact.FirstName = oldMapContact.values().FirstName;
                objOldContact.LastName = oldMapContact.values().LastName;
                objOldContact.Email = oldMapContact.values().Email;
                objOldContact.Phone = oldMapContact.values().Phone;
                objOldContact.Birthdate = oldMapContact.values().Birthdate;
                objOldContact.Fax = oldMapContact.values().Fax;
                objOldContact.Description = oldMapContact.values().Description;
                objOldContact.AccountId = oldMapContact.values().AccountId;
                lstContactToUpdate.add(objOldContact);
            }
        }
        if(lstContactToUpdate.size()>0 && runOnce == false)
            update lstContactToUpdate;
    }
}