19626
2023-07-17 254ea442ff0e0a7a97e9f481e9f127a782cba840
force-app/main/default/classes/OPDPlanHandler.cls
@@ -666,25 +666,75 @@
                    insert insertCloneList;
                }
            }
            List<Attachment> getAttachmentList = [SELECT Id, Body, Name, ParentId
                                        FROM Attachment
                                        WHERE ParentId In:reapplyopdMap.keySet()];
            List<Attachment> insertAttachmentCloneList = new List<Attachment>();
            if (getAttachmentList.size() > 0 ) {
                for(Attachment att :getAttachmentList){
                    Attachment attClone = new Attachment();
                    attClone = att.clone();
                    attClone.Id = null;
                    attClone.Body = att.Body;
                    attClone.Name = att.Name;
                    attClone.ParentId = reapplyopdMap.get(att.ParentId);
                    insertAttachmentCloneList.add(attClone);
            List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId,LinkedEntityId
                                                   FROM ContentDocumentLink
                                                   WHERE LinkedEntityId In:reapplyopdMap.keySet()];
            List<ID> fileIDs = new List<ID>();
            for (ContentDocumentLink docLink : cdlList) {
                fileIDs.add(docLink.ContentDocumentId);
            }
            List<ContentDocument> attachmentinfo=[SELECT Id,Title,OwnerId from ContentDocument WHERE id IN :fileIDs];
            List<Id> idList = new List<Id>();
            for (ContentDocument attachment : attachmentinfo) {
                idList.add(attachment.Id);
            }
            List<ContentVersion> versionList = [select Id,VersionData,Title,ContentLocation,PathOnClient from ContentVersion where ContentDocumentId in: idList];
            List<ContentVersion> newVersionList = new List<ContentVersion>();
            for (ContentVersion version : versionList) {
                ContentVersion newVersion = new ContentVersion();
                newVersion = version.clone();
                newVersion.Id = null;
                newVersion.VersionData = version.VersionData;
                newVersion.Title = version.Title;
                newVersion.ContentLocation = version.ContentLocation;
                newVersion.PathOnClient = version.PathOnClient;
                newVersionList.add(newVersion);
                }
                if (insertAttachmentCloneList.size() > 0) {
                    insert insertAttachmentCloneList;
            if (newVersionList.size() > 0) {
                insert newVersionList;
                }
            idList = new List<Id>();
            for (ContentVersion newVersion : newVersionList) {
                idList.add(newVersion.Id);
            }
            newVersionList = [SELECT ContentDocumentId FROM ContentVersion WHERE Id IN:idList];
            List<ContentDocumentLink> linkList = new List<ContentDocumentLink>();
            Integer i = 0;
            for (ContentVersion newVersion : newVersionList) {
                ContentDocumentLink link = new ContentDocumentLink();
                link.ContentDocumentId = newVersion.ContentDocumentId;
                link.LinkedEntityId = reapplyopdMap.get(cdlList[i].LinkedEntityId);
                link.ShareType = 'I';
                link.Visibility = 'AllUsers';
                linkList.add(link);
                i++;
            }
            if (linkList.size() > 0) {
                insert linkList;
            }
            // List<Attachment> getAttachmentList = [SELECT Id, Body, Name, ParentId
            //                             FROM Attachment
            //                             WHERE ParentId In:reapplyopdMap.keySet()];
            // List<Attachment> insertAttachmentCloneList = new List<Attachment>();
            // if (getAttachmentList.size() > 0 ) {
            //     for(Attachment att :getAttachmentList){
            //         Attachment attClone = new Attachment();
            //         attClone = att.clone();
            //         attClone.Id = null;
            //         attClone.Body = att.Body;
            //         attClone.Name = att.Name;
            //         attClone.ParentId = reapplyopdMap.get(att.ParentId);
            //         insertAttachmentCloneList.add(attClone);
            //     }
            //     if (insertAttachmentCloneList.size() > 0) {
            //         insert insertAttachmentCloneList;
            //     }
            // }
    }
    private void updateOppPlan() {