+ Start a Discussion
MGimelliMGimelli 

Unable to delete ContentDocument via Scheduled Job

Hi - 

We have a schedueld job that is designed to remove old documents that no longer need to be in our system. It appears around 6/20 (Summer 16?) permissions were changed and we were not able to query all documents via the system administrator profile. I have fixed this issue by adding "USING SCOPE Team" to our query.

I am now receving an "INSUFFICIENT_ACCESS_OR_READONLY" on the Content Document object when trying to run the delete through a class ran by a schedueld job. I have tested a one off delete of the object via the Developer console and am able to successfully delete the job thorugh there. I have also checked my class to ensure the job is running under my profile, which it is. 

It seems strange to me I can run a detele through the developer console but am unable too when running the schedueld job on the same object.

Code Snippit: 

//scope in this case is passed in as an SObject of ContentDocument
global void execute(Database.BatchableContext BC,List<SObject> scope) {
 if( dataCleanup.Delete__c )
            delete scope;
}
Best Answer chosen by MGimelli
Vishal_GuptaVishal_Gupta
Hi MGimelli,

In this case please ensure that your class version is latest one because console code always execute with latest version, Currently 37.0 is the latest version, please try with it.

Thanks,
Vishal

All Answers

Vishal_GuptaVishal_Gupta
Hi MGimelli

Could you share complete code of your batch, it will be more helpful to debug the issue.

Thanks,
Vishal
MGimelliMGimelli
Sure, thank you in advance for your help - 

//Scheduled class:
global class DataCleanupScheduable implements Scheduable {
    //Get list of objects to delete/archive from custom setting table
    List<Data_Cleanup__c> dataCleanupList = [SELECT Name, ObjectAPIName__c, Condition__c FROM Data_Cleanup__c];
    for (Data_Cleanup__c dc : dataCleanupList) {
        DataCleanupBatch dataCleanupQuery = new DataCleanupBatch(dc);
        Id BatchProcessId = Database.ExecuteBatch(dataCleanupQuery,100);
    }
}

//DataCleanupBatch: 
global class DataCleanupBatch implements Database.Batchable<sObject> {
     global Data_Cleanup__c dataCleanup;
     global DataCleanupBatch(Data_Cleanup__c customSetting) {
             //Some logic here to build query string based on dataCleanupList custom setting
             dataCleanup = customSetting;
     }

     global void execute(Database.BatchableContext BC,List<SObject> scope) {
            if( dataCleanup.Delete__c )
            delete scope;
      }
}


 
Vishal_GuptaVishal_Gupta
Hi MGimelli,

Still I can't see the Start method in Batch, I want to see the query which you are using in query locator, I am assuming you are using ObjectAPIName__c from Data_Cleanup__c object to delete the documents and might be recently a new object added in your org on which you don't have sufficient permissions. Its my assumption but if you could share complete code of your batch, I will try to dig into it more.

Thanks,
Vishal
MGimelliMGimelli
Hi Vishal - 

I am yes, here is the query generated, and I can confirm I get records returned from this query, I'd prefer not to share the entire class: 

SELECT (fields)
FROM ContentDocument USING SCOPE Team
WHERE PublishStatus = 'R' AND ( Title LIKE 'Cost Factor Letter : %' OR Title LIKE 'Sell Factor Letter :%' OR Title LIKE 'Sales Summary Report :%')

The object I am having issues with is not new, however. It is the ContentDocument object. I have the System Administrator profile, so I do not see why I cannot delete the object. As I said before, if I run a script in the developer console to delete a ContentDocument record, I am able to delete it successfully. 
Vishal_GuptaVishal_Gupta
Hi MGimelli,

In this case please ensure that your class version is latest one because console code always execute with latest version, Currently 37.0 is the latest version, please try with it.

Thanks,
Vishal
This was selected as the best answer
MGimelliMGimelli
That was it, thank you Vishal for your help.

I have updated our classes to the latest version (37) and am no longer receiving the error.