5

I'm trying to query Files (ContentVersion & ContentDocument) but unable to read any data.

At first I thought it was that running user does not have access to Files, but then I found that when I run Functions locally, then File information is read without issues, but when I deploy the function and invoke it using Apex I get 0 records.

I'm working with a Scratch Org, and have enabled Production Space and Test Space both for Functions.

The dataApi() query returns 0 records in deployed function but I'm getting 1 record when I run function locally - what could be the issue here? I've tried invoking function in scratch org using Developer Console as well as an LWC.

My Function (.java):

public class MyFunction implements SalesforceFunction<FunctionInput, FunctionOutput> {
  private static final Logger LOGGER = LoggerFactory.getLogger(MyFunction.class);
  
  @Override
  public FunctionOutput apply(InvocationEvent<FunctionInput> event, Context context) throws Exception {
    
    List<Record> records =
    context.getOrg().get().getDataApi().query("SELECT Id, Title FROM ContentVersion WHERE ID='0689D0000011tqWQAQ'").getRecords();//hard-coded ID for poc-development purposes only
    
    LOGGER.info("Function successfully queried {} file records!", records.size());
    
    List<ContentVersion> files = new ArrayList<>();
    for (Record record : records) {
       String id = record.getStringField("Id").get();
       String title = record.getStringField("Title").get();
       
       files.add(new ContentVersion(id, title));
    }
    
    return new FunctionOutput(files);
  }
}
6
  • You'll need to assign the proper permissions to the Functions permission set. Jan 6, 2022 at 17:48
  • Content is not available as an object to give permissions to in Permission Set.
    – VarunC
    Jan 7, 2022 at 17:49
  • Hi @VarunC, did you have to/were you able to extract the blob: ContentVersion.VersionData through Functions? The documentation/examples appear to be quite barren on this topic. I tried Base4 encoding and decoding of String contentVersionData = record.getStringField("VersionData").get(); but get a path back: /services/data/v53.0/sobjects/ContentVersion/ How can I get the actual image as a byte[]? Feb 1, 2022 at 18:47
  • 1
    Nopes. Though I did got a confirmation from Salesforce Team that there is an internal bug for this since Cloud Integration User running the Function does not have Files access at all. So locally your org user is running fine and able to access files but when running the function in cloud via Sandbox or Scratch org it does not have access to files. It will be fixed but no ETA.
    – VarunC
    Feb 2, 2022 at 12:55
  • Is it just a sandbox issue? That is, do you know if things will work in Production? Feb 2, 2022 at 18:41

2 Answers 2

1

This appears to be a Salesforce issue. We got around it by creating a duplicate classic Attachment object (automatically created via after-insert trigger on ContentVersion object) and working off of it instead of ContentVersion. Ugly, but works until Salesforce addresses this issue.

There is a 25MB file size limit for Attachments though, which Salesforce Support can increase to 65MB, HOWEVER, the maximum file size limit to create Attachment programmatically is 36MB only. The 65MB max limit works ONLY when an attachment is uploaded manually via the UI.

0

A possible way to overcome this is to create CDLs for the Integration User so then, on runtime, that user has the right access to the content files. This way, if the payload sent from apex indicates the content version ids to filter the query (for example) - it is certain the integration user can reach those and manipulate them.

See the article that was published: https://help.salesforce.com/s/articleView?language=en_US&id=000365095&type=1

You must log in to answer this question.

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