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
golugolu 

transferring files from one object to another

Hi ,
I want to transfer files from one object to another. Custom object A with record id ( 1,2,3,4) maps to custom object b with record id ( 5,6,7,8 ) respectively. I want to transfer the files of record 1 to record 5 , 2 to record 6, 3 to record 7 and so on. Can anyone please pass me the code snippet to do the same?
Raj VakatiRaj Vakati
You can do it by using apex trigger 
like below 


 
Opportunity opp; //original Opportunity record
Opportunity oppCopy; //copy of opp record with different id

for (ContentVersion img : [SELECT Id, Checksum, ContentDocumentId, ContentLocation, ContentModifiedById, ContentModifiedDate, ContentSize,  FileExtension, FileType, FirstPublishLocationId, IsAssetEnabled, IsLatest, IsMajorVersion, NegativeRatingCount, Origin, OwnerId, PathOnClient, PositiveRatingCount, PublishStatus, RatingCount, ReasonForChange, SharingOption, Title, VersionData, VersionNumber 
                           FROM ContentVersion 
                           WHERE FirstPublishLocationId =: opp.Id])
{
    insert new ContentVersion(Title = img.Title, PathOnClient = img.PathOnClient, 
                              VersionData = img.VersionData, 
                              FirstPublishLocationId = oppCopy.Id);
}
 
ContentVersion  cont =[SELECT Checksum,ContentDocumentId,ContentLocation,ContentSize,ContentUrl,Description,FileExtension,FileType,FirstPublishLocationId,Id,IsAssetEnabled,IsDeleted,Origin,OwnerId,PathOnClient,PublishStatus,RatingCount,ReasonForChange,SharingOption,Title,VersionData,VersionNumber FROM ContentVersion WHERE ContentDocumentId = '06990000003Cq5V'];

 ContentVersion newcont = new ContentVersion();
 newcont.Title  = cont.Title;
 newcont.PathOnClient  = cont.PathOnClient;
 newcont.VersionData = cont.VersionData;
 newcont.FirstPublishLocationId  = cont.FirstPublishLocationId;

insert newcont;