function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kumar Saurav 32Kumar Saurav 32 

Batch Class on ContentDocumentLink

I have tried to create a Batch Class on the ContentDocumentLink. The querylocator in the start method is returning some records but it is not able to execute the execute() method. I am getting Internal Salesforce.com Error. May I know is there any limitations to this object?

Here is the code snippet I am using:
 
global class CommunityActivityJob implements Database.Batchable<sObject>,Database.AllowsCallouts ,Database.Stateful {
    global Database.QueryLocator start(Database.BatchableContext BC) {
    List<String> linked = new List<String>();
    linked.add('00528000000M2xj');
       String query = 'Select Id, ContentDocumentId, LinkedEntityId, ShareType, Visibility from ContentDocumentLink Where LinkedEntityId In :linked ';
        System.debug('===START==='+Database.query(query));
        return Database.getQueryLocator(query);
    }
     global void execute(Database.BatchableContext BC, List<sObject> scope) {
        System.Debug('===INSIDE EXECUTE==='+scope);
        }
    global void finish(Database.BatchableContext BC) {
        System.debug('===Inside Finish===');
    }

 
Ajay Ghuge 6Ajay Ghuge 6
Try adding a limit clause for example Limit 1 or Limit 5 so that we will get some hint.
Kumar Saurav 32Kumar Saurav 32
I have already tried that.
Ajay Ghuge 6Ajay Ghuge 6
Check this : 
https://salesforce.stackexchange.com/questions/76121/retrieve-all-contentdocument-ids-whose-related-contentdocumentlink-record-is-upd

It is mentioned as: You can't run a query without filters against ContentDocumentLink.
 
Kumar Saurav 32Kumar Saurav 32
I have already applied the filter on LinkedEntityId. The querylocator is returning records.
GauravGargGauravGarg

Hi Saurav, 

Please post the error message. 

Thanks,

Gaurav
Skype: gaurav62990

Kumar Saurav 32Kumar Saurav 32
Here is the debug log for execute method: 02:15:35.0 (368461)|EXECUTION_STARTED 02:15:35.0 (372197)|CODE_UNIT_STARTED|[EXTERNAL]|01p2800000FdNp2|CommunityActivityJob 02:15:35.0 (3586564)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 02:15:35.0 (25452249)|FATAL_ERROR|Internal Salesforce.com Error 02:15:35.25 (25736049)|CUMULATIVE_LIMIT_USAGE 02:15:35.25 (25736049)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 200 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 60000 Maximum heap size: 0 out of 12000000 Number of callouts: 0 out of 0 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 0 Number of queueable jobs added to the queue: 0 out of 1 Number of Mobile Apex push calls: 0 out of 10 02:15:35.25 (25736049)|CUMULATIVE_LIMIT_USAGE_END 02:15:35.0 (25817145)|CODE_UNIT_FINISHED|CommunityActivityJob 02:15:35.0 (26892816)|EXECUTION_FINISHED
GauravGargGauravGarg
Can you try removing extra space in query 
String query = 'Select Id, ContentDocumentId, LinkedEntityId, ShareType, Visibility from ContentDocumentLink Where LinkedEntityId In :linked '

and try again. 

Thanks,
Gaurav
Kumar Saurav 32Kumar Saurav 32
The query is working in the query editor and workbench both. Even the debug logs show a record. You can have a look to the following logs: 02:15:35.0 (19310292)|SOQL_EXECUTE_BEGIN|[6]|Aggregations:0|Select Id, ContentDocumentId, LinkedEntityId, ShareType, Visibility from ContentDocumentLink Where LinkedEntityId In :linked 02:15:35.0 (33928861)|SOQL_EXECUTE_END|[6]|Rows:1 02:15:35.0 (33958888)|HEAP_ALLOCATE|[6]|Bytes:8 02:15:35.0 (33981234)|HEAP_ALLOCATE|[6]|Bytes:140 02:15:35.0 (34051767)|HEAP_ALLOCATE|[6]|Bytes:8 02:15:35.0 (34145496)|HEAP_ALLOCATE|[6]|Bytes:152 02:15:35.0 (34165424)|HEAP_ALLOCATE|[6]|Bytes:163 02:15:35.0 (34187494)|USER_DEBUG|[6]|DEBUG|===START===(ContentDocumentLink:{Id=06A2800000BmhrXEAR, ContentDocumentId=06928000007w7vwAAA, LinkedEntityId=00528000000M2xjAAC, ShareType=I, Visibility=AllUsers}) 02:15:35.0 (34205676)|STATEMENT_EXECUTE|[7] 02:15:35.0 (36817352)|SOQL_EXECUTE_BEGIN|[7]|Aggregations:0|Select Id, ContentDocumentId, LinkedEntityId, ShareType, Visibility from ContentDocumentLink 02:15:35.0 (61985818)|SOQL_EXECUTE_END|[7]|Rows:1 02:15:35.0 (62038900)|HEAP_ALLOCATE|[7]|Bytes:20 02:15:35.78 (78378721)|CUMULATIVE_LIMIT_USAGE 02:15:35.78 (78378721)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 200 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 60000 Maximum heap size: 0 out of 12000000 Number of callouts: 0 out of 0 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 0 Number of queueable jobs added to the queue: 0 out of 1 Number of Mobile Apex push calls: 0 out of 10 02:15:35.78 (78378721)|CUMULATIVE_LIMIT_USAGE_END 02:15:35.0 (78418985)|CODE_UNIT_FINISHED|CommunityActivityJob 02:15:35.0 (79187035)|EXECUTION_FINISHED
GauravGargGauravGarg
Can you an error in query?
 
Select Id, ContentDocumentId, LinkedEntityId, ShareType, Visibility from ContentDocumentLink Where LinkedEntityId In :linked

@run-time this should be changed to "temp" not "linked"

This means, the variable is not providing value to salesforce.

Add the debug state before Database.query() and find the actual query build. 

Thanks,

Gaurav

Kumar Saurav 32Kumar Saurav 32
Here is the debug logs for the complete start method.
 
02:15:35.0 (414954)|EXECUTION_STARTED
02:15:35.0 (419436)|CODE_UNIT_STARTED|[EXTERNAL]|01p2800000FdNp2|CommunityActivityJob
02:15:35.0 (2810195)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
02:15:35.0 (12066316)|HEAP_ALLOCATE|[72]|Bytes:3
02:15:35.0 (12105673)|HEAP_ALLOCATE|[77]|Bytes:152
02:15:35.0 (12120355)|HEAP_ALLOCATE|[342]|Bytes:408
02:15:35.0 (12134564)|HEAP_ALLOCATE|[355]|Bytes:408
02:15:35.0 (12147080)|HEAP_ALLOCATE|[467]|Bytes:48
02:15:35.0 (12175018)|HEAP_ALLOCATE|[139]|Bytes:6
02:15:35.0 (12216189)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:11
02:15:35.0 (12230091)|SYSTEM_METHOD_ENTRY|[15]|BatchableContextImpl.BatchableContextImpl()
02:15:35.0 (12237798)|STATEMENT_EXECUTE|[15]
02:15:35.0 (12244922)|SYSTEM_METHOD_EXIT|[15]|BatchableContextImpl
02:15:35.0 (12252011)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12
02:15:35.0 (12256413)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
02:15:35.0 (12261945)|VARIABLE_SCOPE_BEGIN|[31]|this|Database.BatchableContextImpl|true|false
02:15:35.0 (12303020)|VARIABLE_ASSIGNMENT|[31]|this|{}|0x618b195b
02:15:35.0 (12309890)|VARIABLE_SCOPE_BEGIN|[31]|jobId|Id|false|false
02:15:35.0 (12393460)|VARIABLE_ASSIGNMENT|[31]|jobId|"70728000061NGQsAAO"
02:15:35.0 (12400642)|VARIABLE_SCOPE_BEGIN|[31]|childJobId|Id|false|false
02:15:35.0 (12408357)|VARIABLE_ASSIGNMENT|[31]|childJobId|null
02:15:35.0 (13188699)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:96
02:15:35.0 (13198835)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:6
02:15:35.0 (13203005)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:7
02:15:35.0 (13211473)|METHOD_ENTRY|[1]|01p2800000FdNp2|CommunityActivityJob.CommunityActivityJob()
02:15:35.0 (13220102)|STATEMENT_EXECUTE|[1]
02:15:35.0 (13224069)|STATEMENT_EXECUTE|[1]
02:15:35.0 (13227678)|METHOD_EXIT|[1]|CommunityActivityJob
02:15:35.0 (13232923)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
02:15:35.0 (13236198)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12
02:15:35.0 (13242052)|VARIABLE_SCOPE_BEGIN|[2]|this|CommunityActivityJob|true|false
02:15:35.0 (13258914)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x5cf5f91a
02:15:35.0 (13265256)|VARIABLE_SCOPE_BEGIN|[2]|BC|Database.BatchableContext|true|false
02:15:35.0 (13336270)|VARIABLE_ASSIGNMENT|[2]|BC|{"jobId":"70728000061NGQsAAO"}|0x618b195b
02:15:35.0 (13348006)|STATEMENT_EXECUTE|[2]
02:15:35.0 (13349448)|STATEMENT_EXECUTE|[3]
02:15:35.0 (13358805)|HEAP_ALLOCATE|[3]|Bytes:4
02:15:35.0 (13396771)|VARIABLE_SCOPE_BEGIN|[3]|linked|List<String>|true|false
02:15:35.0 (13426028)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
02:15:35.0 (13439043)|VARIABLE_ASSIGNMENT|[3]|linked|[]|0x650d721e
02:15:35.0 (13445269)|STATEMENT_EXECUTE|[4]
02:15:35.0 (13448282)|HEAP_ALLOCATE|[4]|Bytes:15
02:15:35.0 (13485226)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
02:15:35.0 (13493274)|STATEMENT_EXECUTE|[5]
02:15:35.0 (13495760)|HEAP_ALLOCATE|[5]|Bytes:125
02:15:35.0 (13500520)|VARIABLE_SCOPE_BEGIN|[5]|query|String|false|false
02:15:35.0 (13511127)|VARIABLE_ASSIGNMENT|[5]|query|"Select Id, ContentDo (105 more) ..."
02:15:35.0 (13515509)|STATEMENT_EXECUTE|[6]
02:15:35.0 (13517194)|HEAP_ALLOCATE|[6]|Bytes:11
02:15:35.0 (13534334)|HEAP_ALLOCATE|[50]|Bytes:5
02:15:35.0 (13551449)|HEAP_ALLOCATE|[56]|Bytes:5
02:15:35.0 (13560024)|HEAP_ALLOCATE|[64]|Bytes:7
02:15:35.0 (19310292)|SOQL_EXECUTE_BEGIN|[6]|Aggregations:0|Select Id, ContentDocumentId, LinkedEntityId, ShareType, Visibility from ContentDocumentLink Where LinkedEntityId In :linked 
02:15:35.0 (33928861)|SOQL_EXECUTE_END|[6]|Rows:1
02:15:35.0 (33958888)|HEAP_ALLOCATE|[6]|Bytes:8
02:15:35.0 (33981234)|HEAP_ALLOCATE|[6]|Bytes:140
02:15:35.0 (34051767)|HEAP_ALLOCATE|[6]|Bytes:8
02:15:35.0 (34145496)|HEAP_ALLOCATE|[6]|Bytes:152
02:15:35.0 (34165424)|HEAP_ALLOCATE|[6]|Bytes:163
02:15:35.0 (34187494)|USER_DEBUG|[6]|DEBUG|===START===(ContentDocumentLink:{Id=06A2800000BmhrXEAR, ContentDocumentId=06928000007w7vwAAA, LinkedEntityId=00528000000M2xjAAC, ShareType=I, Visibility=AllUsers})
02:15:35.0 (34205676)|STATEMENT_EXECUTE|[7]
02:15:35.0 (36817352)|SOQL_EXECUTE_BEGIN|[7]|Aggregations:0|Select Id, ContentDocumentId, LinkedEntityId, ShareType, Visibility from ContentDocumentLink 
02:15:35.0 (61985818)|SOQL_EXECUTE_END|[7]|Rows:1
02:15:35.0 (62038900)|HEAP_ALLOCATE|[7]|Bytes:20
02:15:35.78 (78378721)|CUMULATIVE_LIMIT_USAGE
02:15:35.78 (78378721)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 200
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 60000
  Maximum heap size: 0 out of 12000000
  Number of callouts: 0 out of 0
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 0
  Number of queueable jobs added to the queue: 0 out of 1
  Number of Mobile Apex push calls: 0 out of 10

02:15:35.78 (78378721)|CUMULATIVE_LIMIT_USAGE_END

02:15:35.0 (78418985)|CODE_UNIT_FINISHED|CommunityActivityJob
02:15:35.0 (79187035)|EXECUTION_FINISHED
GauravGargGauravGarg
This debug do not have any error statement. 
Kumar Saurav 32Kumar Saurav 32
Yes, the debug for the start or finish method does not show any error. But the execute method shows the error which I have posted earlier in this thread.