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
Ross Gilbert 31Ross Gilbert 31 

failing scheduled batch job

This batch job runs once a day every day at 1am.

It works fine 50% of the time and doesn't process or fails the other 50%.  When it fails, I see no error message on the job history at all.  It just fails.

I put on debug logs last week on it and when it failde I noticed it had a very, very long duration (in ms) to process.  So I'm thinking something is causing this thing to time out when it runs, sometimes.

Does anyone see anything wrong with the code and in particular the soql query that might cause a timeout/job failure?  


global class updateAssetTermDates implements Database.Batchable<sObject>, Schedulable{

    string query;
    
    global void execute(SchedulableContext SC) 
    {
        updateAssetTermDates  x = new updateAssetTermDates(); 
        database.executebatch(x);
    }

    global Database.querylocator start(Database.BatchableContext BC){
        Query = 'Select id, Entitlement__r.EndDate, Previous_Annual_Maintenance__c, Annual_Maintenance__c, Future_Annual_Maintenance__c, Term_Begin_Date__c, Term_End_Date__c, Term_Start_Date_Hidden__c, Term_End_Date_Hidden__c  FROM Customer_Asset__c WHERE Term_Start_Date_Hidden__c = TODAY AND Maintenance_Status__c = \'On Subscription\' ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Customer_Asset__c> scope1){
        List<Customer_Asset__c> assList = new List<Customer_Asset__c>();
        Set<Id> assEntId = new Set<Id>();
        Date d;
        
            for(Customer_Asset__c ca : Scope1){
                ca.Term_Begin_Date__c = ca.Term_Start_Date_Hidden__c;
                ca.Term_End_Date__c = ca.Term_End_Date_Hidden__c;
                d= ca.Term_End_Date__c;
                if(ca.Future_Annual_Maintenance__c != NULL)
                {
                    ca.Previous_Annual_Maintenance__c = ca.Annual_Maintenance__c;
                    ca.Annual_Maintenance__c = ca.Future_Annual_Maintenance__c;
                    ca.Future_Annual_Maintenance__c = NULL;
                }
                
                assList.add(ca);
                assEntId.add(ca.Entitlement__c);
            } 
                  
        try{
                update assList;
        } catch (Exception e) {         
            Error_Log__c log = new Error_Log__c();
            log.trace__c = 'Type: ' + e.getTypeName() + '\n' + 'Cause: ' + e.getCause() + '\n' + 'Message: ' 
            + e.getMessage() + '\n' + 'Line #: ' + e.getLineNumber() + '\n' + e.getStackTraceString() + '\n';
           // + 'Some Custom Variable Information From Class: ' + myClassVariable;
            insert log;         
        }        

        List<Entitlement> entList = new List<Entitlement>();
        
        for(Entitlement e: [select id from Entitlement where ID =: assEntId]){
            e.EndDate = d;
            entList.add(e);
        }
        
        try{
                update entList;
        } catch (Exception e) {         
            Error_Log__c log = new Error_Log__c();
            log.trace__c = 'Type: ' + e.getTypeName() + '\n' + 'Cause: ' + e.getCause() + '\n' + 'Message: ' 
            + e.getMessage() + '\n' + 'Line #: ' + e.getLineNumber() + '\n' + e.getStackTraceString() + '\n';
           // + 'Some Custom Variable Information From Class: ' + myClassVariable;
            insert log;         
        }
        
    }

    global void finish(Database.BatchableContext BC){
    }
    
}

 
Alain CabonAlain Cabon
Hello,

How many rows in Customer_Asset__c  and for your scope? I am interested in time-out problems.

Alain

 
Ross Gilbert 31Ross Gilbert 31
The object itself has only 30,000 rows.  The scope is way less than that...usually no more than 1-20 records total on a given day.  It just makes no sense to me why this batch job fails.  I have had a cas open with Salesforce support for a week and their developer has found no reason for why this job fails.
Alain CabonAlain Cabon
Ok, a time out occurs with much more rows logically.

The entitlements are complex processes. 

Could you test with "ID IN :assEntId" instead of "ID =" and without this update of the entitlements at all ( // update entList; ) ?
List<Entitlement> entList = new List<Entitlement>();
        
 for(Entitlement e: [select id from Entitlement where ID IN :assEntId]){
         e.EndDate = d;
         entList.add(e);
}

Alain