File Management Patterns

Overview

Salesforce provides multiple mechanisms for file storage and management: Attachments, Files (ContentVersion), ContentDocument, and Salesforce Files. Each mechanism has different use cases, limitations, and best practices.

Core Principle: Choose the file storage mechanism that best matches your requirements for sharing, versioning, storage limits, and access patterns. Different mechanisms serve different needs.

Prerequisites

Required Knowledge:

Recommended Reading:

When to Use Each File Mechanism

Use Attachments When

Limitations:

Use Files (ContentVersion) When

Benefits:

File Storage Mechanisms

Mechanism 1: Attachments (Legacy)

Object: Attachment

Use Cases:

Limitations:

Migration: Migrate from Attachments to Files (ContentVersion) for modern file management.

Mechanism 2: Files (ContentVersion)

Object: ContentVersion (file data), ContentDocument (file metadata), ContentDocumentLink (sharing)

Use Cases:

Benefits:

Implementation:

// Create file
ContentVersion cv = new ContentVersion(
    Title = 'Document.pdf',
    PathOnClient = 'Document.pdf',
    VersionData = Blob.valueOf('File content'),
    FirstPublishLocationId = recordId
);
insert cv;

// Link file to record
ContentDocumentLink cdl = new ContentDocumentLink(
    LinkedEntityId = recordId,
    ContentDocumentId = cv.ContentDocumentId,
    ShareType = 'V'
);
insert cdl;

Mechanism 3: Salesforce Files

Purpose: Native Salesforce file storage with rich features.

Features:

Use Cases:

File Sharing Patterns

Pattern 1: Record-Based Sharing

Purpose: Share files with users who have access to the related record.

Implementation:

Best Practices:

Pattern 2: User-Based Sharing

Purpose: Share files directly with specific users.

Implementation:

Best Practices:

Pattern 3: Library-Based Sharing

Purpose: Share files via content libraries.

Implementation:

Best Practices:

File Management Best Practices

File Organization

File Versioning

File Storage Optimization

File Security

Migration Patterns

Pattern 1: Attachments to Files Migration

Purpose: Migrate from Attachments to Files (ContentVersion).

Implementation:

Best Practices:

Pattern 2: External File Migration

Purpose: Migrate files from external systems to Salesforce.

Implementation:

Best Practices:

Q&A

Q: What are the different file storage mechanisms in Salesforce?

A: Salesforce provides: (1) Attachments (legacy, 25MB limit, deprecated), (2) Files (ContentVersion) (modern, up to 2GB, versioning, rich sharing), (3) Salesforce Files (native file storage with libraries). Use Files (ContentVersion) for modern file management, migrate from Attachments.

Q: When should I use Attachments vs Files?

A: Use Files (ContentVersion) for: (1) Modern file storage (recommended), (2) Large files (up to 2GB), (3) Versioning (file versioning), (4) Complex sharing (rich sharing options). Use Attachments only for: (1) Legacy compatibility (existing systems), (2) Simple use cases (basic attachments). Migrate from Attachments to Files.

Q: How do I share files in Salesforce?

A: Share files by: (1) Record-based sharing (link files to records, inherit record sharing), (2) User-based sharing (share directly with users via ContentDocumentLink), (3) Library-based sharing (share via content libraries). Use ContentDocumentLink to control file sharing.

Q: How do I migrate from Attachments to Files?

A: Migrate by: (1) Export Attachments (export Attachment records), (2) Create ContentVersion (create ContentVersion from Attachment), (3) Link to records (link ContentVersion to original records), (4) Verify migration (verify files migrated correctly), (5) Archive Attachments (archive original Attachments). Migration enables modern file management.

Q: What are file storage limits in Salesforce?

A: Storage limits: (1) Attachments (25MB per file), (2) Files (2GB per file with Large File Upload), (3) Org storage (varies by edition, can purchase additional). Monitor storage usage and optimize file storage.

Q: How do I organize files in Salesforce?

A: Organize by: (1) Content libraries (organize files in libraries), (2) Naming conventions (consistent file naming), (3) Tagging (tag files for searchability), (4) Folder structure (logical organization), (5) Versioning (use ContentVersion for versioning). Good organization improves file management.

Q: How do I optimize file storage?

A: Optimize by: (1) Monitor storage (track storage usage), (2) Compress files (compress when possible), (3) Archive old files (archive unused files), (4) Clean up (remove unused files), (5) Use appropriate mechanism (choose right mechanism for use case). Storage optimization reduces costs and improves performance.