0

I have this below soql query in one batch class start method. Even if I run the batch class with the below-matching query, I am not getting anything. I have few records with this condition but not getting anything even I run the Batch. I think something problem with this query. Can anyone correct this query, please?

  String query = 'SELECT Id, Name, Account_Type__c, ' 
                    +'(SELECT Id, Name, Account__c, Application_Status__c, Training_Requirements_Met__c, Training_Override__c, Level__r.Name , Partner_Type__r.Name , Solution_Type__r.Name FROM Accreditations__r WHERE Application_Status__c = \'Legal Approved/Awaiting Training Completion\' OR Application_Status__c = \'Partner Agreement Accepted\' ),' 
                    +'(SELECT Id, Account__c, Active__c, Student_Id__c, Training_Course__r.Name, Expiration_Date__c FROM Certifications__r ORDER BY Student_Id__c)'
                    +'FROM Account WHERE Account_Type__c includes (\'Channel\')';
    system.debug('========='+ query);        
4
  • Have you executed this query in developer console?
    – Reshma
    Commented Dec 2, 2016 at 9:09
  • 1
    are you getting any exception. Check the apex job page for details. Commented Dec 2, 2016 at 9:18
  • Are you running the batch with the same User who runs the SOQL Query ?
    – Akram G
    Commented Dec 2, 2016 at 9:40
  • Yes, I executed In developer console. It says "Unknown error parsing query", And Yes, I am running the batch with the same user who runs the query
    – user24737
    Commented Dec 2, 2016 at 9:57

1 Answer 1

0

The best way to check what the query returns is to run it in the Developer Console Query Editor. You can then quickly add and remove where terms to isolate where the problem is.

You do not need to use dynamic SOQL in a batch class. Using static SOQL allows the compiler to check your query (so you find mistakes earlier) and avoids you having to add escape characters and quotes so the query a little easier to read:

public Database.QueryLocator start(Database.BatchableContext bc) {
    return Database.getQueryLocator([
        SELECT
            Id, Name, Account_Type__c,
            (
                SELECT Id, Name, Account__c, Application_Status__c, Training_Requirements_Met__c, Training_Override__c, Level__r.Name, Partner_Type__r.Name, Solution_Type__r.Name
                FROM Accreditations__r
                WHERE Application_Status__c = 'Legal Approved/Awaiting Training Completion'
                OR Application_Status__c = 'Partner Agreement Accepted'
            ),
            (
                SELECT Id, Account__c, Active__c, Student_Id__c, Training_Course__r.Name, Expiration_Date__c
                FROM Certifications__r
                ORDER BY Student_Id__c
            )
        FROM Account
        WHERE Account_Type__c includes ('Channel')
    ]);
}

You must log in to answer this question.