We have a requirement where when we close the work order, we have to set custom field value "Allow Archieving" to true on it's content versions documents. For this, I have already written a Logic. Now, we have existing Closed Work Orders where I have to perform Data Update activity. But I found Data update acitivity bit complex when the record count is high because I can fire below 2 queries easily but in the 3rd query if I get Millions of the documents, due to characters limitation in SOQL, I have to break the Content Document Id lists into multiple parts and create multiple csv file which is very complicated.

Query 1 : To Fetch Work Order

SELECT Id,Status,CGT_Work_Completed_Date__c,CreatedDate,LastModifiedDate FROM WorkOrder where Status='Closed'

Query 2 : To Fetch Content Document of Work order

Select Id,ContentDocumentId, LinkedentityId FROM contentDocumentLink where Linkedentityid=:workOrderId

Query 3 : To Fetch Content Version of Content Document Id

SELECT Id,ContentDocumentId,Allow_Archiving__c,FileType,IsLatest FROM ContentVersion where IsLatest=true AND ContentDocumentId=:contentDocId

I tried to use nested query but it seems ContentDocument link is not supported. I have also prepared anonymous test scripts for this but I am getting CPU time limit or other governor limit errors. Is there any other feasible way to achieve this?

1 Answer 1


If you have millions of records, you should consider using a dataloader or writing a an Apex batch. Also, you do have LatestPublishedVersionId field on ContentDocument object, so you can use it instead of your third query. An example query for dataloader export would be:

SELECT ContentDocument.LatestPublishedVersionId FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM WorkOrder WHERE Status = 'Closed')

This will return you a csv with ContentVersion ids, to which you can add a new column and set true in every cell. Then, do an update on ContentVersion object with this csv mapping ContentDocument.LatestPublishedVersionId to Id and the new column to Allow_Archiving__c.

If this does not work or you prefer to write a batch for this one time job, just do the same: add ContentDocument.LatestPublishedVersionId to your second query and then initialize "new" ContentVersions:

List<ContentVersion> cvToUpdate = new List<ContentVersion>();
for (ContentDocumentLink cdl : cdls) {
    cvToupdate.add(new ContentVersion(
        Id = cdl.ContentDocument.LatestPublishedVersionId, 
        Allow_Archiving__c = true)
update cvToUpdate;

You must log in to answer this question.

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