Skip to content

FilesRecipes

pozil edited this page Oct 11, 2021 · 16 revisions

layout: default

FilesRecipes class

Demonstrates how to create, link and share Files


Enums

GenericFileType

This enum encapsulates a 'generic' filetype a 'filetype' that may have multiple file extension and mime types associated with it. For instance, IMAGE encapsulates: jpg, gif, jpeg, & png this allows developers to say, 'give me all image attachments' without worrying about the actual file extension.


Methods

createFileAttachedToRecord(Blob fileContents,Id attachedTo,String fileName)Database.SaveResult

Creates a file and links it to a given record

Parameters

Param Description
fileContents the binary blob of the files contents
attachedTo the record to link this file to, initially
fileName the name of the file. Note that the system determines

Return

Type

Database.SaveResult

Description

Database.SaveResult

Example

Blob fileContents = Blob.valueOf('Hello World 2');
Account acct = [SELECT id FROM Account LIMIT 1];
 FilesRecipes.createFileAttachedToRecord(
     fileContents,
     firstLocation,
     'AwesomeFile1'
 );
System.debug('Look for files assoicated with account: ' + acct.id);

createFileFromStringAttachedToRecord(String text,Id firstLocation)void

creates a file attachment containing the given string and links it to the object specified in firstLocation

Parameters

Param Description
text String to write to the file
firstLocation object to immediately link this file to

Example

Account acct = [SELECT id FROM Account LIMIT 1];
FilesRecipes.createFileFromStringAttachedToRecord('Hello World', acct.Id);
System.debug('Look for files assoicated with account: ' + acct.id);

getFilteredAttachmentsForRecord(FilesRecipes.GenericFileType genericFileType,Id recordId)List<ContentVersion>

Searches for content version records linked to this record Filtering by a generic file type: image, audio, document etc. Note: This method has a false-positive PMD warning. Our Query includes the keyword 'WITH SECURITY_ENFORCED' which prevents this Query from accessing fields and objects that they don't have permission to access. This is a form of inline CRUD/FLS Check.

Parameters

Param Description
genericFileType Enum of image, audio, document
recordId Record id to limit searching to

Return

Type

List<ContentVersion>

Description

List&lt;ContentVersion&gt;

Example

Account acct = [SELECT id FROM Account LIMIT 1];
FilesRecipes.createFileFromStringAttachedToRecord('Hello World', acct.Id);
System.debug('Found the following ContentVersion Ids: ' + FilesRecipes.getFilteredAttachmentsForRecord(FilesRecipes.GenericFileType.ALL, acct.id));

publishContent(ContentDocumentLink cdl)Database.SaveResult

Given a content document link, publish the content version

Parameters

Param Description
cdl Content Document link record to publish

Return

Type

Database.SaveResult

Description

Database.SaveResult

Example

Account acct = [SELECT id FROM Account LIMIT 1];
FilesRecipes.createFileFromStringAttachedToRecord('Hello World', acct.Id);
ContentDocumentLink cdl = [SELECT LinkedEntityId, ContentDocument.LatestPublishedVersionId FROM ContentDocumentLink WHERE LinkedEntityId = :acct.id LIMIT 1];
System.debug('Found the following ContentVersion Ids: ' + FilesRecipes.getFilteredAttachmentsForRecord(FilesRecipes.GenericFileType.ALL, acct.id));

Inner Classes

FilesRecipes.FilesRecipesException class

Internal exception class


Clone this wiki locally