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
Kaylee GrayKaylee Gray 

Mass Reassign accounts by location

I am trying to transfer ownership of some 294k account records  to new owners based on the Billing Zip of the account and the owner's store location. We have geolocation setup, it was being used to assign incoming leads similarly (but into queues). My plan was to have a custom field on the accounts populated with the store number of the closest store, then assign owners from there. In order to do this I attempted to utilize the code from the trigger that was assigning leads to the queues, but quickly realized whoever designed it was using a DML statement in a for loop. Below is my new trigger code with the DML still in the loop, I have been trying for a a day or 2 to come up with a way to get the DML out of the for loop and still find the closest store to populate the ReAssignStore__c field, with no luck. Any help is so much appreciated.
Trigger Code:

trigger ReAssignJennTrig on Account (Before insert, Before Update) {
    ApexTrigger testTrigger= [SELECT id, Status, Name FROM ApexTrigger WHERE Name =: 'ReAssignJennTrig'];
         If( testTrigger.Status=='Inactive'){
             return;
         }else
         {
    List<Account> accounts = new List<Account>();
    
    Map<String, ZipToLatLong__c> zipToLatLongMap = new Map<String, ZipToLatLong__c>(); 
    User jenn = [SELECT id FROM User WHERE Name =:'Jenn Morrison'];
             
    for(Account ac : trigger.new){
        if(ac.OwnerID==jenn.id){
            
            accounts.add(ac);
        }
    }
System.debug(accounts.size()+'accounts List size after insert');
    if (accounts.size() > 0){
			// update ziptoLatlong map
			zipToLatLongMap = ZipToLatLong__c.getAll(); 
				
		}
             
    For(Account acct : accounts){
    if (!String.isBlank(String.valueOf(acct.BillingPostalCode)) && // ensure postalcode is not blank
					zipToLatLongMap.containsKey(String.valueOf((acct.BillingPostalCode))) // ensure zip is not missing from the zipToLatLongMap 
				){ 
				Decimal dLat = zipToLatLongMap.get(String.valueOf((acct.BillingPostalCode))).Latitude__c; system.debug(dLat);
				Decimal dLon = zipToLatLongMap.get(String.valueOf((acct.BillingPostalCode))).Longitude__c; system.debug(dLon);
				Store__c[] sList = [SELECT id, name FROM Store__c WHERE DISTANCE(Location__c, GEOLOCATION(:dLat,:dLon),'mi') < 5000 ORDER BY DISTANCE(Location__c, GEOLOCATION(:dLat,:dLon),'mi') LIMIT 1];	
                    
					if (sList.size() == 1){ // Check to see whether a store was returned
	
					acct.ReAssignStore__c = sList[0].name;  // assign lead to store queue
					
				} else { // Assign to generic queue if no store is matched
					acct.ReAssignStore__c = 'none';
				}
                }else {acct.ReAssignStore__c = 'none';} 	
                  
 
		}
	
    }}


Test Code:
@isTest (seeAllData=true)

public class ReAssignJennTEST {
	
       static testMethod void ReAssignTestGoodZip(){
        User jenn = [SELECT id FROM User WHERE Name = 'Jenn Morrison'];
           Account testAct = new Account();
   			String name ='name';
    		String CID = 'TESTCIDNUMB';
        	String Zip ='32202';
        	testAct.Name=name;
       		testAct.Customer_ID__c=CID;
        	testAct.BillingPostalCode=zip;
           testAct.OwnerId = Jenn.id;
        
        test.startTest();
        	insert testAct;
        	Account act = [SELECT id,ReAssignStore__c,CUstomer_id__c,billingpostalcode,Longitude__c,Latitude__c FROM Account WHERE customer_id__c =:'TESTCIDNUMB'];
           System.assertEquals('029',act.ReAssignStore__c);
           
           test.stopTest();
    }
        
     static testMethod void ReAssignTestBadZip(){
        User jenn = [SELECT id FROM User WHERE Name = 'Jenn Morrison'];
           Account testAct = new Account();
   			String name ='name';
    		String CID = 'TESTCIDNUMB';
        	String Zip ='322';
        	testAct.Name=name;
       		testAct.Customer_ID__c=CID;
        	testAct.BillingPostalCode=zip;
        	testAct.OwnerId = Jenn.id;
         
        test.startTest();
        	insert testAct;
        	Account act = [SELECT id,ReAssignStore__c,CUstomer_id__c,billingpostalcode FROM Account WHERE customer_id__c =:'TESTCIDNUMB'];
           System.assertEquals('none',act.ReAssignStore__c);
           test.stopTest();
    }
    
    /*static testMethod void ReAssignTest200(){
        User jenn = [SELECT id FROM User WHERE Name = 'Jenn Morrison'];
           
   			String name ='name';
    		String CID = 'TESTCIDNUMB';
        	String Zip ='32202';
        List<account> accounts200=new List<Account>();
        for(integer i=0;i<100;i++){
            Account testAct = new Account();
        	testAct.Name=name+i;
       		testAct.Customer_ID__c=CID+i;
        	testAct.BillingPostalCode=zip;
        	testAct.OwnerId = Jenn.id;
            accounts200.add(testAct);
        }
        test.startTest();
        	insert accounts200;
        	List<Account> accts = [SELECT id,ReAssignStore__c,CUstomer_id__c,billingpostalcode FROM Account WHERE ReAssignStore__c =:'029'];
           System.assertEquals(100,accts.size());
           test.stopTest();
    }*/
     static testMethod void ReAssignTestNoZip(){
        User jenn = [SELECT id FROM User WHERE Name = 'Jenn Morrison'];
           Account testAct = new Account();
   			String name ='name';
    		String CID = 'TESTCIDNUMB';
        	String Zip ='';
        	testAct.Name=name;
       		testAct.Customer_ID__c=CID;
        	testAct.BillingPostalCode=zip;
        	testAct.OwnerId = Jenn.id;
         
        test.startTest();
        	insert testAct;
        	Account act = [SELECT id,ReAssignStore__c, CUstomer_id__c,billingpostalcode FROM Account WHERE customer_id__c =:'TESTCIDNUMB'];
           System.assertEquals('none',act.ReAssignStore__c);
           test.stopTest();
    }
}