Notes And Attachment

Attachment to File Conversion in Salesforce Using Apex

APEX

Salesforce introduced Files related list in Winter’16, and now Salesforce is going to replace Notes and Attachment section with Files. Basically, Files that you upload to the Notes & Attachments related list on records in Salesforce Classic are now Salesforce Files objects, rather than the old attachment objects.

If you want to modify System audit Fields like CreatedDate, CreatedById, etc fields while converting to file. you must have the below permission “Set Audit Fields upon Record Creation” and “Update Records with Inactive Owners” 

How to Enable?

Activating this feature in User Interface allows you to grant permission to set audit fields and update records with inactive owners, but it does not grant the permission by default.
You will need to grant the following profile permissions to your Users(Who are converting to file)

Set Audit Fields upon Record Creation – Allow the User to set audit fields (like ‘Created By’ or ‘Last Modified By’) when you create a record via API importing tools like Data Loader.
Update Records with Inactive Owners – Allow the User to update record owner and sharing-based records with inactive owners.

Add a permission set

1. Navigate to Setup.
2. Quick Find ‘Permission Set’.
3. Click New.
4. Enter the name of the permission set.
5. Under System Permissions, grant any of the 2 permissions:
    i. Set Audit Fields upon Record Creation
    ii. Update Records with Inactive Owners
6. Click Save.
7. Click Manage Assignments.
8. Search for the name of the Users that will need the permission.
9. Click Add Assignment.

After you have permission now you are ready to convert the file.

Before creating Files in Salesforce we need to understand the Object relationship. Here is an example to create File from Attachment using apex.

ContentDocument: This object record you don’t need to create. It gets created when you create ContentVersion which is the child of ContentDocument.

ContentVersion: This object stores document information similar like Attachment.

ContentDocumentLink: This object will share the files with Users, Records, Groups etc. You can create multiple records to attach the same files under multiple records. Salesforce link

//Get attachment
Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId From Attachment LIMIT 1];
 
//Insert ContentVersion
ContentVersion cVersion = new ContentVersion();
cVersion.ContentLocation = 'S'; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
cVersion.PathOnClient = attach.Name;//File name with extention
cVersion.Origin = 'H';//C-Content Origin. H-Chatter Origin.
cVersion.OwnerId = attach.OwnerId;//Owner of the file
cVersion.Title = attach.Name;//Name of the file
cVersion.VersionData = attach.Body;//File content
Insert cVersion;
 
//After saved the Content Verison, get the ContentDocumentId
Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId;
 
//Insert ContentDocumentLink
ContentDocumentLink cDocLink = new ContentDocumentLink();
cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId
cDocLink.LinkedEntityId = attach.ParentId;//Add attachment parentId
cDocLink.ShareType = 'I';//V - Viewer permission. C - Collaborator permission. I - Inferred permission.
cDocLink.Visibility = 'InternalUsers';//AllUsers, InternalUsers, SharedUsers
Insert cDocLink;

uniquesymbol

Leave a Reply