Here is my attachment loop to convert into Salesforce Files. The document as Files is created along with the title and content type however it has no content or sometimes '1' or 'MQ==' which is not expected. Any help would be appreciated.
public static void afterInsert(List<Attachment> attachinserted){
Set<ID> agreementset = New Set<ID>();
List<ID> attachedIDs = New List<ID>();
for(Attachment attach : attachinserted){
if(attach.ParentId.getSobjectType() == Apttus__APTS_Agreement__c.SobjectType){
agreementset.add(attach.ParentId);
attachedIDS.add(attach.Id);
}
}
//Holds agreements with new Attachments
List<Apttus__APTS_Agreement__c> agreementAUS = [Select Id FROM Apttus__APTS_Agreement__c
WHERE CNG_AUS_RecordType__c = TRUE AND ID IN:agreementset];
//Holds attachments from correct Recordtypes
List<Attachment> attachtoupdate = [SELECT Id, ParentId, Name, IsPrivate, Body, OwnerId, CreatedById, CreatedDate,ContentType, Description FROM Attachment
WHERE ParentId IN:agreementAUS AND ID IN:attachedIDs];
//Conversion of Attachments into ContentVersions
//map Attachment Id with ContentVersion record
Map<Id,ContentVersion> attachmentCVs = new Map<Id,ContentVersion>();
List<Attachment> attdelete = new List<Attachment>();
//Map —> Attachment – Attachment Parent Id
Map<Id,Id> attachmentParentIds = new Map<Id,Id>();
List<ContentVersion> contentversionupdate=new List<ContentVersion>();
//Generating file version using content version object
//Set<Id> contentDocumentIdSet = new Set<Id>();
if(!attachtoupdate.isEmpty()){
for(Attachment att : attachtoupdate) {
ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.PathOnClient ='/'+att.Name +'.doc';
cv.Origin = 'H';
cv.OwnerId = att.OwnerId;
cv.Title = att.Name;
cv.VersionData = att.body;
// cv.VersionData = Blob.valueOf(EncodingUtil.base64Encode(att.body));
/* String fileContent = att.body!=Null ?
string.valueOf(att.Body).replaceAll('<','<')
.replaceAll('>','>')
.replaceAll('&','&')
.replaceAll('"','"')
.replaceAll('[',''')
.replaceAll('\'', '''):'.';*/
cv.FirstPublishLocationId = att.ParentId;
attachmentCVs.put(att.Id,cv);
attachmentParentIds.put(att.Id,att.ParentId);
attdelete.add(att);
//contentversionupdate.add(cv);
}
}
//Insert the content versions from attachment data
if(attachmentCVs.values().size() > 0 ) {
insert attachmentCVs.values();
}
}
```