• naveen1685
  • NEWBIE
  • 25 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 6
    Replies

I am getting this exception even after I am providing LIMIT of 5000 to the following dynamic SOQL query:

 

strCamMemQuery = 'SELECT Id,Name,LastName,Initials__c,AccountId,Email ';
strQuery=' FROM Contact WHERE Id NOT IN :existingCampaignMembers ';
strQuery+='AND AccountId IN:acts ';

strCamMemQuery += ' LIMIT 5000';

 

List<Contact> cl = Database.query(strCamMemQuery);

 

Please let me know what is the causing the error.

 

Facing the issue "Too many SOQL queries: 101" at the highlighted line in the following code :

 

public with sharing class AccountTriggerHandler {
     
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
    public static boolean isDeletedByTrigger = false;
    public set<Id> recordTypeSet = new set<Id>();
    public AccountTriggerHandler(boolean isExecuting, integer size){
        m_isExecuting = isExecuting;
        BatchSize = size;
        for(RecordType rt : [Select Id, DeveloperName From RecordType Where DeveloperName Like '%Ukraine%' And SobjectType='Account']){
            recordTypeSet.add(rt.Id);
        }
    }
    
    public void OnAfterInsert(List<Account> newObjects){
         createNewContacts(newObjects);
    }

    private void createNewContacts(list<Account> accountList){
        /*set<Id> recordTypeSet = new set<Id>();
        for(RecordType rt : [Select Id, DeveloperName From RecordType Where DeveloperName Like '%Ukraine%' And SobjectType='Account']){
            recordTypeSet.add(rt.Id);
        }*/
        list<Contact> contactsToBeInserted = new list<Contact>();
        for (Account acc : accountList){
            if(recordTypeSet.contains(acc.RecordTypeId))
              contactsToBeInserted.add(new Contact (AccountId=acc.Id, LastName= acc.Name, Position__c = 'Virtual',Country__c=acc.Base__Country__c,City__c = acc.City__c));
        }
        if(contactsToBeInserted.size()>0)
           insert contactsToBeInserted;
    }
    
    public void OnAfterUpdate(map<Id, Account> newObjects, map<Id, Account> oldObjects){        
         updateContacts(newObjects, oldObjects);
    }
    
    private void updateContacts(map<Id, Account> accountNew, map<Id, Account> accountOld){
        
        /*List<RecordType> rectype = new list<RecordType>();
        set<Id> recordTypeSet = new set<Id>();
        rectype = [Select Id, DeveloperName From RecordType Where DeveloperName Like '%Ukraine%' And SobjectType='Account'];
        for(RecordType rt : rectype ){
            recordTypeSet.add(rt.Id);
        }*/
        
        set<Id> accountsWithNameChanges = new set<Id>();
        for (Id key : accountNew.keySet()){
            if(recordTypeSet.contains(accountNew.get(key).RecordTypeId)){
               if(accountOld.get(key).Name <> accountNew.get(key).Name){
                  accountsWithNameChanges.add(key);
               }
            }
        }
        
        list<Contact> contactsToBeUpdated = new list<Contact>();
        for(Contact con : [Select id, LastName,AccountId
                           From Contact 
                           Where AccountId IN :accountsWithNameChanges 
                            And Position__c = 'Virtual']){
            con.LastName = accountNew.get(con.AccountId).Name;
            contactsToBeUpdated.add(con);
        }
        if(contactsToBeUpdated.size() > 0)
            update contactsToBeUpdated;
    }
    
    public void OnBeforeDelete(map<Id, Account> oldObjects){
        deleteVirtualContacts(oldObjects);
    }
    
    private void deleteVirtualContacts(map<Id, Account> accountOld){
        isDeletedByTrigger = true;
        list<Contact> contactsToBeDeleted = [Select Id From Contact Where AccountId IN :accountOld.keySet() and Position__c = 'Virtual'];
        if(contactsToBeDeleted.size()>0)
            delete contactsToBeDeleted;         
    }
}

 

Please advice how to solve this issue.

 

Hi,

 

We are encountering the deployment error due to some test classes where batch apex class is called. The error occurring is:

"System.unexpectedException:No more than one executeBatch can be called within a test method."

 

In our test class, there are insert and update statements which in turn calls the batch apex from a trigger. We have also tried to limit the batch query by using "Test.isRunningTest()" method but we are again facing the same error.

The code works fine in sandbox and the error is coming only at the time of deployment to production.

Also, the test classes causing the error were working fine previously in the production.

 

Please provide some pointers/solution for the above mentioned error.

 

Thank you.

I am getting this exception even after I am providing LIMIT of 5000 to the following dynamic SOQL query:

 

strCamMemQuery = 'SELECT Id,Name,LastName,Initials__c,AccountId,Email ';
strQuery=' FROM Contact WHERE Id NOT IN :existingCampaignMembers ';
strQuery+='AND AccountId IN:acts ';

strCamMemQuery += ' LIMIT 5000';

 

List<Contact> cl = Database.query(strCamMemQuery);

 

Please let me know what is the causing the error.

 

Facing the issue "Too many SOQL queries: 101" at the highlighted line in the following code :

 

public with sharing class AccountTriggerHandler {
     
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
    public static boolean isDeletedByTrigger = false;
    public set<Id> recordTypeSet = new set<Id>();
    public AccountTriggerHandler(boolean isExecuting, integer size){
        m_isExecuting = isExecuting;
        BatchSize = size;
        for(RecordType rt : [Select Id, DeveloperName From RecordType Where DeveloperName Like '%Ukraine%' And SobjectType='Account']){
            recordTypeSet.add(rt.Id);
        }
    }
    
    public void OnAfterInsert(List<Account> newObjects){
         createNewContacts(newObjects);
    }

    private void createNewContacts(list<Account> accountList){
        /*set<Id> recordTypeSet = new set<Id>();
        for(RecordType rt : [Select Id, DeveloperName From RecordType Where DeveloperName Like '%Ukraine%' And SobjectType='Account']){
            recordTypeSet.add(rt.Id);
        }*/
        list<Contact> contactsToBeInserted = new list<Contact>();
        for (Account acc : accountList){
            if(recordTypeSet.contains(acc.RecordTypeId))
              contactsToBeInserted.add(new Contact (AccountId=acc.Id, LastName= acc.Name, Position__c = 'Virtual',Country__c=acc.Base__Country__c,City__c = acc.City__c));
        }
        if(contactsToBeInserted.size()>0)
           insert contactsToBeInserted;
    }
    
    public void OnAfterUpdate(map<Id, Account> newObjects, map<Id, Account> oldObjects){        
         updateContacts(newObjects, oldObjects);
    }
    
    private void updateContacts(map<Id, Account> accountNew, map<Id, Account> accountOld){
        
        /*List<RecordType> rectype = new list<RecordType>();
        set<Id> recordTypeSet = new set<Id>();
        rectype = [Select Id, DeveloperName From RecordType Where DeveloperName Like '%Ukraine%' And SobjectType='Account'];
        for(RecordType rt : rectype ){
            recordTypeSet.add(rt.Id);
        }*/
        
        set<Id> accountsWithNameChanges = new set<Id>();
        for (Id key : accountNew.keySet()){
            if(recordTypeSet.contains(accountNew.get(key).RecordTypeId)){
               if(accountOld.get(key).Name <> accountNew.get(key).Name){
                  accountsWithNameChanges.add(key);
               }
            }
        }
        
        list<Contact> contactsToBeUpdated = new list<Contact>();
        for(Contact con : [Select id, LastName,AccountId
                           From Contact 
                           Where AccountId IN :accountsWithNameChanges 
                            And Position__c = 'Virtual']){
            con.LastName = accountNew.get(con.AccountId).Name;
            contactsToBeUpdated.add(con);
        }
        if(contactsToBeUpdated.size() > 0)
            update contactsToBeUpdated;
    }
    
    public void OnBeforeDelete(map<Id, Account> oldObjects){
        deleteVirtualContacts(oldObjects);
    }
    
    private void deleteVirtualContacts(map<Id, Account> accountOld){
        isDeletedByTrigger = true;
        list<Contact> contactsToBeDeleted = [Select Id From Contact Where AccountId IN :accountOld.keySet() and Position__c = 'Virtual'];
        if(contactsToBeDeleted.size()>0)
            delete contactsToBeDeleted;         
    }
}

 

Please advice how to solve this issue.

 

Hi,

 

We are encountering the deployment error due to some test classes where batch apex class is called. The error occurring is:

"System.unexpectedException:No more than one executeBatch can be called within a test method."

 

In our test class, there are insert and update statements which in turn calls the batch apex from a trigger. We have also tried to limit the batch query by using "Test.isRunningTest()" method but we are again facing the same error.

The code works fine in sandbox and the error is coming only at the time of deployment to production.

Also, the test classes causing the error were working fine previously in the production.

 

Please provide some pointers/solution for the above mentioned error.

 

Thank you.