51 votes
Accepted

Obtaining all ContentDocument records using SOQL

Just found out today that with the Spring 19' update there is a new setting in the "Setup -> Permission Sets -> (choose an existing or create a new one) -> App Permission -> Content -> Query All Files:...
Denis S.'s user avatar
  • 656
15 votes
Accepted

How to Upload a File to a specific Library Folder using Apex?

As I can see from Your code while creating a ContentDocumentLink you are not specifying a LinkedEntityId. Linked entity ID tells in which folder it exists. So Minimum code needed to store file in a ...
Pranay Jaiswal's user avatar
13 votes
Accepted

Entity 'ContentDocumentLink' is not supported for semi join inner selects

Some objects have an insanely high SOQL cost, so they're simply not allowed. For example, you also can't do semi-join selects on Task or Activity, either because the performance cost is simply too ...
sfdcfox's user avatar
  • 487k
13 votes
Accepted

How to query ContentDocument and Custom object's fields at once?

You can do it in one query if you start from the Custom Object and utilize the ContentDocumentLinks relationship in a subquery. Here's an example with Account as the main object: SELECT Name, ...
David Reed's user avatar
  • 92.6k
12 votes
Accepted

ContentVersion FileExtension

It's determined by the PathOnClient field as populated when you create the ContentVersion. As an example, in one of my orgs I uploaded a file called App.logo.png through the API, populating its file ...
David Reed's user avatar
  • 92.6k
9 votes
Accepted

How to query ContentDocumentLink in SOQL and then upsert records in Data Loader

This is a system restriction on ContentDocumentLink. The object reference has specifics of what you're required to do: You can't run a query without filters against ContentDocumentLink. You can't ...
David Reed's user avatar
  • 92.6k
8 votes

How to create an EmailMessage record with DML having HasAttachment to be 'true'?

I spent close to 4 hours on this and figured it out. The key thing is to populate Attachment.ParentId with EmailMessage Id. Here is the sample code: EmailMessage emailMessage = new EmailMessage(); ...
javanoob's user avatar
  • 8,847
8 votes

Obtaining all ContentDocument records using SOQL

As advised by David Ha, you should add a "USING SCOPE" option in your SOQL query in order to retrieve all the ContentDocument records. Strangely, I found that "USING SCOPE Everything" restricts the ...
Sylvain's user avatar
  • 81
7 votes

Obtaining all ContentDocument records using SOQL

If you want to delete files that are related to a single object then export ContentDocumentIds from below query and then use those ContentDocumentIds to delete files which are related to object. ...
sfdc's user avatar
  • 13.6k
7 votes

How to generate a download URL for a ContentDocument in Lightning Community

The download link on the file previewer in a community generates a link of this format. It should work fine for your scenario with the exception of not being a best practice to hardcode it like this....
Mark Pond's user avatar
  • 22.9k
6 votes

Obtaining all ContentDocument records using SOQL

Official response from SFDC Support is this is working as designed. There is no ability to query all content programmatically via SOQL which I suspected. You have to query ContentDocumentLink with ...
CoryCowgill's user avatar
  • 2,203
6 votes
Accepted

How to prevent duplicate File (ContentDocument) if the same file is uploaded at multiple records?

Well, Apex does have that lovely heap limit of only a few MB so you can't compare the entire content of two large files. But the ContentVersion object does have a Checksum field. So in principle you ...
Charles T's user avatar
  • 11k
5 votes

Obtaining all ContentDocument records using SOQL

A bit late but this worked for me SELECT ContentDocumentId,ContentDocument.Title, ContentDocument.CreatedDate,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM User)
Nicolas Evans's user avatar
5 votes

ContentDocumentLink insertion fails

Using visibility to all users: link.Visibility = 'AllUsers'; You should also set the share type to this: link.ShareType = 'I'; Which means: Inferred permission. The user’s permission is ...
Mo Hamada's user avatar
  • 107
5 votes
Accepted

Unable to delete ContentDocumentLink

As you can see above, when you upload a file to any record like account New Test Account, it will always create 2 links - 1st for the user TestUser with share type I (which cannot be deleted) and 2nd ...
salesforce-sas's user avatar
5 votes
Accepted

LinkedEntityId referring to custom object record id

Yes, you can relate documents to custom objects. Here's the code I used to determine that: for(sobjecttype relatedTo: ContentDocumentLink.LinkedEntityId.getDescribe().getReferenceTo()) { if(...
sfdcfox's user avatar
  • 487k
5 votes
Accepted

Attempting to pass Flow variable to Apex to use in HTTP request & create PDF

First of all, don't copy from Office products. It screws up your code with "Smart Quotes", which don't compile in Apex. Secondly, your InvocableMethod annotation has an error because you ...
sfdcfox's user avatar
  • 487k
4 votes
Accepted

ContentDocumentID is not available just after ContentVersion getting inserted But available after soql query

The only value that gets updated in the Apex instance for any SObject when you insert is the Id value. Other fields populated as part of the insert by triggers or other logic have to be re-queried to ...
Keith C's user avatar
  • 136k
4 votes

Obtaining all ContentDocument records using SOQL

We had a similar issue with Report and it turned out the reports were under the Private space of the owners. In our case, this query gives us the full data: Select id,name,ownerid from report USING ...
David Ha's user avatar
  • 141
4 votes
Accepted

LinkedEntityid is the user incorrectly coming in ContentDocumentLink trigger

Trigger for contentdocumentLink fired twice one for User currently uploading the document and one for the record against which document being uploaded. At the second time when the trigger is fired I ...
Prakash Chandra's user avatar
4 votes

SOQL ContentDocument: Ids not returning

I think I found a simpler solution: add the Apex user to the contentdocument.. Here is the code: //First, get all the CDLs for each object ContentDocumentLink[] cdls = [SELECT ...
ZAR's user avatar
  • 2,033
4 votes
Accepted

ContentDocumentLink filter not working- Trailhead Conference Management App

The reason you are getting this error is because you are getting null value in recordId parameter (As mentioned by you in your comment). The solution for your problem is to make sure that you pass a ...
Vijay Ganji's user avatar
  • 6,586
4 votes
Accepted

How to generate an external downloadlink for files

you can try to use this approach: public static String documentDownloadUrl() { ContentDocumentLink cdl = //You get in your own way return Url.getSalesforceBaseUrl().toExternalForm() ...
Oleh Berehovskyi's user avatar
4 votes
Accepted

ContentDocumentLink fires trigger multiple times on list insertion

Let's consider a simple ContentDocumentLink trigger and helper inspector class trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert, before update, after insert, after update) { ...
cropredy's user avatar
  • 70.8k
4 votes
Accepted

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY : [NetworkId]

I have a similar issue and this workaround helped me: Id networkId = [SELECT Id FROM Network LIMIT 1].Id; System.runAs(portalUser) { ContentVersion contentVersion = new ...
OlegY's user avatar
  • 56
4 votes
Accepted

ContentDocumentLink query returns different results based on LinkedEntityId size

Worked with support. It required them to enable an internal permission to overcome the issue which seems to be a limitation of 2,000 records to filter the LinkedEntityId by before you start seeing ...
Kris Goncalves's user avatar
4 votes

Attach File (ContentDocumentLink) to Email

The key method you want to use is the Messaging.setEntityAttachments() method. This method accepts: Array of IDs of Document, ContentVersion, or Attachment items to attach to the email. See also: ...
Nick C's user avatar
  • 6,553
4 votes

LinkedEntityId referring to custom object record id

The documentation does mention (though a little off-handedly) that custom objects can be set for LinkedEntityId, emphasis mine. Description ID of the linked object. Can include Chatter users, groups, ...
Derek F's user avatar
  • 60.9k
4 votes
Accepted

How can I prevent a ContentDocument being deleted when it is linked to a record meeting certain criteria?

It should be as simple as: AggregateResult[] results = [ SELECT COUNT(Id) recordCount, ContentDocumentId documentId FROM ContentDocumentLink WHERE ContentDocumentId IN :contentDocumentIds ...
sfdcfox's user avatar
  • 487k
3 votes
Accepted

Scheduled Data Export: how to obtain ContentVersion linking data

One thing to realize is that ContentDocument can't be directly inserted; it is inserted automatically when the first ContentVersion is inserted. ContentDocument acts as a parent for each version of ...
cropredy's user avatar
  • 70.8k

Only top scored, non community-wiki answers of a minimum length are eligible