You need to sign in to do that
Don't have an account?
iswarya 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); } }
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.