+ Start a Discussion
Kumar5Kumar5 

How to bulkify the below code

Hi Team,

I want to populate account id  value into the field (BISM_Officer__c) of object Clarity_Effort__c. 

Below is the requirement in brief 
I have two objects Clarity and Account.  If any record is created with out BISM_Officer__c then, there is field Resource_Name__c in Clarity object and in Account there is field Clarity_Effort_Resource_Name__c if these two are matching, the accountId of value with Clarity_Effort_Resource_Name__c need to popualte in Clarity record. The below code is working  for single record, but not bulk records.

Would you please guide me with above requirement.


trigger  AccountPopulate on Clarity_Effort__c (before insert, before update) 
{

     String AccountId;

     Map<String,List<Clarity_Effort__c>> AccountClarityList = new Map <String,List<Clarity_Effort__c>>();
     
     
     List<Account>  AccountList = new List<Account> ();
     
    // List<Clarity_Effort__c> UpdateClarityList = new List<Clarity_Effort__c>();
     List<Clarity_Effort__c> NewClartiryList= new List<Clarity_Effort__c>();
     
     List<string> ClarityWithoutAccounts = new List<string>();
   
     for (Clarity_Effort__c  CE : Trigger.new) 
      
       {
     
          if (Trigger.isInsert || Trigger.isUpdate)
          {
           if(CE.BISM_Officer__c == null)
       
             {
                 ClarityWithoutAccounts.add(CE.Resource_Name__c);
                 NewClartiryList.add(CE);
                 
             } 
          }      
             
            system.debug( 'Naresh'+ ClarityWithoutAccounts);
            
                                     
       }       
       
        AccountList = [SELECT Id FROM Account WHERE Clarity_Effort_Resource_Name__c IN : ClarityWithoutAccounts];  
        

          for(Account Acc : AccountList) 
          
          
           {
              if(!AccountClarityList.containsKey(Acc.Id))
              
                  {
                
                     AccountClarityList.put(Acc.id, new List<Clarity_Effort__c>());
                     AccountClarityList.get(Acc.Id); 
                     AccountId= Acc.Id;  
                     
                  }
              
              system.debug('AccountListCL'+AccountClarityList);
              
            for (Clarity_Effort__c  CEU : NewClartiryList ) 
        
               {
          
                   CEU.BISM_Officer__c = AccountId;
                   // UpdateClarityList.add(CEU);
               }          
              

                 
           }                         
        
          
       
      
 }

Thanks,
Kumar
Best Answer chosen by Kumar5
SaranSaran
Hi Naresh,


Below is the modified code. Hope it works.
 
trigger  AccountPopulate on Clarity_Effort__c (after insert, after update) 
{
	Map<String, id> AccountMap = new Map <String,id>();
	List<Clarity_Effort__c> NewClartiryList= new List<Clarity_Effort__c>();
	set<string> ClarityWithoutAccounts = new set<string>();
	
	for (Clarity_Effort__c  CE : Trigger.new) 
	{
		if(CE.BISM_Officer__c == null)
		{
			ClarityWithoutAccounts.add(CE.Resource_Name__c);
			NewClartiryList.add(CE);
		}
	}   
    
	for(Account acc : [SELECT Id, Clarity_Effort_Resource_Name__c FROM Account WHERE Clarity_Effort_Resource_Name__c IN : ClarityWithoutAccounts])
	{
		AccountMap.put(acc.Clarity_Effort_Resource_Name__c , acc.Id);
	}

	for(Clarity_Effort__c  CE : NewClartiryList) 
	{
		if(AccountMap.containsKey(CE.Resource_Name__c))
		{
		CEU.BISM_Officer__c = AccountMap.get(CE.Resource_Name__c);
		}          
	}      
}

Thanks,
Saraz

All Answers

SaranSaran
Hi Naresh,


Below is the modified code. Hope it works.
 
trigger  AccountPopulate on Clarity_Effort__c (after insert, after update) 
{
	Map<String, id> AccountMap = new Map <String,id>();
	List<Clarity_Effort__c> NewClartiryList= new List<Clarity_Effort__c>();
	set<string> ClarityWithoutAccounts = new set<string>();
	
	for (Clarity_Effort__c  CE : Trigger.new) 
	{
		if(CE.BISM_Officer__c == null)
		{
			ClarityWithoutAccounts.add(CE.Resource_Name__c);
			NewClartiryList.add(CE);
		}
	}   
    
	for(Account acc : [SELECT Id, Clarity_Effort_Resource_Name__c FROM Account WHERE Clarity_Effort_Resource_Name__c IN : ClarityWithoutAccounts])
	{
		AccountMap.put(acc.Clarity_Effort_Resource_Name__c , acc.Id);
	}

	for(Clarity_Effort__c  CE : NewClartiryList) 
	{
		if(AccountMap.containsKey(CE.Resource_Name__c))
		{
		CEU.BISM_Officer__c = AccountMap.get(CE.Resource_Name__c);
		}          
	}      
}

Thanks,
Saraz
This was selected as the best answer
SaranSaran
Hi Naresh,

Add a line - update NewClartiryList;
next to the line 27

Thanks,
Saraz