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
lakshmi.sf9lakshmi.sf9 

caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.

Hi,

                I written one batch class,and schedular class.From trigger I am calling these two calsses.

                if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')]<5){
                           database.executebatch(updateContact,200);
                }
                else{              
                            scheduledupdateContactOwner sc = new scheduledupdateContactOwner(UpdateContacts);
                            Datetime dt = Datetime.now() + (0.024305); // i.e. 30 mins
                            String timeForScheduler = dt.format('s m H d M \'?\' yyyy');
                            Id schedId = System.Schedule('ContactOwnerUpdate'+timeForScheduler,timeForScheduler,sc);
                }


But i am getting below error In the below Query 

if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')]<5)

If I run this query with out filters also results are less than 800.If I seacrh in google In every post inframtion is need to give more filters.

Can anyone expalin how to solve this issue..

Thanks in advance,


nbknbk
It is depends on the data you have in the org, for example you have huge records in the Account object and the batch class is referring to fetch the accounts data you might get the issue.

Identify 'updateContact' class have any select statements to return more data, if yes use the limits and put the where conditions possible.

The issue is throwing internal classes execution rather than if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')]<5).
Mikola SenykMikola Senyk
In your SOQL query you try to find count of async jobs that are processing or prepearing. However, you are looking through whole AsyncApexJob records.
It isn't necessary. You should be interested in jobs that were created last time. So, try to limit amount of AsyncApexJob records by using WHERE condition for CreatedDate. For example, for last 5 days:
DateTime dt = DateTime.now().addDays(-5);
if ([SELECT count() FROM AsyncApexJob WHERE CreatedDate > :dt AND JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')]<5){
   ...


lakshmi.sf9lakshmi.sf9
Thanks for ur reply,

here is my code

-------Triggers-------------
  {
   for(Account currentAcc:Trigger.new){
                A.Triggers.AccountUpdateContactowner.init(currentAcc,Trigger.oldmap.get(currentAcc.id));
            }
   A.Triggers.AccountUpdateContactowner.Process(Trigger.new);
  }
 
  ------class----------
  public void Init(Account currentAccount,Account previousAccount) {
            if( currentAccount.OwnerId != previousAccount.OwnerId && !currentAccount.IsPersonAccount){
                accountMap.put(currentAccount.id,currentAccount.ownerid);
            }
        }
  public List<Account> Process(List<Account> Accounts) {
  list<Contact> UpdateContacts = new list<Contact>();
        list<Contact> batchUpdateContacts = new list<Contact>();
            if(!accountMap.isempty()){
                for(Contact c: [select Id, OwnerId, AccountId from Contact where AccountId IN :accountMap.keySet() ]) {
                    c.OwnerId = accountMap.get(c.AccountId);
                     UpdateContacts.add(c);                   
                }
                DateTime today =  system.now();
                updateContactOwner updateContact = new updateContactOwner(UpdateContacts);
                if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')]<5){
                database.executebatch(updateContact,200);
                }
                else{              
                scheduledupdateContactOwner sc = new scheduledupdateContactOwner(UpdateContacts);
                Datetime dt = Datetime.now() + (0.024305); // i.e. 30 mins
                String timeForScheduler = dt.format('s m H d M \'?\' yyyy');
                Id schedId = System.Schedule('ContactOwnerUpdate'+timeForScheduler,timeForScheduler,sc);
                }
                UpdateContacts.clear();
            }
            return Accounts;
        }
 
  ---batch class----------
 
  global class updateContactOwner implements Database.Batchable<Contact>{
        global List<Contact> contactRecords = new List<Contact>();
        global updateContactOwner(List<Contact> contactList){
           contactRecords = contactList;
        }
        global Iterable<Contact> start(database.batchablecontext BC){
           return (contactRecords);   
        }
        global void execute(Database.BatchableContext BC, List<Contact> scope){
          set<id> ids = new set<id>();
           List<Contact> contactList = new List<Contact>();
            for(Contact c : contactRecords){
    ids.add(c.AccountId);
            }
            for(Id i : ids ){
               for(Contact c : scope){
                   if(c.AccountId == i){
                   contactList.add(c);
                   }      
               }
               update contactList;
               contactList.clear();
            }
       }   
       global void finish(Database.BatchableContext BC){
       }   
}
lakshmi.sf9lakshmi.sf9
If I excute below Query reult is zero.
SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')

If I exucute below query results are 275365
SELECT count() FROM AsyncApexJob

If I exceute below query results are 766
SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex'

Mikola SenykMikola Senyk
Did you try to put my changes into your code?
lakshmi.sf9lakshmi.sf9
yes I tried ur code.

DateTime lastTwoDays = DateTime.now().addDays(-2);                    
if ([SELECT count() FROM AsyncApexJob WHERE CreatedDate >:lastTwoDays AND JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')]<5){
}

Still it is not working...
Mikola SenykMikola Senyk
Hmm,

It seems that exception is thrown in different place. Look at the line of code before:
updateContactOwner updateContact = new updateContactOwner(UpdateContacts);
And try to find any SOQL statements inside class UpdateContactOwner as Nbk user suggested. It could be cause of your trouble.

lakshmi.sf9lakshmi.sf9
Thanks for ur relpy,

But I am not using soql Queries in updateContactOwner class.Just passing contatc records list .I posted my code in last two posts back..
Please check and suggest to me.
Mikola SenykMikola Senyk
Try to avoid of using group function like COUNT():
DateTime lastTwoDays = DateTime.now().addDays(-2);
List<AsyncApexJob> aJobList = [
    SELECT Id
    FROM AsyncApexJob
    WHERE CreatedDate >:lastTwoDays
    AND JobType='BatchApex'
    AND (Status = 'Processing' OR Status = 'Preparing')
    ORDER BY CreatedDate DESC
    LIMIT 5
];
if ( aJobList.size() < 5 ){
}
lakshmi.sf9lakshmi.sf9
Thanks for imm response...

But again same error............
Mikola SenykMikola Senyk
Maybe the following code will help:
Set<String> statuses = new Set<String> {'Processing', 'Preparing'};
List<AsyncApexJob> aJobList = [
    SELECT Id
    FROM AsyncApexJob
    WHERE CreatedDate >:lastTwoDays
    AND JobType='BatchApex'
    AND Status IN :statuses
    ORDER BY CreatedDate DESC
    LIMIT 5
];
Sometimes it is really hard to create the selective query.
lakshmi.sf9lakshmi.sf9
I tried this...again same error
lakshmi.sf9lakshmi.sf9
thanks for ur help Mikola SenykMikola Seny.This is urgent..can u please help on this
Mikola SenykMikola Senyk
Hmm, a hard case.
Make sure that exception take place in this query and not in different part of your code. Just for double check.
Maybe the following SOQL query will help:
List<AsyncApexJob> aJobList = [
    SELECT Id
    FROM AsyncApexJob
    WHERE Status IN :statuses
    AND JobType='BatchApex'
    ORDER BY CreatedDate DESC
    LIMIT 5
];