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);        
  • Have you executed this query in developer console? – Reshma Dec 2 '16 at 9:09
  • 1
    are you getting any exception. Check the apex job page for details. – Tushar Sharma Dec 2 '16 at 9:18
  • Are you running the batch with the same User who runs the SOQL Query ? – Akram G Dec 2 '16 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 Dec 2 '16 at 9:57
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')
    ]);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy