+ Start a Discussion
lp okllllp oklll 

Trigger has hit a max query size:50001

This is my trigger but I am getting an error Trigger has hit a max query size:50001

The following trigger is trying to populate a lookup field APA on child account. This value is based on the value in another field call APA_Name__c which is populated as a text field on child account. I have 3 accounts of APA . . Please can someone help on this.

Trigger PopulateAPA on Account (before insert,before update) {

  // Get a list of all active record types within the system associated to Accounts
        List<Account> APAList = [Select Name, Id From Account];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> NewAPA = new Map<String,String>{};
                  
            for(Account APAAcc: APAList)
                NewAPA.put(APAAcc.Name,APAAcc.Id); 

  for (Account NewAccount : Trigger.new)
  {
  If(NewAccount.APA_Name__c == 'Global Heat Source')
      {
     
      NewAccount.APA__c = NewAPA.get('Global Heat Source');
       
       } 
  else If(NewAccount.APA_Name__c == 'Forrest')
      {
     
      NewAccount.APA__c = NewAPA.get('Forrest');
      }
  else If(NewAccount.APA_Name__c == 'Keepmoat')
      {
     
      NewAccount.APA__c = NewAPA.get('Keepmoat');
      }
   }
   }
Best Answer chosen by lp oklll
Chidambar ReddyChidambar Reddy
Hi,

The following code will work for you
 
Trigger PopulateAPA on Account (before insert,before update) {
    
    
    Set<String> APA_NameSet = new Set<string>(){'Global Heat Source','Forrest','Keepmoat'};
        // Get a list of all active record types within the system associated to Accounts
    List<Account> APAList = [Select Name, Id From Account WHERE NAME LIKE : APA_NameSet];
    
    // Create a map between the Account record Type Name and Id for for easy retrieval
    Map<String,String> NewAPA = new Map<String,String>{};
        
        for(Account APAAcc: APAList)
        	NewAPA.put(APAAcc.Name,APAAcc.Id); 
    
    for (Account NewAccount : Trigger.new)
    {
        If(NewAccount.APA_Name__c == 'Global Heat Source')
        {
            
            NewAccount.APA__c = NewAPA.get('Global Heat Source');
            
        } 
        else If(NewAccount.APA_Name__c == 'Forrest')
        {
            
            NewAccount.APA__c = NewAPA.get('Forrest');
        }
        else If(NewAccount.APA_Name__c == 'Keepmoat')
        {
            
            NewAccount.APA__c = NewAPA.get('Keepmoat');
        }
    }
}

 

All Answers

Chidambar ReddyChidambar Reddy
Hi,

You should get rid of this query 
List<Account> APAList = [Select Name, Id From Account];

What are APA list?  Do you have a specific criteria to get APA list?

like all account with Name 'APA'?
Chidambar ReddyChidambar Reddy
Hi,

The following code will work for you
 
Trigger PopulateAPA on Account (before insert,before update) {
    
    
    Set<String> APA_NameSet = new Set<string>(){'Global Heat Source','Forrest','Keepmoat'};
        // Get a list of all active record types within the system associated to Accounts
    List<Account> APAList = [Select Name, Id From Account WHERE NAME LIKE : APA_NameSet];
    
    // Create a map between the Account record Type Name and Id for for easy retrieval
    Map<String,String> NewAPA = new Map<String,String>{};
        
        for(Account APAAcc: APAList)
        	NewAPA.put(APAAcc.Name,APAAcc.Id); 
    
    for (Account NewAccount : Trigger.new)
    {
        If(NewAccount.APA_Name__c == 'Global Heat Source')
        {
            
            NewAccount.APA__c = NewAPA.get('Global Heat Source');
            
        } 
        else If(NewAccount.APA_Name__c == 'Forrest')
        {
            
            NewAccount.APA__c = NewAPA.get('Forrest');
        }
        else If(NewAccount.APA_Name__c == 'Keepmoat')
        {
            
            NewAccount.APA__c = NewAPA.get('Keepmoat');
        }
    }
}

 
This was selected as the best answer
lp okllllp oklll
Hi  Chidambar Reddy,

The code looks good and it works fine too. the only problem is that earlier i was getting 100% coverage but now i am getting 91%
Chidambar ReddyChidambar Reddy
You should have created accounts with Same Names in Test classes

Please share the test class code,

and choose the trigger answer as best answer
lp okllllp oklll
The Class has the following code


@isTest 
public class PopReselleroNGSMandCASE {
    static testMethod void insertNewGSMHandshake() {
    
        // Get a list of all active record types within the system associated to Accounts
        List<RecordType> accRTypes = [Select Name, Id From RecordType where sObjectType='Account'];

        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> accountRecordTypes = new Map<String,String>{};
        for(RecordType accRT: accRTypes)
            accountRecordTypes.put(accRT.Name,accRT.Id);
       
       Account testReseller = new Account();
    
    testReseller.Name='Test of the Account1';
    testReseller.type='Reseller';
    testReseller.RecordTypeId= accountRecordTypes.get('Reseller');
    testReseller.Shippingcountry='United Kingdom';
    insert testReseller;
    Account acc=[select id,name from account where id=:testReseller.id];
  
    
    
        Account testAPA = new Account();
    
    testAPA.Name='Test of the Account1';
    testApa.APA_Name__c='Forrest';
    testAPA.type='APA';
    testAPA.RecordTypeId= accountRecordTypes.get('APA');
    testAPA.Reseller__c = testReseller.id ;
    testAPA.Shippingcountry='United Kingdom';
    insert testAPA;
    
       Account testAPA1 = new Account();
    
    testAPA1.Name='Test of the Account1';
    testAPA1.APA_Name__c='Global Heat Source';
    testAPA1.type='APA';
    testAPA1.RecordTypeId= accountRecordTypes.get('APA');
    testAPA1.Reseller__c = testReseller.id ;
    testAPA1.Shippingcountry='United Kingdom';
    insert testAPA1;
    
       Account testAPA2 = new Account();
    
    testAPA2.Name='Test of the Account1';
    testAPA2.APA_Name__c='Keepmoat';
    testAPA2.type='APA';
    testAPA2.RecordTypeId= accountRecordTypes.get('APA');
    testAPA2.Reseller__c = testReseller.id ;
    testAPA2.Shippingcountry='United Kingdom';
    insert testAPA2;
    
       
      Account testHome = new Account();
    
    testHome.Name='Test of the Account1';
    testHome.type='Home';
    testHome.RecordTypeId= accountRecordTypes.get('Home');
    testHome.Shippingcountry='United Kingdom';
    testHome.Reseller__c = testReseller.id ;
    testHome.APA__c= testAPA.id;
    
    insert testHome;  
    
      Account testHome1 = new Account();
    
    testHome1.Name='Test of the Account1';
    testHome1.type='Home';
    testHome1.RecordTypeId= accountRecordTypes.get('Home');
    testHome1.Shippingcountry='United Kingdom';
    testHome1.Reseller__c = testReseller.id ;
    testHome1.APA__c= testAPA1.id;
    
    insert testHome1;  
    
      Account testHome2 = new Account();
    
    testHome2.Name='Test of the Account1';
    testHome2.type='Home';
    testHome2.RecordTypeId= accountRecordTypes.get('Home');
    testHome2.Shippingcountry='United Kingdom';
    testHome2.Reseller__c = testReseller.id ;
    testHome2.APA__c= testAPA2.id;
    
    insert testHome2;  
    
    
    GSM_Handshake__c testGSM_Handshake = new GSM_Handshake__c();
    
    testGSM_Handshake.Contact_Name__c='Test of the GSM_Handshake';
    testGSM_Handshake.Home_Account__c = testHome.id ;
        insert testGSM_Handshake;
        
     Case TestCase = new Case();
    
    TestCase.accountid = testHome.id ;
    TestCase.Reseller__c = testReseller.id;
    TestCase.APA__c = testAPA.id ;
        TestCase.status = 'New';
        insert TestCase;
    
        }
}
 
Chidambar ReddyChidambar Reddy
Hi

Try the following test class
 
@isTest 
public class PopReselleroNGSMandCASE {
    static testMethod void insertNewGSMHandshake() {
        
        // Get a list of all active record types within the system associated to Accounts
        List<RecordType> accRTypes = [Select Name, Id From RecordType where sObjectType='Account'];
        
        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> accountRecordTypes = new Map<String,String>{};
        for(RecordType accRT: accRTypes)
            accountRecordTypes.put(accRT.Name,accRT.Id);
        
        Account testReseller = new Account();
        testReseller.Name='Test of the Account1';
        testReseller.type='Reseller';
        testReseller.RecordTypeId= accountRecordTypes.get('Reseller');
        testReseller.Shippingcountry='United Kingdom';
        insert testReseller;
        Account acc=[select id,name from account where id=:testReseller.id];
        
        List<Account> childAccountsList = new List<Account>();
        
        Account testAPA = new Account();
        testAPA.Name='Test of the Account1';
        testApa.APA_Name__c='Forrest';
        testAPA.type='APA';
        testAPA.RecordTypeId= accountRecordTypes.get('APA');
        testAPA.Reseller__c = testReseller.id ;
        testAPA.Shippingcountry='United Kingdom';
        childAccountsList.add(testAPA);
        insert testAPA;
        
        
        Account testAPA1 = new Account();
        testAPA1.Name='Test of the Account1';
        testAPA1.APA_Name__c='Global Heat Source';
        testAPA1.type='APA';
        testAPA1.RecordTypeId= accountRecordTypes.get('APA');
        testAPA1.Reseller__c = testReseller.id ;
        testAPA1.Shippingcountry='United Kingdom';
        childAccountsList.add(testAPA1);
        insert testAPA1;
        
        Account testAPA2 = new Account();
        testAPA2.Name='Test of the Account1';
        testAPA2.APA_Name__c='Keepmoat';
        testAPA2.type='APA';
        testAPA2.RecordTypeId= accountRecordTypes.get('APA');
        testAPA2.Reseller__c = testReseller.id ;
        testAPA2.Shippingcountry='United Kingdom';
        childAccountsList.add(testAPA2);
        insert testAPA2;
        
        insert childAccountsList;
        
        Account testHome = new Account();
        testHome.Name='Test of the Account1';
        testHome.type='Home';
        testHome.RecordTypeId= accountRecordTypes.get('Home');
        testHome.Shippingcountry='United Kingdom';
        testHome.Reseller__c = testReseller.id ;
        testHome.APA__c= testAPA.id;
        
        insert testHome;  
        
        Account testHome1 = new Account();
        testHome1.Name='Test of the Account1';
        testHome1.type='Home';
        testHome1.RecordTypeId= accountRecordTypes.get('Home');
        testHome1.Shippingcountry='United Kingdom';
        testHome1.Reseller__c = testReseller.id ;
        testHome1.APA__c= testAPA1.id;
        insert testHome1;  
        
        Account testHome2 = new Account();
        testHome2.Name='Test of the Account1';
        testHome2.type='Home';
        testHome2.RecordTypeId= accountRecordTypes.get('Home');
        testHome2.Shippingcountry='United Kingdom';
        testHome2.Reseller__c = testReseller.id ;
        testHome2.APA__c= testAPA2.id;
        insert testHome2;  
        
        
        GSM_Handshake__c testGSM_Handshake = new GSM_Handshake__c();
        testGSM_Handshake.Contact_Name__c='Test of the GSM_Handshake';
        testGSM_Handshake.Home_Account__c = testHome.id ;
        insert testGSM_Handshake;
        
        Case TestCase = new Case();
        TestCase.accountid = testHome.id ;
        TestCase.Reseller__c = testReseller.id;
        TestCase.APA__c = testAPA.id ;
        TestCase.status = 'New';
        insert TestCase;
        
    }
}

 
lp okllllp oklll
this is the error now

System.DmlException: Insert failed. First exception on row 0 with id 001L000000cibI3IAI; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Chidambar ReddyChidambar Reddy
Hi

I thought of it,
 
@isTest 
public class PopReselleroNGSMandCASE {
    static testMethod void insertNewGSMHandshake() {
        
        // Get a list of all active record types within the system associated to Accounts
        List<RecordType> accRTypes = [Select Name, Id From RecordType where sObjectType='Account'];
        
        // Create a map between the Account record Type Name and Id for for easy retrieval
        Map<String,String> accountRecordTypes = new Map<String,String>{};
        for(RecordType accRT: accRTypes)
            accountRecordTypes.put(accRT.Name,accRT.Id);
        
        Account testReseller = new Account();
        testReseller.Name='Test of the Account1';
        testReseller.type='Reseller';
        testReseller.RecordTypeId= accountRecordTypes.get('Reseller');
        testReseller.Shippingcountry='United Kingdom';
        insert testReseller;
        Account acc=[select id,name from account where id=:testReseller.id];
        
        List<Account> childAccountsList = new List<Account>();
        
        Account testAPA = new Account();
        testAPA.Name='Test of the Account1';
        testApa.APA_Name__c='Forrest';
        testAPA.type='APA';
        testAPA.RecordTypeId= accountRecordTypes.get('APA');
        testAPA.Reseller__c = testReseller.id ;
        testAPA.Shippingcountry='United Kingdom';
        childAccountsList.add(testAPA.clone(false, false, false, false));
        insert testAPA;
        
        
        Account testAPA1 = new Account();
        testAPA1.Name='Test of the Account1';
        testAPA1.APA_Name__c='Global Heat Source';
        testAPA1.type='APA';
        testAPA1.RecordTypeId= accountRecordTypes.get('APA');
        testAPA1.Reseller__c = testReseller.id ;
        testAPA1.Shippingcountry='United Kingdom';
        childAccountsList.add(testAPA1.clone(false, false, false, false));
        insert testAPA1;
        
        Account testAPA2 = new Account();
        testAPA2.Name='Test of the Account1';
        testAPA2.APA_Name__c='Keepmoat';
        testAPA2.type='APA';
        testAPA2.RecordTypeId= accountRecordTypes.get('APA');
        testAPA2.Reseller__c = testReseller.id ;
        testAPA2.Shippingcountry='United Kingdom';
        childAccountsList.add(testAPA2.clone(false, false, false, false));
        insert testAPA2;
        
        insert childAccountsList;
        
        Account testHome = new Account();
        testHome.Name='Test of the Account1';
        testHome.type='Home';
        testHome.RecordTypeId= accountRecordTypes.get('Home');
        testHome.Shippingcountry='United Kingdom';
        testHome.Reseller__c = testReseller.id ;
        testHome.APA__c= testAPA.id;
        
        insert testHome;  
        
        Account testHome1 = new Account();
        testHome1.Name='Test of the Account1';
        testHome1.type='Home';
        testHome1.RecordTypeId= accountRecordTypes.get('Home');
        testHome1.Shippingcountry='United Kingdom';
        testHome1.Reseller__c = testReseller.id ;
        testHome1.APA__c= testAPA1.id;
        insert testHome1;  
        
        Account testHome2 = new Account();
        testHome2.Name='Test of the Account1';
        testHome2.type='Home';
        testHome2.RecordTypeId= accountRecordTypes.get('Home');
        testHome2.Shippingcountry='United Kingdom';
        testHome2.Reseller__c = testReseller.id ;
        testHome2.APA__c= testAPA2.id;
        insert testHome2;  
        
        
        GSM_Handshake__c testGSM_Handshake = new GSM_Handshake__c();
        testGSM_Handshake.Contact_Name__c='Test of the GSM_Handshake';
        testGSM_Handshake.Home_Account__c = testHome.id ;
        insert testGSM_Handshake;
        
        Case TestCase = new Case();
        TestCase.accountid = testHome.id ;
        TestCase.Reseller__c = testReseller.id;
        TestCase.APA__c = testAPA.id ;
        TestCase.status = 'New';
        insert TestCase;
        
    }
}

Can you quickly try this?