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';
objCntVersion.FirstPublishLocationId = attIterator.ParentId;
objCntVersion.OwnerId = attIterator.OwnerId;
objCntVersion.CreatedById = attIterator.OwnerId;
objCntVersion.CreatedDate = attIterator.CreatedDate;
objCntVersion.FirstPublishLocationId = attIterator.OwnerId;
lstCntVersionsToInsert.add(objCntVersion);
}
if(!lstCntVersionsToInsert.isEmpty()) {
insert lstCntVersionsToInsert;
}