본문 바로가기

개발자정보

How To Convert Attachments To Files In Salesforce ?

반응형

Scenario

I have uploaded an attachment (salesfiorce_migration_guide.pdf) to an opportunity record from classic salesforce. The uploaded document record has the following actions where ‘view’ action is a link to download the file.

Uploaded attachments to an opportunity record

Next, uploading another file (router-sample.pdf) to the same opportunity record using upload file functionality in lightning. The uploaded files in the above and current scenario are shown below.

 

Uploaded Files & attachment links

You may have noticed that both the files are in PDF format but the recently uploaded file has the pdf icon which we can preview the content without downloading. The other file which is uploaded as an attachment from classic salesforce is treated as a link here.

Therefore, How We Can Convert The Attachments From Classic View To Files In Salesforce?

Solution

It’s possible to change the attachments to files by enabling a couple of checkboxes in GeneralSettings.

Why Files?

You can attach a file to the notes & attachments related list of record. The users who have access to the record can see the attached documents. The only way to share the record documents with others is by changing the org’s record sharing rules or posting documents in the chatter feed. Salesforce was trying to solve this in a better way.

Finally, In winter 16 release, Salesforce announced a new related list called files. The benefits of files are

  • Version Control
  • Preview & Options
  • Share with multiple users and records
  • Optimised for lightning
  • Post to chatter 
  • Supports large size

 

The attachments are uploaded in the past cannot be converted as files by enabling the above settings. Run the script given below (script should run by the record owner otherwise it will throw errors). 

 

  //Fetching all available attachments in the org
  List<Attachment> attachments = [Select Body, Id, Name, OwnerId,ParentId From Attachment];
  //System.debug('Attachments'+attachments);
  //Details of content version fields are given in the below link
  //https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm
   
  //to map Attachment Id with ContentVersion record
  Map<Id,ContentVersion> attachmentCVs = new Map<Id,ContentVersion>();
  //Map —> Attachement – Attachment Parent Id
  Map<Id,Id> attachementParentIds = new Map<Id,Id>();
   
  //Generating file version using content version object
  for(Attachment att : attachments) {
  ContentVersion cv = new ContentVersion();
  //S – Salesforce , check details on above link
  cv.ContentLocation = 'S';
  cv.PathOnClient = att.Name;
  //H – Chatter , check details on above link
  cv.Origin = 'H';
  cv.OwnerId = att.OwnerId;
  cv.Title = att.Name;
  cv.VersionData = att.Body;
   
  attachmentCVs.put(att.Id,cv);
  attachementParentIds.put(att.Id,att.ParentId);
  }
  //System.debug(attachmentCVs);
   
  //Insert the content versions from attachment data
  if(attachmentCVs.values().size() > 0 ) {
  insert attachmentCVs.values();
  }
   
  //to map ContentVersionId with AttachmentParent Id for ContentDocument Linking
  map<Id,Id> cvToAttchmtParentIdMap = new map<Id,Id>();
   
  List<Id> cvIds = new List<Id>();
  for(Id key : attachmentCVs.keySet()){
  ContentVersion cv = attachmentCVs.get(key);
  Id attchmentParentId = attachementParentIds.get(key);
  //System.debug('cvToAttchmtParentIdMap'+cvToAttchmtParentIdMap);
  cvToAttchmtParentIdMap.put(cv.Id,attchmentParentId);
  cvIds.add(cv.Id);
  //System.debug('cvIds'+cvIds);
  }
  //Fetching contentDocumentId using contentVersionId
  List<ContentVersion> cvWithDocIds=[select Id,ContentDocumentId from ContentVersion where Id IN :cvIds];
  //System.debug('cvDocIds'+cvWithDocIds);
   
  //to link the files (versions) to its parent records like opportunity,case etc.
  List<ContentDocumentLink> contentDocumentLinks = new List<ContentDocumentLink>();
  for (ContentVersion cv : cvWithDocIds) {
  Id attachmentParentId = cvToAttchmtParentIdMap.get(cv.Id);
  ContentDocumentLink cl = new ContentDocumentLink(LinkedEntityId = attachmentParentId, ContentDocumentId = cv.ContentDocumentId, ShareType = 'I');
  ContentDocumentLinks.add(cl);
  }
   
  if(contentDocumentLinks.size() > 0){
  insert contentDocumentLinks;
  }
   
  //System.debug('contentDocumentLinks'+contentDocumentLinks);
  //Delete all exisiting attachments once it is converted into Files if needed.
  if(attachments.size() > 0) {
  delete attachments;
  }
   
   
   

 

References

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm

https://help.salesforce.com/articleView?id=collab_files_overview.htm&type=5

반응형