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
Will BauzonWill Bauzon 

Need help with bulkifying trigger and deploying into prod

Hi there,

I'm using the following code to trigger a lookup to a custom object on the account. I'm currently hitting SOQL limits on this and I think its because I have a DML statement inside the loop.
 
trigger AccountTerritoryTag on Account (before insert, before update)
{
    List<Account> accountsToUpdate = new List<Account>();

    for (Account account : Trigger.new)
    {    
      if (account.RunAssignment__c == TRUE)
      {
          // Find the territory using routing kew
          List<RoutingKey__c> routingkey = [select Name,Territory__c from RoutingKey__c
                             where Name = :account.RoutingKey__c limit 1];     
               
          // if you found one
          if (routingkey.size() > 0)
          {   
              //assign the territory 
              account.Territory__c = routingkey[0].Territory__c;
              account.RunAssignment__c = FALSE;
         
              accountsToUpdate.add(account);
         
          }
       }
    }
}

Also, when I try to deploy this trigger into prod using this class, I get a coverage error saying that there is no coverage at all even though I have 100% coverage in sandox.
 
@isTest

private class AccountTerritoryTest
{
    static testMethod void AccountTerritoryTest()
    { 
       RoutingKey__c k = new RoutingKey__c();
       k.Name = 'FL';
       insert k;
       
       Account a = new Account();
       a.Name  = 'Test';
       a.RoutingKey__c =  'FL';
       a.RunAssignment__c = TRUE;
       insert a;
       
       a.RunAssignment__c  =  FALSE;
       update a;
       
    }
    
}

I'm fairly new to coding and would greatly appreicate any help you could provide!
Best Answer chosen by Will Bauzon
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger AccountTerritoryTag on Account (before insert, before update)
{
	Set<String> setName =  new Set<String>();
    for (Account account : Trigger.new)
    {    	
		if (account.RunAssignment__c == TRUE)
		{
			setName.add(account.RoutingKey__c);
		}	
	}	
	
	if(setName.size() > 0 )
	{
		List<RoutingKey__c> routingkey = [select Name,Territory__c from RoutingKey__c  where Name in :setName ];
		Map<String, RoutingKey__c> mapNameWiseR = new  Map<String, RoutingKey__c> ();
		for(RoutingKey__c  RK : routingkey )
		{
			mapNameWiseR.put(RK.Name , RK);
		}

		for (Account account : Trigger.new)
		{    
		  if (account.RunAssignment__c == TRUE)
		  {
			  if (mapNameWiseR.containsKey(account.RoutingKey__c) )
			  {   
					RoutingKey__c rk = mapNameWiseR.get(account.RoutingKey__c);
				  //assign the territory 
					account.Territory__c = rk.Territory__c;
					account.RunAssignment__c = FALSE;
			 
			  }
		   }
		}
		
	}
		
}

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
trigger AccountTerritoryTag on Account (before insert, before update)
{
	Set<String> setName =  new Set<String>();
    for (Account account : Trigger.new)
    {    	
		if (account.RunAssignment__c == TRUE)
		{
			setName.add(account.RoutingKey__c);
		}	
	}	
	
	if(setName.size() > 0 )
	{
		List<RoutingKey__c> routingkey = [select Name,Territory__c from RoutingKey__c  where Name in :setName ];
		Map<String, RoutingKey__c> mapNameWiseR = new  Map<String, RoutingKey__c> ();
		for(RoutingKey__c  RK : routingkey )
		{
			mapNameWiseR.put(RK.Name , RK);
		}

		for (Account account : Trigger.new)
		{    
		  if (account.RunAssignment__c == TRUE)
		  {
			  if (mapNameWiseR.containsKey(account.RoutingKey__c) )
			  {   
					RoutingKey__c rk = mapNameWiseR.get(account.RoutingKey__c);
				  //assign the territory 
					account.Territory__c = rk.Territory__c;
					account.RunAssignment__c = FALSE;
			 
			  }
		   }
		}
		
	}
		
}

Let us know if this will help you
This was selected as the best answer
Will BauzonWill Bauzon
Hi Amit,

Thank you for helping with the trigger! I still get errors when I try to validate this in production using the test class above. SFDC says that I have 0% coverage. Can you help?
Amit Chaudhary 8Amit Chaudhary 8
I hope you are deploying Test class with trigger as well. Can you please share the screen shot of error ?
Will BauzonWill Bauzon
Hi Amit, I already have the test class deployed in production. When I attempt to deploy, I run only the specified test for the trigger. 

User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try to deploy trigger and test class together.
Will BauzonWill Bauzon
Hi Amit, I just tried to do that and recieved the same error.