0

I am trying to clone File(s) from a parent object to child. The code I am using is doing that but the file type is set to "LINK" while doing so, and I am unable to open that file or it has nothing when I open. Below is my code. Please help me out.

Thank you.

set<Id> contentDocIDList = new set<Id>();
    for(ContentDocumentLink conDocLink : [SELECT ContentDocumentId,Id,IsDeleted,LinkedEntityId,ShareType,SystemModstamp,Visibility FROM ContentDocumentLink WHERE LinkedEntityId = :parentobject.id]){
        contentDocIDList.add(conDocLink.ContentDocumentId);
    }
    VersionList = new List<ContentVersion>();
    VersionList = [select Title, Description, FileType,Owner.Name, VersionNumber,CreatedDate,FileExtension from ContentVersion 
                where ContentDocumentId in :contentDocIDList AND IsLatest = true];

     for(ContentVersion objAttach : VersionList)
        {
            for(ContentVersion objIndividualAttachment : [select id, Title, Description, FileType,Owner.Name, VersionNumber,CreatedDate from ContentVersion where Id = :objAttach.Id])
            {
                ContentVersion con= new contentversion();
                con.ContentLocation = 'S'; 
                con.PathOnClient = con.Title; 
                con.ContentUrl = '/'+childobject.id;
                con.Title = objAttach.Title;
                insert con;
                con.VersionData = null;


                Id conDoc = [SELECT ContentDocumentId,Title FROM ContentVersion WHERE Id =:objA1.Id].ContentDocumentId;

                /
                ContentDocumentLink CDL = new ContentDocumentLink();
                CDL .ContentDocumentId = conDoc;
                CDL .LinkedEntityId = newco.Id; 
                CDL .ShareType = 'I'; 
                CDL .Visibility = 'AllUsers'; 

                insert CDL; 


            }
        } 
    }

1 Answer 1

3

You're populating the ContentURL field, which makes the ContentVersion a Link type:

[ContentURL]: URL for links. This is only set for links. One of the fields that determines the FileType

It doesn't copy the content from that URL into the new ContentVersion. You'd have to actually populate the VersionData field for that.

A much more efficient path would be to simply insert new ContentDocumentLink records to share the exiting Files on the parent to the child object as well. Actually copying the files, even if successful, will result in consuming excess capacity against your storage allotment.

As written, your code uses two SOQL queries in nested loops, which will quickly result in you receiving LimitsException when you exceed the transaction limit. You should bulkify this code by collecting related record ids in a Set, and then issue a single, non-looped query against that Set using a clause like WHERE Id IN :mySetOfIds.

3
  • As suggested, I am just trying to insert a new ContentDocumentLink and passing ContentDocumentId from parent object to new content document link. But I get an error "List has more than 1 row for assignment to SObject". Thanks a lot for your input David, made me think in another approach. Oct 16, 2018 at 14:29
  • That's a separate issue you might open a new question for, but I would recommend you search the error text first - you'll find that's a very common, and very easy to cure, issue. I'm glad this approach was helpful.
    – David Reed
    Oct 16, 2018 at 15:03
  • @Daivd , Was able to insert contentdocumentlink by passing the contentdocumentId from parent object. But, still link is being shared instead of the file. I did not even insert any content version (like as mentioned content URL parameter in content version might be setting the file to link). Oct 16, 2018 at 16:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .