Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

Thursday, February 6, 2020

How to download the files using URL from salesforce

Note: To download the file, require the Content Version record Ids

For Single File

Use the below URL to download the single file from the salesforce.
URL https://<YOUR_SFDC_BASE_URL>/sfc/servlet.shepherd/version/download/068XXXXXXXXXXXX

String idCntDoc = '068XXXXXXXXXXXX';
String strURL = URL.getSalesforceBaseUrl().toExternalForm() + '/sfc/servlet.shepherd/version/download/' + idCntDoc;
System.debug('URL ===> '+strURL);

For Multiple Files

URL: https://<YOUR_SFDC_BASE_URL>/sfc/servlet.shepherd/version/download/068XXXXXXXXXXXX/068XXXXXXXXXXXX
above URL downloads the files in ZIP folder

list<String> lstCntVersionIds = new list<String>{'068XXXXXXXXXXXX', '068XXXXXXXXXXXX'};

String strURL = URL.getSalesforceBaseUrl().toExternalForm() + '/sfc/servlet.shepherd/version/download/';

for(String iterator : lstCntVersionIds) {
    strURL += iterator + '/';
}

strURL = strURL.removeEnd('/');

System.debug('URL ===> '+strURL);

Saturday, November 9, 2019

Convert Attachment to File in Salesforce Using Apex

Note:
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?

Path:
From Setup, enter User Interface in the Quick Find box, and then select "Set Audit Fields upon Record Creation" and "Update Records with Inactive Owners" 


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.

Sample Code
list<ContentVersion> lstCntVersionsToInsert = new list<ContentVersion>();
list<Attachment> lstAttachments = [SELECT Id, ParentId, Name, IsPrivate, Body, OwnerId, CreatedById, CreatedDate, Description FROM Attachment where ParentId != null];
for(Attachment attIterator : lstAttachments) {
 ContentVersion objCntVersion = new ContentVersion();
 objCntVersion.Title = attIterator.Name;
 objCntVersion.PathOnClient = '/' + attIterator.Name;
 objCntVersion.VersionData = (attIterator.Body == null ? Blob.valueOf('.') : attIterator.Body);
 objCntVersion.Description = attIterator.Description;
 objCntVersion.SharingPrivacy = 'N'; // Can be always public.
 objCntVersion.FirstPublishLocationId = attIterator.ParentId; // Parent Id
 objCntVersion.OwnerId = attIterator.OwnerId;
 // To avoid "Documents in a user's private library must always be owned by that user" error.
 objCntVersion.CreatedById = attIterator.OwnerId; 
 objCntVersion.CreatedDate = attIterator.CreatedDate;
 objCntVersion.FirstPublishLocationId = attIterator.OwnerId;
 lstCntVersionsToInsert.add(objCntVersion);
}

if(!lstCntVersionsToInsert.isEmpty()) {
    insert lstCntVersionsToInsert;
}