yumenghui
2023-08-04 0a3f9325599467015aecc4af7543dd10fe70ec56
force-app/main/default/classes/OPDPlanHandler.cls
@@ -556,6 +556,7 @@
                }
            }
            if (Trigger.isInsert && String.isNotBlank(t.OriginalOpdPlan__c)) {
                
                reapplyopdMap.put(t.OriginalOpdPlan__c, t.Id);
@@ -666,25 +667,73 @@
                    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> getdocLink = [SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId In:reapplyopdMap.keySet()];
            List<Id> idList = new List<Id>();
            for (ContentDocumentLink link : getdocLink) {
                idList.add(link.ContentDocumentId);
            }
            List<Id> linkIdList = new List<Id>();
            for (ContentDocumentLink link : getdocLink) {
                linkIdList.add(link.LinkedEntityId);
            }
            List<ContentVersion> cvList = [SELECT VersionData,Title,PathOnClient,ContentDocumentId FROM ContentVersion WHERE ContentDocumentId In: idList];
            List<ContentVersion> insertContentVersion = new List<ContentVersion>();
            if (cvList.size()>0) {
                for (ContentVersion cv : cvList) {
                    ContentVersion newCV = new ContentVersion();
                    newCV.VersionData = cv.VersionData;
                    newCV.Title = cv.Title;
                    newCV.PathOnClient = cv.PathOnClient;
                    insertContentVersion.add(newCV);
                }
                if (insertContentVersion.size() > 0) {
                    insert insertContentVersion;
                    List<Id> versionIdList = new List<Id>();
                    for (ContentVersion ver : insertContentVersion) {
                        versionIdList.add(ver.Id);
                    }
                if (insertAttachmentCloneList.size() > 0) {
                    insert insertAttachmentCloneList;
                    List<ContentDocumentLink> insertContentDocumentLink = new List<ContentDocumentLink>();
                    List<ContentDocumentLink> getdocLinkList = [SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId In: idList
                                                                    AND LinkedEntityId In:linkIdList];
                    if (getdocLinkList.size()>0) {
                        for (ContentVersion ver : insertContentVersion) {
                            Id docId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :ver.Id].ContentDocumentId;
                            for (ContentDocumentLink cdl  : getdocLinkList) {
                                ContentDocumentLink newCDL = new ContentDocumentLink();
                                newCDL.ContentDocumentId = docId;
                                // newCDL.LinkedEntityId = cdl.LinkedEntityId;
                                newCDL.LinkedEntityId = reapplyopdMap.get(cdl.LinkedEntityId);
                                newCDL.ShareType = 'I';
                                newCDL.Visibility = 'AllUsers';
                                insertContentDocumentLink.add(newCDL);
                            }
                        }
                        if (insertContentDocumentLink.size()>0) {
                            insert insertContentDocumentLink;
                        }
                    }
                }
            }
            // 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() {