function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MaheemSamMaheemSam 

Bulk Transaction trigger and helper class

Hi,

  I wrote below trigger and helper class which is working fine need your suggestion how to modify the code to handle bulk transaction please suggest me.
Trigger

trigger CtapAssessmentTrigger on CTAP_Assessment__c (before Insert, before Update) {

     if(Trigger.isBefore) {
       if(Trigger.isInsert) {
            CtapAssessmentTriggerUtils.processInsert(Trigger.new);
        } else if(Trigger.isUpdate) {
            CtapAssessmentTriggerUtils.processUpdate(Trigger.newMap, Trigger.oldMap);                     
        }
} 


Helper Class

public class CtapAssessmentTriggerUtils {
  
   
    public static void processInsert(List<CTAP_Assessment__c> newLst) {
       for(CTAP_Assessment__c  ctap : newLst){
              ctap.Contact__c = CtapAssessmentTriggerUtils.GetContact(ctap.Partner_email__c);
              ctap.Lead__c = CtapAssessmentTriggerUtils.GetLead(ctap.Partner_email__c);
              }  
       
    }
    
    public static void processUpdate(Map<id,CTAP_Assessment__c> newMap, Map<id,CTAP_Assessment__c> oldMap) {
    
      for(CTAP_Assessment__c  ctap : newMap.values()){
              ctap.Contact__c = CtapAssessmentTriggerUtils.GetContact(ctap.Partner_email__c);
              ctap.Lead__c = CtapAssessmentTriggerUtils.GetLead(ctap.Partner_email__c);
              }  
    
    }
    
   
   public static String GetContact(String partneremail){
    
    String ContID;   
    list<Contact> con = [select id,email from contact where email = :partneremail];  
     
      if( con.size()>0){
        ContID = con[0].id;
       }
     
     return ContID; 
   }
   
   public static String GetLead(String partneremail){
     String LedID;
     list<Lead> led = [select id, email from lead where email = :partneremail];  
     
      if (led.size() > 0){
        LedID = led[0].id;
       }
     
     return LedID;
   }
    
    
}

Thanks
Sudhir
Best Answer chosen by MaheemSam
Nayana KNayana K
public class CtapAssessmentTriggerUtils {
  
   
    public static void processInsert(List<CTAP_Assessment__c> newLst) {
       populateEmailMatchedRecord(newLst);
       
    }
    
    public static void processUpdate(Map<id,CTAP_Assessment__c> newMap, Map<id,CTAP_Assessment__c> oldMap) {
    
      populateEmailMatchedRecord(newMap.values());
    }
    
	private static void populateEmailMatchedRecord(List<CTAP_Assessment__c> lstNewAssessment)
	{
		Set<String> setEmail = new Set<String>();
		Map<String, Id> mapEmailToConId = new Map<String, Id>();
		Map<String, Id> mapEmailToLeadId = new Map<String, Id>();
		// collect emails in a set
		for(CTAP_Assessment__c ctap : lstNewAssessment)
		{
			setEmail.add(ctap.Partner_email__c);
		}
		// removing nulls
		setEmail.remove(null);
		
		if(!setEmail.isEmpty())
		{
			for(Contact objCon : [select id,email from contact where email IN :setEmail])
			{
				mapEmailToConId.put(objCon.Email, objCon.Id);
			}
			
			for(Lead objLead: [select id,email from Lead where email IN :setEmail])
			{
				mapEmailToLeadId.put(objLead.Email, objLead.Id);
			}
		}
		
		// asssign based on map key match with email
		for(CTAP_Assessment__c ctap : lstNewAssessment)
		{
			if(mapEmailToConId.containsKey(ctap.Partner_email__c))
			{
				ctap.Contact__c = mapEmailToConId.get(ctap.Partner_email__c);
			}
			if(mapEmailToLeadId.containsKey(ctap.Partner_email__c))
			{
				ctap.Lead__c = mapEmailToLeadId.get(ctap.Partner_email__c);
			}
		}
	}
}


On update use case, if you want the logic to happen only when contact and Lead changes to avoid unnecessary processing then we have to change the logic a bit.

Please let me know if this helps.

All Answers

Ruwantha  LankathilakaRuwantha Lankathilaka
You can handle bulks as follows, 
Create the key,value map with the filed you want to have. loop throguh it as shown.
 
public static void processInsert(List<CTAP_Assessment__c> newLst) {
		List<String> emails = new List<String>();
		Map<String,Contact> emailContactMap = new Map<String,Contact>();
		Map<String,Lead> emailLeadMap = new Map<String,Contact>();
		
        for(CTAP_Assessment__c  ctap : newLst){
 			  if(ctap.Partner_email__c != null){
				 emails.add(ctap.Partner_email__c);
			  }
        }  
        if(!emails.isEmpty()){
			for(Contact con : [SELECT Id,Email FROM Contact WHERE email IN : emails]){
				emailContactMap.put(con.Email,con);
			}
			for(Lead ld : [SELECT Id,Email FROM Lead WHERE Email IN : emails]){
				emailLeadMap.put(ld.Email,ld);
			}
			
			
			for(CTAP_Assessment__c  ctap : newLst){
				ctap.Contact__c = emailContactMap.get(ctap.email);
				etap.Lead__c = emailLeadMap.get(ctap.Email);
			}
		}
	   
    }
    /*
    public static void processUpdate(Map<id,CTAP_Assessment__c> newMap, Map<id,CTAP_Assessment__c> oldMap) {
    
      for(CTAP_Assessment__c  ctap : newMap.values()){
              ctap.Contact__c = CtapAssessmentTriggerUtils.GetContact(ctap.Partner_email__c);
              ctap.Lead__c = CtapAssessmentTriggerUtils.GetLead(ctap.Partner_email__c);
              }  
    
    }
	*/

}

I have done only the insert you can do the update as the same way


If this helps you please mark it as the best answer so it could help the community in many ways.

 
Nayana KNayana K
public class CtapAssessmentTriggerUtils {
  
   
    public static void processInsert(List<CTAP_Assessment__c> newLst) {
       populateEmailMatchedRecord(newLst);
       
    }
    
    public static void processUpdate(Map<id,CTAP_Assessment__c> newMap, Map<id,CTAP_Assessment__c> oldMap) {
    
      populateEmailMatchedRecord(newMap.values());
    }
    
	private static void populateEmailMatchedRecord(List<CTAP_Assessment__c> lstNewAssessment)
	{
		Set<String> setEmail = new Set<String>();
		Map<String, Id> mapEmailToConId = new Map<String, Id>();
		Map<String, Id> mapEmailToLeadId = new Map<String, Id>();
		// collect emails in a set
		for(CTAP_Assessment__c ctap : lstNewAssessment)
		{
			setEmail.add(ctap.Partner_email__c);
		}
		// removing nulls
		setEmail.remove(null);
		
		if(!setEmail.isEmpty())
		{
			for(Contact objCon : [select id,email from contact where email IN :setEmail])
			{
				mapEmailToConId.put(objCon.Email, objCon.Id);
			}
			
			for(Lead objLead: [select id,email from Lead where email IN :setEmail])
			{
				mapEmailToLeadId.put(objLead.Email, objLead.Id);
			}
		}
		
		// asssign based on map key match with email
		for(CTAP_Assessment__c ctap : lstNewAssessment)
		{
			if(mapEmailToConId.containsKey(ctap.Partner_email__c))
			{
				ctap.Contact__c = mapEmailToConId.get(ctap.Partner_email__c);
			}
			if(mapEmailToLeadId.containsKey(ctap.Partner_email__c))
			{
				ctap.Lead__c = mapEmailToLeadId.get(ctap.Partner_email__c);
			}
		}
	}
}


On update use case, if you want the logic to happen only when contact and Lead changes to avoid unnecessary processing then we have to change the logic a bit.

Please let me know if this helps.
This was selected as the best answer
Nayana KNayana K
On update use case, if you want the logic to happen only when contact and Lead changes to avoid unnecessary processing then we have to change the logic a bit.

Sorry I meant only when "email "changes not "contact and Lead" changes.
MaheemSamMaheemSam

Thanks Ruwanthalk and Nayana for you help. 

 Nayana, I used you code it is working I see one issue during update old values wont clear when email dont match. 

 Example :  Contact and Lead both are updated in insert In update when i change the partner contact only contact match but lead do not match in this case lead must be set to null but now it is still showing the old value. 

Please let me know if you have any suggestion to change. 

Thanks

Sudhir

Nayana KNayana K
// asssign based on map key match with email
		for(CTAP_Assessment__c ctap : lstNewAssessment)
		{
			if(mapEmailToConId.containsKey(ctap.Partner_email__c))
			{
				ctap.Contact__c = mapEmailToConId.get(ctap.Partner_email__c);
			}
			if(mapEmailToLeadId.containsKey(ctap.Partner_email__c))
			{
				ctap.Lead__c = mapEmailToLeadId.get(ctap.Partner_email__c);
			}
		}

Replace these lines with
 
// asssign based on map key match with email
		for(CTAP_Assessment__c ctap : lstNewAssessment)
		{

				ctap.Contact__c = mapEmailToConId.containsKey(ctap.Partner_email__c) ?mapEmailToConId.get(ctap.Partner_email__c) : null;

				ctap.Lead__c = mapEmailToLeadId.containsKey(ctap.Partner_email__c) ?
 mapEmailToLeadId.get(ctap.Partner_email__c) : null;
			
		}

 
MaheemSamMaheemSam
Thanks Nayana You resolved my issue appriciate you help.

Thanks
Sudhir