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
wlevywlevy 

Apex Batch does nothing

I am trying to run my first Apex Batch job. In this case it's a trivial update to a custom object, just a "proof of concept" as I learn the platform.

The problem is that nothing gets updated in the database. The debug log in the Anonymous Execution view even shows "Number of SOQL queries: 0 out of 100".

Following is my Apex class and the Execute Anonymous script:

 

global class Batch_UpdateTargetField implements Database.Batchable<SObject> {
    global Database.Querylocator start(Database.BatchableContext context) {
        System.debug('Start!');
        String query = 'SELECT SourceField__c, TargetField__c FROM MyCustomObject__c';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext context, List<SObject> scope) {
        for (SObject rec : scope) {
            MyCustomObject__c foo = (MyCustomObject__c)rec;
            rec.put(foo.TargetField__c, TargetFieldFromSourceField(foo.SourceField__c));
        }
        update scope;
    }
    string TargetFieldFromSourceField(string value) {
        return 'Foobar';
    }
    global void finish(Database.BatchableContext context) {
        System.debug('Finished');
    }
}

 

Script:

Batch_UpdateTargetField batch = new Batch_UpdateTargetField();
Id jobId = Database.executeBatch(batch);
System.debug('Started Batch Apex job: ' + jobId);

 

What am I missing here???

 

Thanks

colemabcolemab

I think you need an extra clause in your SOQL - such as LIMIT, WHERE, etc.

wlevywlevy

Why would I need that?

 

In any case I tried adding LIMIT 100 and it made no difference.

wlevywlevy

I simplified the Apex batch class so it just executes some System.debug calls and gets data in the start method. It seems like nothing happens at all, not even the debug statement at the beginning of my start method.

 

Here are the results in the Execute Anonymous view:

 

Anonymous execution was successful.

24.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: Batch_Update batch = new Batch_Update();
Execute Anonymous: Id jobId = Database.executeBatch(batch);
Execute Anonymous: System.debug('Started Batch Apex job: ' + jobId);
08:14:15.071 (71968000)|EXECUTION_STARTED
08:14:15.071 (71983000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
08:14:15.076 (76313000)|METHOD_ENTRY|[1]|01pd0000001mrQw|Batch_Update.Batch_Update()
08:14:15.076 (76439000)|METHOD_EXIT|[1]|Batch_Update
08:14:15.076 (76490000)|CONSTRUCTOR_ENTRY|[1]|01pd0000001mrQw|<init>()
08:14:15.076 (76566000)|CONSTRUCTOR_EXIT|[1]|01pd0000001mrQw|<init>()
08:14:15.112 (112597000)|USER_DEBUG|[3]|DEBUG|Started Batch Apex job: 707d0000003Rl56AAC
08:14:15.946 (115176000)|CUMULATIVE_LIMIT_USAGE
08:14:15.946|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 3 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

08:14:15.946|CUMULATIVE_LIMIT_USAGE_END

08:14:15.115 (115209000)|CODE_UNIT_FINISHED|execute_anonymous_apex
08:14:15.115 (115221000)|EXECUTION_FINISHED

 

Can someone please help me here? This is driving me nuts!

 

Thanks

colemabcolemab

The limit / where clause was to avoid going over limits due to a lack of batch size in your call.

 

Perhaps you just need to put a batch size in your database.executebatch call?

wlevywlevy

Does my batch class code look correct? Doe the Execute Anonymous script look OK? I think I am doing everything "by the book" for my proof of concept example. The custom object I'm querying has only a handful of records.

 

I tried adding the optional "scope" parameter to Database.ExecuteBatch and as expected that made no difference.

 

Still need help!!!!

wlevywlevy

Can I get some help on this?

wlevywlevy

Still trying to get a solution! Please help!