1

I have a batch where I first query some ContentDocumentLink. If the query has some results, I got an internal Salesforce.com Error when the batch start during the SerialBatchApexRangeChunkHandler operation. Here is the code :

global class BATCH_ContentDocumentLink implements Database.Batchable < sObject > {

    String query;

    global BATCH_ContentDocumentLink() {
        query = 'SELECT ContentDocumentId FROM ContentDocumentLink ' +
            'WHERE LinkedEntityId IN (SELECT Id FROM User)';

    }


    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List < sObject > scope) {
        System.debug(scope);
    }

    global void finish(Database.BatchableContext BC) {

    }
}

What is the issue ?

3
  • can you provide exact error message ? what line gives it ? Oct 9, 2018 at 10:40
  • The exact message is "Internal Salesforce.com Error", I don't have more details about it. What I forgot to mention is that the error occurs during the "SerialBatchApexRangeChunkHandler" operation. I don't have any line specified. I have add this last point to the description
    – N. Germeau
    Oct 9, 2018 at 11:20
  • ContentDocumentLink has some odd restrictions on queries. This query works in developer console, but I wonder if those restrictions are being applied differently or have side effects in batch context.
    – David Reed
    Oct 9, 2018 at 11:30

1 Answer 1

4

I am not sure why it does not work with Database.QueryLocator but it works with custom iterables. It might be related to query restrictions that David Reed mentioned in comments.

(I copied iterator from this link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_iterable.htm)

Iterator:

global class CustomIterable 
   implements Iterator<ContentDocumentLink>{ 

   List<ContentDocumentLink> cdls {get; set;} 
   Integer i {get; set;} 

   public CustomIterable(){ 
       cdls =  [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM User)]; 
       i = 0; 
   }   

   global boolean hasNext(){ 
       if(i >= cdls.size()) {
           return false; 
       } else {
           return true; 
       }
   }    

   global ContentDocumentLink next(){ 
       // 8 is an arbitrary 
       // constant in this example
       // that represents the 
       // maximum size of the list.
       if(i == 8){return null;} 
       i++; 
       return cdls[i-1]; 
   } 
}

Iterable:

global class CustomIterableExample implements iterable<ContentDocumentLink>{
   global Iterator<ContentDocumentLink> Iterator(){
      return new CustomIterable();
   }
}

Batch:

global class BATCH_ContentDocumentLink implements Database.Batchable < ContentDocumentLink > {

    String query;

    global BATCH_ContentDocumentLink() {
    }

    global Iterable<ContentDocumentLink> start(Database.batchableContext info){ 
       return new CustomIterableExample(); 
   }   

    global void execute(Database.BatchableContext BC, List < Object > scope) {
        System.debug(scope);
    }

    global void finish(Database.BatchableContext BC) {

    }
}
1

You must log in to answer this question.

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