I have a batch class, in that I'm doing a dynamic relationship query my code is as follows

global class RBS_DeleteScheduleBillRunBatch implements Database.Batchable<sObject>{
global String objInvoice;
global String objCreditMemo;
global String objAssociatedAcc;
global String query;
global String ScheduleBillRun;

global RBS_DeleteScheduleBillRunBatch(String inv, String cm, String assAcc, String ScheduleBillRunId){
    objInvoice = inv; 
    objCreditMemo = cm;
    objAssociatedAcc = assAcc;
    ScheduleBillRun = ScheduleBillRunId;}

global Database.QueryLocator start(Database.BatchableContext BC){
      query = 'SELECT Id ';
      if(objInvoice != null){
           query += ',(SELECT Id,Name FROM Invoices__r)';
      if(objCreditMemo != null){
           query += ',(SELECT Id,Name FROM CreditMemos__r)';
      if(objAssociatedAcc != null){
           query += ',(SELECT Id,Name FROM Associated_Accounts__r)';
      query += ' FROM ScheduledBillRun__c WHERE Id =:ScheduleBillRun';
      System.debug('query >>'+query);
 return Database.getQueryLocator(query);   }   

global void execute(Database.BatchableContext BC, List<ScheduledBillRun__c> scope){
    //delete scope;


   global void finish(Database.BatchableContext BC){


In debug I'm getting the Id of the parent record only, child record has more than 50k records, I'm not able to understand the problem, the query is properly built but in debug, records are not shown

  • 2
    Have you tried iterating through the child lists and printing out each item? – akiradev Mar 11 '20 at 10:19

You need to explicitly get the list of related objects via getSObjects() method

for (ScheduledBillRun__c sbr : scope) {

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.