+ Start a Discussion
Neelima KongathiNeelima Kongathi 

Is there any way to get this soql and dml out of the loop in this code??

public class DuplicatePanCheck {
    public static void duplicatePanNo(List<Consultant__c> cons){
        for(Consultant__c c: cons){
                if(c.Pan_Number__c!= null){ 
                List<Consultant__c> con = [select id,Pan_Number__c,Phone__c,Email__c from Consultant__c where Pan_Number__c =: c.Pan_Number__c];  //checking the duplicate record based on the pan number
                        if(con.size()>1){
                        for(Consultant__c cn : con){   //if duplicate records found, updating phone, email fields in the old records 
                            cn.Phone__c = c.Phone__c;
                            cn.Email__c = c.Email__c;
                        }
                    update con;
                }
            }
        }
    }
}

trigger Trigg_DuplicatePanCheck on Consultant__c (after insert) {
    DuplicatePanCheck.duplicatePanNo(Trigger.new);
}
 
Somya TiwariSomya Tiwari
Hi Neelma,

Please try the following code
public class DuplicatePanCheck {
    public static void duplicatePanNo(List<Consultant__c> cons){
        List<String> Pan_Number_List = new List<String>();
        List<Consultant__c> con2 = new List<Consultant__C>();
        List<Consultant__c> con2Update = new List<Consultant__C>();
        for(Consultant__c c: cons)
        {
            Pan_Number_List.add(c.Pan_Number__c);
        }
        List<Consultant__c> con = [select id,Pan_Number__c,Phone__c,Email__c from Consultant__c where Pan_Number__c =: Pan_Number_List];  //checking the duplicate record based on the pan number
        for(Consultant__c c: cons)
        {
            for(Consultant__c t:con)
            {
                if(c.Pan_Number__c == t.Pan_Number__c)
                    con2.add(t);
            }
            if(c.Pan_Number__c!= null)
            { 
                if(con2.size()>1)
                {
                    for(Consultant__c cn : con2)
                    {   //if duplicate records found, updating phone, email fields in the old records 
                        cn.Phone__c = c.Phone__c;
                        cn.Email__c = c.Email__c;
                    }
                    con2Update.add(con2);
                }
            }
        }
    }
}

trigger Trigg_DuplicatePanCheck on Consultant__c (after insert) {
    DuplicatePanCheck.duplicatePanNo(Trigger.new);
}
Do reply if there is any issue with the code. To my best guess this should work, but if any thing creates a problem do let me know will help you futher with the solution. If you get any problem occoured solved yourself, then also comment with the correct solution here.
Also if my reply helped you kindly mark my answer as Best.

Regards,
Somya Tiwari
Abdul KhatriAbdul Khatri
I would take this simple approach with simple code.
 
public class DuplicatePanCheck {
    
    public static void duplicatePanNo(List<Consultant__c> cons)
    {
		Map<String, Consultant__c> consultantMap = getPanNumberMap(cons);
        
        //checking the duplicate record based on the pan number 
		List<Consultant__c> conList = [SELECT id,Pan_Number__c,Phone__c,Email__c 
                                         FROM Consultant__c 
                                         WHERE Pan_Number__c =: consultantMap.keyset()];  

        
        for(Consultant__c c: contList)
        {
            cn.Phone__c = consultantMap.get(c.Pan_Number__c).Phone__c;
            cn.Email__c = consultantMap.get(c.Pan_Number__c).Email__c;
        }
        
        Database.update(contList);
    }
    
    public static Map<String, Consultant__c> getPanNumberMap(List<Consultant__c> cons) {
        
        Map<String, Consultant__c> returnVal = new Map<String, Consultant__c>();
        for(Consultant__c con : cons) {
            
            if(con.Pan_Number__c != null)
            {
                returnVal.put(con.Pan_Number__c, con);
            }
        }
        
        return returnVal;
    }
}

trigger Trigg_DuplicatePanCheck on Consultant__c (after insert) {
    DuplicatePanCheck.duplicatePanNo(Trigger.new);
}

 
Neelima KongathiNeelima Kongathi
thank you so much for ur help @SowmyaTiwari n i also get almost same approach n here is my code
public class DuplicatePanCheck {
	public static void duplicatePanNo(List<Consultant__c> cons){
	List<Consultant__c> consultants = [select id,Pan_Number__c,Phone__c,Email__c from Consultant__c where Pan_Number__c!=null];
	List<Consultant__c> updatedConsultants = new List<Consultant__c>(); 
		for(Consultant__c c: cons){
        	if(c.Pan_Number__c!= null){ 
        		for(consultant__c cs : consultants){
					if(c.Pan_Number__c == cs.Pan_Number__c){
						cs.Phone__c = c.Phone__c;
						cs.Email__c = c.Email__c;
						updatedConsultants.add(cs);
					}
				}
        	}
        }
        update updatedConsultants;
    }
}
Deepali KulshresthaDeepali Kulshrestha
Hi Neelima,

Please try this code:
 
public class DuplicatePanCheck {
    public static void duplicatePanNo(List<Consultant__c> cons){
Set<Integer> panNumberList = new Set<Integer>();
        Set<Id> newConsId = new Set<Id>();
    //
        for(Consultant con : cons){
            newConsId.add(cons.Id);
            panNumberList.add(cons.Pan_Number__c);
        }
    //querying all old consultants
        List<Consultant__c> oldConsultants= [select id,Pan_Number__c,Phone__c,Email__c from Consultant__c where Pan_Number__c IN :panNumberList AND ID NOT IN :newConsId];
        Map<Integer,Consultant__c> oldConsMap = new Map<Integer,Consultant__c>();
        for(Consultant__c con : oldConsultants{
            if(!oldConsMap.ContainsKey(con.Pan_Number__c))
                oldConsMap.put(con.Pan_Number__c,con);
        }
            
            List<Consultant__c> consToUpdate = new List<Consultant__c>();
            for(Consultant__c c: cons){
                if(oldConsMap.ContainsKey(c.Pan_Number__c)){
                    Consultant oldCon = oldConsMap.get(c.Pan_Number__c);
                    oldCon.Phone__c = c.Phone__c;
                    oldCon.Email__c = c.Email__c;
                    consToUpdate .add(oldCon);
                }
            }
            update consToUpdate;        
    }
}       

trigger Trigg_DuplicatePanCheck on Consultant__c (after insert) {
    DuplicatePanCheck.duplicatePanNo(Trigger.new);
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com