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
Venkata Shiva Koushik RachapudiVenkata Shiva Koushik Rachapudi 

Help with Bulkifying Apex Code

Trigger

trigger SDS_AE_onAccount on Account (before update) {
//load salesmapping table in memory
List<Sales_mapping__c> Shiva = [Select OwnerId, mapping_type__c,AE__c from sales_mapping__c];    
List<Account> UpdateSDSAE = new List<Account>();
    for(account a: Trigger.new)
    {
        if(a.Owner_profile__c == 'SDS' || a.Owner_profile__c == 'SDS Manager')
        {
            a.SDS__c = a.OwnerId;
            UpdateSDSAE.add(a);
        }
        else if(a.Owner_Profile__c=='AE' && trigger.Oldmap.get(a.Id).SDS__c == trigger.Newmap.get(a.Id).SDS__c)
        {
          a.AE__c = a.OwnerId;
            UpdateSDSAE.add(a);
            SDSAD.SSDSD(UpdateSDSAE);
            //look up the salesmapping table in memory
                
        }
        else if(a.AE__c != null && trigger.oldmap.get(a.id).AE__c != trigger.newmap.get(a.id).AE__c){
            SDSAD.SSDSD(UpdateSDSAE);            
        }
        }
}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Class  and Method : 

public class SDSAD {
    public static void SSDSD(List<Account>UpdateSDSAE){
    list<account> shiva = [Select Id, AE__c from Account where Id In: UpdateSDSAE];
    list<sales_mapping__c> Koushik = [Select Id, AE__c, mapping_type__c,SDS__c from Sales_mapping__c];
        for(account a : shiva){
            for(sales_mapping__c s: koushik) {
                if (a.AE__c == s.AE__c && s.mapping_type__c == 'AE/SDS'){
                a.SDS__c = s.SDS__c;                    
                }
                else if(a.AE__c == s.AE__c && s.mapping_type__c == 'AE/IWAE'){
                    a.iWayAE__c = s.sds__c; 
                }
            }
        }
    }
}

The trigger calls the class based on condition, but i need to bulkify the class without changing the functionality.

Note : Account and Sales_Mapping__c Objects are not related in any way, the sales mapping Object has all mapped AE and SDS users, hence i need to get SDS value from Sales_mapping__c object when AE is the account owner.
Best Answer chosen by Venkata Shiva Koushik Rachapudi
Amit Chaudhary 8Amit Chaudhary 8
Please update your trigger like below
trigger SDS_AE_onAccount on Account (before update) {
//load salesmapping table in memory
List<Sales_mapping__c> Shiva = [Select OwnerId, mapping_type__c,AE__c from sales_mapping__c];    
List<Account> UpdateSDSAE = new List<Account>();
    for(account a: Trigger.new)
    {
        if(a.Owner_profile__c == 'SDS' || a.Owner_profile__c == 'SDS Manager')
        {
            a.SDS__c = a.OwnerId;
            UpdateSDSAE.add(a);
        }
        else if(a.Owner_Profile__c=='AE' && trigger.Oldmap.get(a.Id).SDS__c == trigger.Newmap.get(a.Id).SDS__c)
        {
			a.AE__c = a.OwnerId;
			UpdateSDSAE.add(a);
			// SDSAD.SSDSD(UpdateSDSAE);
			//look up the salesmapping table in memory
        }
        else if(a.AE__c != null && trigger.oldmap.get(a.id).AE__c != trigger.newmap.get(a.id).AE__c){
            //SDSAD.SSDSD(UpdateSDSAE);            
        }
	}
	if(UpdateSDSAE.size() >0 ){
        SDSAD.SSDSD(UpdateSDSAE);            
	}
	
}

Apex class like below
public class SDSAD {
    public static void SSDSD(List<Account>UpdateSDSAE){
	
		list<account> shiva = [Select Id, AE__c from Account where Id In :UpdateSDSAE];

		list<sales_mapping__c> Koushik = [Select Id, AE__c, mapping_type__c,SDS__c from Sales_mapping__c];
		Map<String , sales_mapping__c> mapAEWiseSM = new Map<String , sales_mapping__c>();
		for(sales_mapping__c sm: Koushik){
			mapAEWiseSM.put(sm.AE__c,sm);
		}
		
		
        for(account a : shiva){
		
			if(mapAEWiseSM.containsKey(a.AE__c)){
				sales_mapping__c sm =mapAEWiseSM.get(a.AE__c);
				if(sm.mapping_type__c == 'AE/SDS'){
					a.SDS__c = s.SDS__c;                    
				}
				else if( sm.mapping_type__c == 'AE/IWAE'){
                    a.iWayAE__c = s.sds__c; 
				}
			}
        }
    }
}

Let us know if this will help you