I have a batch apex class to update a value on the opportunity but I cant seem to get the value updated and cant figure out which part is wrong. In my query editor the getquerylocator SOQL seem to be fine.

I see no error in my Apex job log only 0 batches.

I I use system.debug() in the class I dont see these in the log

global class RenewalValueOpp implements 
Database.Batchable<sObject>, Database.Stateful {

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT Id,Opportunity.Name, (SELECT Id FROM Orders__r) FROM Opportunity WHERE CreatedBy.Name='+'\''+'John Visher'+'\''+' AND Stagename='+'\''+'DMU Check'+'\''+' AND Type='+'\''+'Renewal'+'AND Createddate=TODAY'+'\''


    global void execute(Database.BatchableContext bc, List<Opportunity> scope){
        // process each batch of records   
    for(Opportunity r : scope){
      double TotOrder = 0;

      List<Order__c> OrderAmount =[SELECT id, Order_Amount__c FROM Order__c WHERE Opportunity_Name__r.Id=:r.Id];

      System.debug('OrderAmount '+ OrderAmount);
        for(Order__c o : OrderAmount){ 
          TotOrder += o.Order_Amount__c; 
     Update Scope;        

    global void finish(Database.BatchableContext bc){
        //  System.debug(recordsProcessed + ' records processed.');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
                            TotalJobItems, CreatedBy.Email
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
        // call some utility   
  • You will get a separate log for each batch execute. Are you seeing any failures in your Apex Job logs?
    – Adrian Larson
    Dec 19, 2017 at 21:04
  • Also based on the number of opportunities that you have in your scope I see an issue where you have a query inside of the for loop that runs through your scope. I would suggest creating a list of Opp Ids and using that to find the Order__c records you are querying for. Dec 19, 2017 at 21:09
  • @AdrianLarson I see no error in my Apex job log only 0 batches. In my logs in the developer console I see 2 Bach apex logs with succes. What is weard is that I see SOQL_EXECUTE_BEGIN [16]|Aggregations:1|SELECT Id,Opportunity.Name, (SELECT Id FROM Orders__r) FROM Opportunity . The where part is not there
    – Thomas
    Dec 19, 2017 at 21:14
  • Ok, so zero batches is really useful information that you should edit into your question. That tells us the error is in the start method rather than execute.
    – Adrian Larson
    Dec 19, 2017 at 21:16

2 Answers 2


Since you are getting 0 executions, you now know your query is not returning any records. Here is the raw SOQL:

SELECT Id,Opportunity.Name, (SELECT Id FROM Orders__r)
FROM Opportunity
WHERE CreatedBy.Name='John Visher'
AND Stagename='DMU Check'
AND Type='RenewalAND Createddate=TODAY'

So, right off the bat the obvious problem is the last line where you mashed two criteria together. It should instead be:

AND Type='Renewal'
AND Createddate=TODAY

This mistake is just one reason you should write out your query statically instead of dynamically. It will be much easier to spot this kind of error.

Another change I really strongly advise is to query your child records in your execute block. There are a lot of posts out there detailing problems people have had which ultimately boiled down to this root cause.

Considering all of the above, I would start fixing this problem by changing your start method body to:

return Database.getQueryLocator([
    SELECT Name
    FROM Opportunity
    WHERE CreatedBy.Name='John Visher'
    AND Stagename='DMU Check'
    AND Type='Renewal'
    AND Createddate=TODAY

If you still get zero executions, plug this query into your Query Editor (or a similar tool) and remove criteria one at a time to see which is/are winnowing out your data.

  • Thanks that clarified I still am a little confused why I ended up trying to Use the SOQL as string . But your approach made it much simpeler to figure out what was wrong. It works!.
    – Thomas
    Dec 19, 2017 at 21:39

You SOQL query string is bad:

  • No closing ' on the Type string (Renewal)
  • It is missing whitespace between "Renewal" and the last AND.

FWIW, all the concatenation is unnecessary:

'SELECT Id, Opportunity.Name, (SELECT Id FROM Orders__r) FROM Opportunity WHERE CreatedBy.Name = \'John Visher\' AND StageName = \'DMU Check\' AND Type = \'Renewal\' AND Createddate = TODAY'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .