+ Start a Discussion
iswarya sekar 7iswarya sekar 7 

how to bulkify this code. Please help me!! Thanks in advance.

public class UpdateContactName {
    
    public static void methodTopopulateContact(List<Case> cs){
        String emailField;
        
        List<Case> updateCases=new List<Case>(); //creates a list of case records.
        
        for(Case newCase:cs){
            if(newCase.Profile_received_from_Email__c!=null){ //checks whether the custom field is empty
                emailField = newCase.Profile_received_from_Email__c;
                system.debug('The email field is '+ emailField);
            }
        }
        
        List<Contact> con=[select accountId,Id,email from Contact where email=:emailField]; //query the records from contact
        
        if(con.size()>0){
            for(Case cse:cs){
                cse.contactId=con[0].id;
                cse.accountId=con[0].accountId;
                updateCases.add(cse);
            }
        }
    }
}
 
trigger caseTrigger on Case (before insert,before update) {
    if((trigger.isupdate || trigger.isInsert) && trigger.IsBefore){
        UpdateContactName.methodTopopulateContact(Trigger.new);
    }
}

 
Waqar Hussain SFWaqar Hussain SF
To bulkify your code you will have to use map and list. See the below code.
 
public class UpdateContactName {
    
    public static void methodTopopulateContact(List<Case> cs){
        list<String> emailFields = new list<string>();;
        
        List<Case> updateCases=new List<Case>(); //creates a list of case records.
        
        for(Case newCase:cs){
            if(newCase.Profile_received_from_Email__c!=null){ //checks whether the custom field is empty
                emailFields.add(newCase.Profile_received_from_Email__c);
                system.debug('The email field is '+ emailField);
            }
        }
        map<string,Contact> EmailtoContactMap = new map<string, Contact>();
		
        List<Contact> con=[select accountId,Id,email from Contact where email IN :emailFields]; //query the records from contact
        
		for(Contact c : con){
			if(c.email != null)
			EmailtoContactMap.put(c.email, c);
		}
		
		for(Case cse:cs){
			Contact cont = EmailtoContactMap.get(cse.Profile_received_from_Email__c);
			if(cont != null){
				cse.contactId=cont.id;
				cse.accountId=cont.accountId;
				updateCases.add(cse);
			}
		}
    }
}

 
iswarya sekar 7iswarya sekar 7
Hii Waqar, Thank you for your response. Now how I will check this code is bulkyfying.
Waqar Hussain SFWaqar Hussain SF
You can test by creating/updating a list of cases using apex script. 
iswarya sekar 7iswarya sekar 7
Im having one more doubt. if two contacts has same email address, then what will be the output?
Waqar Hussain SFWaqar Hussain SF
Hi Iswarya,
If two contacts having the same email address the one which is creating created recently will be get by the query.  You can put order by in the query.
iswarya sekar 7iswarya sekar 7
But its showing me the error as expression cannot be a statement. where it need to be added and how?
 
Waqar Hussain SFWaqar Hussain SF
Can you please paste your code here?