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
Gabe Rothman 8Gabe Rothman 8 

Can anyone help me to bulk-safe this class? Any help would be greatly appreciated...

I'm trying to do a data-load of 3000 records, but it's failing because I'm hitting my query limit. I've had to limit my batch size to 1 record at a time in order to successfully allow Lexi Loader to work. The offending class is pasted below, can anyone help me bulkify it? Thanks!
 
public with sharing class setAccountGeoFromCountryService {
 
    public static void updateGeo(List<Account> Accounts) {
        List<String> listCountries = new List<String>();
        List<String> listStates = new List<String>(); 
        List<String> listZips = new List<String>(); 
        
        for (Account a : Accounts) {
            if(a.BillingCountry == null){
                return;
            }
            if(a.BillingCountry != null){
                listCountries.add(a.BillingCountry );
                if(a.BillingCountry  != 'US'){
					listStates.add('NULL');
                    listZips.add('NULL');
                }                
                if(a.BillingCountry  == 'US'){
                    listStates.add(a.BillingState );
                    if(a.BillingState  == 'CA' || a.BillingState  == 'California'){
                        listZips.add(a.BillingPostalCode);
                    }
                    if(a.BillingState  != 'CA' && a.BillingState  != 'California'){
                        listZips.add('NULL');
                    }
                }
            }
        }
        try{
            Territories__c territory = [SELECT Country_Code__c, Region__c, Theater__c, Country__c, State__c, State_Code__c, Postal_Code__c
                                            FROM Territories__c 
                                            WHERE Country_Code__c IN: listCountries 
                                        			AND (State__c =: listStates OR State_Code__c =: listStates)
                                       				AND Postal_Code__c =: listZips];
     system.debug(territory);
            for (Account a2: Accounts){
            	if(a2.Owner_is_Federal__c==true){
            		 a2.Geography__c = 'Federal';
            		 a2.Country2__c = territory.Country__c;
                     a2.Region__c = territory.Region__c;
            	}    
		     	if(a2.Owner_is_Federal__c==false){            	
	                 a2.Geography__c = territory.Theater__c;
	                 a2.Country2__c = territory.Country__c;
                     a2.Region__c = territory.Region__c;
		     	}                 
            }
        }catch(QueryException e){}
    }
}

 
Best Answer chosen by Gabe Rothman 8
pconpcon
I think there is a problem field literall"NULL" instead of null.  The code below should help you make this bulk ready
 
public with sharing class setAccountGeoFromCountryService {
    public static void updateGeo(List<Account> Accounts) {
        Set<String> listCountries = new Set<String>();
        Set<String> listStates = new Set<String>();
        Set<String> listZips = new Set<String>();

        for (Account a : Accounts) {
            if (a.BillingCountry == null) {
                continue;
            }

            listCountries.add(a.BillingCountry);

            if (a.BillingCountry != 'US') {
                listStates.add(null);
                listZips.add(null);
            } else {
                listStates.add(a.BillingState);
                if (
                    a.BillingState == 'CA' ||
                    a.BillingState == 'California'
                ){
                    listZips.add(a.BillingPostalCode);
                } else {
                    listZips.add(null);
                }
            }
        }

        Map<String, Map<String, Map<String, Territories__c>>> tMap = new Map<String, Map<String, Map<String, Territories__c>>>();

        for (Territories__c t : [
            select Country_Code__c,
                Region__c,
                Theater__c,
                Country__c,
                State__c,
                State_Code__c,
                Postal_Code__c
            from Territories__c
            where Country_Code__c in :listCountries or
                (
                    State__c = :listStates or
                    State_Code__c = :listStates
                ) or
                Postal_Code__c = :listZips
        ]) {
            if (!tMap.containsKey(t.Country_Code__c)) {
                tMap.put(t.Country_Code__c, new Map<String, Territory>());
            }

            String state = (t.State__c != null) ? t.State__c : t.State_Code__c;

            if (!tMap.get(t.Country_Code__c).containsKey(state)) {
                tMap.get(t.Country_Code__c).put(state, new Map<String, Territory>());
            }

            if (state == null) {
                tMap.get(t.Country_Code__c).get(null).put(null, t);
            } else {
                tMap.get(t.Country_Code__c).get(state).put(t.Postal_Code__c, t);
            }
        }

        for (Account a : Accounts) {
            Territories__c territory = tMap.get(a.BillingCountry).get(a.BillingState).get(a.BillingPostalCode);

            if (territory == null) {
                continue;
            }

            if (a2.Owner_is_Federal__c) {
                a2.Geography__c = 'Federal';
                a2.Country2__c = territory.Country__c;
                a2.Region__c = territory.Region__c;
            } else {
                a2.Geography__c = territory.Theater__c;
                a2.Country2__c = territory.Country__c;
                a2.Region__c = territory.Region__c;
            }
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors

All Answers

pconpcon
I think there is a problem field literall"NULL" instead of null.  The code below should help you make this bulk ready
 
public with sharing class setAccountGeoFromCountryService {
    public static void updateGeo(List<Account> Accounts) {
        Set<String> listCountries = new Set<String>();
        Set<String> listStates = new Set<String>();
        Set<String> listZips = new Set<String>();

        for (Account a : Accounts) {
            if (a.BillingCountry == null) {
                continue;
            }

            listCountries.add(a.BillingCountry);

            if (a.BillingCountry != 'US') {
                listStates.add(null);
                listZips.add(null);
            } else {
                listStates.add(a.BillingState);
                if (
                    a.BillingState == 'CA' ||
                    a.BillingState == 'California'
                ){
                    listZips.add(a.BillingPostalCode);
                } else {
                    listZips.add(null);
                }
            }
        }

        Map<String, Map<String, Map<String, Territories__c>>> tMap = new Map<String, Map<String, Map<String, Territories__c>>>();

        for (Territories__c t : [
            select Country_Code__c,
                Region__c,
                Theater__c,
                Country__c,
                State__c,
                State_Code__c,
                Postal_Code__c
            from Territories__c
            where Country_Code__c in :listCountries or
                (
                    State__c = :listStates or
                    State_Code__c = :listStates
                ) or
                Postal_Code__c = :listZips
        ]) {
            if (!tMap.containsKey(t.Country_Code__c)) {
                tMap.put(t.Country_Code__c, new Map<String, Territory>());
            }

            String state = (t.State__c != null) ? t.State__c : t.State_Code__c;

            if (!tMap.get(t.Country_Code__c).containsKey(state)) {
                tMap.get(t.Country_Code__c).put(state, new Map<String, Territory>());
            }

            if (state == null) {
                tMap.get(t.Country_Code__c).get(null).put(null, t);
            } else {
                tMap.get(t.Country_Code__c).get(state).put(t.Postal_Code__c, t);
            }
        }

        for (Account a : Accounts) {
            Territories__c territory = tMap.get(a.BillingCountry).get(a.BillingState).get(a.BillingPostalCode);

            if (territory == null) {
                continue;
            }

            if (a2.Owner_is_Federal__c) {
                a2.Geography__c = 'Federal';
                a2.Country2__c = territory.Country__c;
                a2.Region__c = territory.Region__c;
            } else {
                a2.Geography__c = territory.Theater__c;
                a2.Country2__c = territory.Country__c;
                a2.Region__c = territory.Region__c;
            }
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors
This was selected as the best answer
Gabe Rothman 8Gabe Rothman 8
Awesome, thanks so much for your response. I'm just running into one error in trying to implement your code changes. The system is telling me "Entity is not org-accessible" and I can't seem to locate the issue.  Any suggestions there?
Gabe Rothman 8Gabe Rothman 8
Quick follow up -- I've found the source of the error (lines 49 and 55), but I can't quite figure out how to fix it.
pconpcon
That's my fault. It should be territories__c instead of territory
Gabe Rothman 8Gabe Rothman 8
Thanks again pcon! You really helpd me out.  I made some additional changes to the code to fit some additional use cases.  Here it is if you're interested:
public with sharing class setAccountGeoFromCountryService {
    
    public static void updateGeo(List<Account> Accounts) {
        Set<String> listCountries = new Set<String>();
        Set<String> listStates = new Set<String>();
        Set<String> listZips = new Set<String>();
            for (Account a : Accounts) {
                if (a.BillingCountry == null) {
                    continue;
                }
                
                listCountries.add(a.BillingCountry);
                
                if (a.BillingCountry != 'US') {
                    listStates.add(null);
                    listZips.add(null);
                } else {
                    listStates.add(a.BillingState);
                    if (
                        a.BillingState == 'CA' ||
                        a.BillingState == 'California'
                    ){
                        listZips.add(a.BillingPostalCode);
                    } else {
                        listZips.add(null);
                    }
                }
            }
            
            Map<String, Map<String, Map<String, Territories__c>>> tMap = new Map<String, Map<String, Map<String, Territories__c>>>();
            Map<String, Map<String, Map<String, Territories__c>>> tMapStCode = new Map<String, Map<String, Map<String, Territories__c>>>();        
            Map<String, Map<String, Territories__c>> tMap2 = new Map<String, Map<String, Territories__c>>();
            Map<String, Map<String, Territories__c>> tMap2StCode = new Map<String, Map<String, Territories__c>>();        
            Map<String, Territories__c> tMap3 = new Map<String, Territories__c>();
            
            for (Territories__c t : [
                select Country_Code__c,
                Region__c,
                Theater__c,
                Country__c,
                State__c,
                State_Code__c,
                Postal_Code__c
                from Territories__c
                where Country_Code__c in :listCountries or
                (
                    State__c = :listStates or
                    State_Code__c = :listStates
                ) or
                Postal_Code__c = :listZips
            ]) {
/*----------- CA Map -------------*/                        
                if (!tMap.containsKey(t.Country_Code__c)) {
                    tMap.put(t.Country_Code__c, new Map<String, Map<String, Territories__c>>());
                }
                
                String state = t.State__c;
                
                if (!tMap.get(t.Country_Code__c).containsKey(state)) {
                    tMap.get(t.Country_Code__c).put(state, new Map<String, Territories__c>());
                }
                
                if (state == null) {
                    tMap.get(t.Country_Code__c).get(null).put(null, t);
                } else {
                    tMap.get(t.Country_Code__c).get(state).put(t.Postal_Code__c, t);
                }
                if (!tMapStCode.containsKey(t.Country_Code__c)) {
                    tMapStCode.put(t.Country_Code__c, new Map<String, Map<String, Territories__c>>());
                }
                
                String stateCode = t.State_Code__c;            
                
                if (!tMapStCode.get(t.Country_Code__c).containsKey(stateCode)) {
                    tMapStCode.get(t.Country_Code__c).put(stateCode, new Map<String, Territories__c>());
                }
                
                if (stateCode == null) {
                    tMapStCode.get(t.Country_Code__c).get(null).put(null, t);
                } else {
                    tMapStCode.get(t.Country_Code__c).get(stateCode).put(t.Postal_Code__c, t);
                }   
/*----------- Non-CA Map -------------*/            
                if (!tMap2.containsKey(t.Country_Code__c)) {
                    tMap2.put(t.Country_Code__c, new Map<String, Territories__c>());
                }
                
                if (!tMap2.get(t.Country_Code__c).containsKey(state)) {
                    tMap2.get(t.Country_Code__c).put(state, t);
                }                
                
                if (!tMap2StCode.containsKey(t.Country_Code__c)) {
                    tMap2StCode.put(t.Country_Code__c, new Map<String, Territories__c>());
                }
                
                if (!tMap2StCode.get(t.Country_Code__c).containsKey(stateCode)) {
                    tMap2StCode.get(t.Country_Code__c).put(stateCode, t);
                }                 
/*----------- Non-US Map -------------*/  
                if (!tMap3.containsKey(t.Country_Code__c)) {
                    tMap3.put(t.Country_Code__c,t);
                }
            }
            
            for (Account a : Accounts) {
                Territories__c territoryProxy = new Territories__c(); 
                if(a.BillingCountry == 'US' && a.BillingState == 'California' && a.BillingPostalCode != null){
                    Territories__c territory = tMap.get(a.BillingCountry).get(a.BillingState).get(a.BillingPostalCode);
                    territoryProxy = territory;                
                }
                if(a.BillingCountry == 'US' && a.BillingState == 'CA' && a.BillingPostalCode != null){
                    Territories__c territory = tMapStCode.get(a.BillingCountry).get(a.BillingState).get(a.BillingPostalCode);
                    territoryProxy = territory;                
                }            
                if(a.BillingCountry == 'US' && a.BillingState != null && a.BillingState != 'CA' && a.BillingState != 'California' && a.BillingState.Length()>2){
                    Territories__c territory = tMap2.get(a.BillingCountry).get(a.BillingState);
                    territoryProxy = territory;                
                }
                if(a.BillingCountry == 'US' && a.BillingState != null && a.BillingState != 'CA' && a.BillingState != 'California' && a.BillingState.Length()<=2){
                    Territories__c territory = tMap2StCode.get(a.BillingCountry).get(a.BillingState);
                    territoryProxy = territory;                
                }
                if(a.BillingCountry != 'US'){
                    Territories__c territory = tMap3.get(a.BillingCountry);
                    territoryProxy = territory;                
                }            
                if (territoryProxy == null) {
                    continue;
                }
                
                if (a.Owner_is_Federal__c) {
                    a.Geography__c = 'Federal';
                    a.Country2__c = territoryProxy.Country__c;
                    a.Region__c = territoryProxy.Region__c;
                } else {
                    a.Geography__c = territoryProxy.Theater__c;
                    a.Country2__c = territoryProxy.Country__c;
                    a.Region__c = territoryProxy.Region__c;
                }
            }
    }
    
}