0

I need help to convert legacy attachments to files with lastmodifieddate of file is equal to created date of attachments by using apex code. I need to do it for all objects on which attachments are present. below is the code that i wrote.(I created a parentid__c custom field on content version in order to relate them with there parent)

     set<id> setOfContentVersion = new set<id>();
    list<contentdocumentlink> listOfContentDoclink = new list<contentdocumentlink>();
    list<contentversion> contentVersionlist = new list<contentversion>();
    for(attachment atch:[select Id, Name,ownerid,body, ParentId, Parent.Type ,parent.name from attachment]){           
        contentversion cv = new contentversion();
        cv.PathOnClient = atch.Name; 
        cv.OwnerId = atch.OwnerId; 
        cv.Title = atch.Name; 
        cv.VersionData = atch.Body; 
        cv.parentid__c = atch.ParentId;
        setOfContentVersion.add(atch.ParentId);
        contentVersionlist.add(cv);
    }
    insert contentVersionlist;
    for(contentversion cv :[select ContentDocumentId ,parentid__c from ContentVersion where parentid__c IN : setOfContentVersion and ID IN :contentVersionlist]){
        ContentDocumentLink cl = new ContentDocumentLink(LinkedEntityId = cv.parentid__c, ContentDocumentId = cv.ContentDocumentId, ShareType = 'I'); 
        listOfContentDoclink.add(cl);
    }
    insert listOfContentDoclink;

Is there any more better solution for this.?

I know setting "Files uploaded to the Attachments related list on records are uploaded as Salesforce Files, not as attachments" has been introduced by salesforce to ensure that Attachments added via Notes & Attachments related list are now uploaded as Files moving forward. But i need to do it for legacy attachments. Any help Appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.