function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12 

Copy Content document into an Attachment

Hi - I'm trying to copy a file in a Content library into an attachment of a Contract record as I can't give the Account Owner access to a group folder in Content. 
The document in Content is encypted.  I've used the attached code to do the migration. If I have an unecrypted file it works fine. The encrypted files are now corrupted when they are copied into attachments. If I manually download from content and upload the encrypted files into attachments they are fine.

Any ideas on what may be changing the file structure that corrupts the encrypted files? I assume that the transfer would copy a byte for byte copy of the document?

Thanks
Public Pagereference Migrate() {

List<contentVersion> ContentDocs = [select id,VersionData,GNS_Partner__c,Implementation_Ref__c,MFR_Ref__c,Migrated__c,Title,description,pathonclient,ownerid,contentsize from contentversion where islatest=true and Migrated__c=false and contentsize <5000001 and GNS_partner__c=:system.currentPageReference().getparameters().get('id')];

List<attachment> NewAtt = new list<attachment> ();

If(ContentDocs.size()>0){
String Descrpt;


    For(ContentVersion c: ContentDocs){

            
    if(c.description !=null){Descrpt = c.description;}
      else {descrpt = 'Executed Contract = Voltage Encrypted';}
      String VerData = encodingUtil.base64Encode(c.versiondata);
              NewAtt.add(new Attachment(ParentId=c.Implementation_Ref__c,body=EncodingUtil.base64Decode(VerData),name=c.title,description=c.description, ownerid=c.ownerid,contenttype='application/pdf'));
    c.Migrated__c=true;
    
    }


// Update content as migrated and insert new Attachments to IMP

update ContentDocs;
insert NewAtt;
return null;
  }
  
return null;


Vinit_KumarVinit_Kumar
Do not encrypt and decrypt body for Attachment Body ,try something like below :-

Attachment(ParentId=c.Implementation_Ref__c,body=c.VersionData,name=c.title,description=c.description, ownerid=c.ownerid,contenttype='application/pdf')

Hope this helps !!
neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12
Sorry - I should have clarified that I started with that one. Doesn't work I'm afraid. 
SFDummySFDummy
Hello, I am trying to do the same thing, copy ContentVersion to Attachment. Can you please help, none of the above code works.