+ Start a Discussion

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?

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> ();

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'));

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

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

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 !!
Sorry - I should have clarified that I started with that one. Doesn't work I'm afraid. 
Hello, I am trying to do the same thing, copy ContentVersion to Attachment. Can you please help, none of the above code works.