trigger ContentDocumentTrigger on ContentDocument (before delete,before update) { System.debug('enter ContentDocumentTrigger'); // 20231103 陈京武 Lightning文件修改 Start if(StaticParameter.ContentDocumentTrigger == false){ return; } if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){ return; } // LHJ CHAN-BCLD4P 20190618 Start List specIds = new List(); // LHJ CHAN-BCLD4P 20190618 End System.debug('enter ContentDocumentTrigger'); List rentaldelIds = new List(); List attdelIds = new List(); // LHJ CHAN-BCLD4P 20190618 Start List specDelIds = new List(); // LHJ CHAN-BCLD4P 20190618 End List accIds = new List(); //20221014 yc 医院审批优化 start List accAppIds = new List(); //20221014 yc 医院审批优化 end List OpporFileIds = new List(); Map OpporFileUndeleteMap = new Map(); List DeleteReIds = new List(); Map OppordeleteReUserMap = new Map(); Map OppordeleteReTimeMap = new Map(); List oFOrderIds = new List(); Map oFOrderIdMap = new Map(); // sx 20230210start 备品借出申请故障报告附件提取 List rentaldeleteApplyFaultIds = new List(); Map> deleteImages = new Map>(); // sx 20230210end 备品借出申请故障报告附件提取 //deloitte-zhj 发票附件删除 20230901 start String invoiceStart = System.Label.invoiceStart; List rentaldelIdsZhj = new List(); List attdelIdsZhj = new List(); Set docidList = new Set(); Set delDocidList = new Set(); if(Trigger.isDelete && Trigger.isBefore){ for (ContentDocument doc : Trigger.old) { docidList.add(doc.Id); delDocidList.add(doc.Id); } } if(Trigger.isUpdate && Trigger.isBefore){ for (ContentDocument doc : Trigger.old) { docidList.add(doc.Id); } } //deloitte-zhj 20231227 改善空指针 start // ContentVersion ver = new ContentVersion(); // if(docidList.size() > 0){ // ver = [select FileType,TextPreview from ContentVersion where ContentDocumentId in: docidList limit 1]; // System.debug('TextPreview=>' + ver.TextPreview); // System.debug('FileType=>' + ver.TextPreview); // if(ver.FileType == 'SNOTE'){ // System.debug('可恶的备注!!!'); // return; // } // } List ver = new List(); if(docidList.size() > 0){ ver = [select FileType,TextPreview from ContentVersion where ContentDocumentId in: docidList limit 1]; if(ver.size() > 0){ System.debug('TextPreview=>' + ver[0].TextPreview); System.debug('FileType=>' + ver[0].TextPreview); if(ver[0].FileType == 'SNOTE'){ System.debug('可恶的备注!!!'); return; } } } //deloitte-zhj 20231227 改善空指针 end if (Trigger.isBefore && Trigger.isUpdate) { List idList = new List(); for(ContentDocument doc: Trigger.old){ idList.add(doc.Id); } List linkList = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList]; for (ContentDocumentLink link : linkList) { if (String.valueOf(link.LinkedEntityId).startsWith('a5C') || String.valueOf(link.LinkedEntityId).startsWith('a5A')) { accAppIds.add(link.LinkedEntityId); } if (String.valueOf(link.LinkedEntityId).startsWith('001')) { accIds.add(link.LinkedEntityId); } } } System.debug('=======>' + JSON.serialize(Trigger.old) + ' : ' + JSON.serialize(Trigger.new)); if (Trigger.isBefore && Trigger.isDelete) { List idList = new List(); for(ContentDocument doc: Trigger.old){ idList.add(doc.Id); } List linkList = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList]; System.debug('Link的条数:' + linkList.size()); for (ContentDocumentLink link : linkList) { if (String.valueOf(link.LinkedEntityId).startsWith('001')) { accIds.add(link.LinkedEntityId); } // LHJ CHAN-BCLD4P 20190618 Start if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.OppSpecialApply)){ specIds.add(link.LinkedEntityId); } // LHJ CHAN-BCLD4P 20190618 End //20221014 yc 医院审批优化 start if (String.valueOf(link.LinkedEntityId).startsWith('a5C') || String.valueOf(link.LinkedEntityId).startsWith('a5A') || String.valueOf(link.LinkedEntityId.getsobjecttype())=='Account_Delay_Apply__c') { accAppIds.add(link.LinkedEntityId); } //20221014 yc 医院审批优化 end } for (ContentDocument cd : Trigger.old) { System.debug('cd.Id = ' + cd.Id); List cdlList = [SELECT id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId = :cd.Id]; System.debug('cdlList = ' + cdlList); System.debug('cd.Id = ' + cd.Id); for (ContentDocumentLink cdl : cdlList) { if (String.valueOf(cdl.LinkedEntityId).startsWith(invoiceStart)) { System.debug('String.valueOf(cdl.LinkedEntityId).startsWith(invoiceStart)'); rentaldelIdsZhj.add(cdl.LinkedEntityId); attdelIdsZhj.add(cd.Id); } } } } if (rentaldelIdsZhj.size() > 0) { System.debug('rentaldelIdsZhj = ' + rentaldelIdsZhj); System.debug('attdelIdsZhj = ' + attdelIdsZhj); List rentaldelList = new List(); for (Consumable_accessories_invoice__c ra : [ SELECT Id FROM Consumable_accessories_invoice__c WHERE Invoice_code__c IN :rentaldelIdsZhj AND Attachment_ID__c IN :attdelIdsZhj ]) { rentaldelList.add(ra); } System.debug('rentaldelList = ' + rentaldelList); ControllerUtil.delConsumableAccessoriesInvoiceSet(rentaldelList); } //deloitte-zhj 发票附件删除 20230901 end //CHAN-BCNCRB ---XHL----End----- if (Trigger.isBefore && Trigger.isDelete) { // List attDeleteList = new List(); List conDeleteList = new List(); List idList = new List(); for (ContentDocument doc : Trigger.old) { idList.add(doc.Id); } List linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList]; List version = [select Id from ContentVersion where ContentDocumentId in: idList]; System.debug('Version的条数:' + version.size()); Id cid; ContentDocumentLink link; for(ContentDocumentLink lin: linkList){ System.debug('...' + lin.LinkedEntityId.getSObjectType().getDescribe().getName()); if(lin.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){ cid = lin.LinkedEntityId; link = lin; } } if(cid == null){ return; } for (ContentDocument doc : Trigger.old) { //if (att.Name.toUpperCase() == '合同书.PDF' && String.valueOf(att.ParentId).startsWith('a0t')) { // 添付した契約書のファイル名は指定できない if (String.valueOf(cid).startsWith('a2K')) { rentaldelIds.add(cid); attdelIds.add(version[0].Id); } if(String.valueOf(cid).startsWith(System.Label.Oppor_sepKey)){ OpporFileIds.add(cid); } // LHJ CHAN-BCLD4P 20190618 Start if(String.valueOf(cid).startsWith(System.Label.OppSpecialApply)){ specDelIds.add(cid); } // LHJ CHAN-BCLD4P 20190618 End if(String.valueOf(cid).startsWith('a3Z')){ oFOrderIds.add(cid); oFOrderIdMap.put(cid,false); } //2023-2-10 sx备品 备品检测分析报告 start String parentLabel = cid.getSObjectType().getDescribe().getName(); System.debug('...' + cid.getSObjectType().getDescribe().getName()); if(parentLabel.equals('Rental_Apply_Fault__c')){ rentaldeleteApplyFaultIds.add(cid); conDeleteList.add(link); if(deleteImages.containskey(version[0].Id)){ deleteImages.get(cid).add(version[0].Id); }else{ List temp = new List(); temp.add(version[0].Id); deleteImages.put(cid,temp); } } //2023-2-10 sx备品 备品检测分析报告 end } //2023-2-10 sx备品 备品检测分析报告 start List rafList = [SELECT Id, Name FROM Rental_Apply_Fault__c WHERE Id IN: rentaldeleteApplyFaultIds AND (status__c = '已发送' OR status__c = '已反馈')]; List docList = new List(); for (Rental_Apply_Fault__c raf : rafList) { for (ContentDocumentLink link1 : conDeleteList) { if (link1.LinkedEntityId == raf.Id) { docList.add(link1.ContentDocumentId); } } } for(ContentDocument doc: Trigger.old){ if(docList.contains(doc.Id)){ doc.addError('非草案中的检测分析报告不允许删除已上传的故障图片'); } } //2023-2-10 sx备品 备品检测分析报告 end } System.debug('ContentDocumentTrigger specIds.size() = ' + specIds.size()); if (specIds.size() > 0) { Map attMap = new Map(); // List attList = [select Id, ParentId from Attachment where ParentId = :specIds order by ParentId]; List linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocument.FileType != 'SNOTE' and LinkedEntityId =: specIds order by LinkedEntityId]; for (ContentDocumentLink link : linkList) { // attMap.put(link.LinkedEntityId, link.LinkedEntityId); if(!delDocidList.contains(link.ContentDocumentId)){ attMap.put(link.LinkedEntityId, link.LinkedEntityId); } } List updSpecList = new List(); for (String specId : specIds) { OpportunitySpecialApply__c spec = new OpportunitySpecialApply__c(Id = specId); spec.Is_upload_file__c = attMap.containsKey(specId); updSpecList.add(spec); } System.debug('ContentDocumentTrigger updSpecList = ' + updSpecList); if (updSpecList.size() > 0) update updSpecList; } //20221207 lt 医院审批优化 start //2023-2-10 sx备品 备品检测分析报告 start if(rentaldeleteApplyFaultIds.size() > 0){ List renList = [SELECT Id,AttachmentId1__c,AttachmentId2__c from Rental_Apply_Fault__c where Id in :rentaldeleteApplyFaultIds]; system.debug('renList====='+renList); for(Rental_Apply_Fault__c ren : renList){ if(deleteImages.containskey(ren.Id)){ System.debug('att1' + ren.AttachmentId1__c); System.debug('att2' + ren.AttachmentId2__c); System.debug('ver' + deleteImages.get(ren.Id)); if(deleteImages.get(ren.Id).contains(ren.AttachmentId1__c)){ ren.AttachmentId1__c = ''; } if(deleteImages.get(ren.Id).contains(ren.AttachmentId2__c)){ ren.AttachmentId2__c = ''; } // if(!(deleteImages.get(ren.Id).contains(ren.AttachmentId2__c)) && !(deleteImages.get(ren.Id).contains(ren.AttachmentId1__c))){ // ren.AttachmentId2__c = ''; // ren.AttachmentId1__c = ''; // } } } system.debug('renList====='+renList); UPDATE renList; } //2023-2-10 sx备品 备品检测分析报告 end if (rentaldelIds.size() > 0) { List rentaldelList = new List(); for (Consumable_accessories_invoice__c ra : [select Id from Consumable_accessories_invoice__c where Invoice_code__c in :rentaldelIds and Attachment_ID__c in :attdelIds]) { rentaldelList.add(ra); } ControllerUtil.delConsumableAccessoriesInvoiceSet(rentaldelList); } if(OpporFileIds.size()>0){ List OpporList = [select id,Last_upload_time__c, Is_Locked__c, Last_upload_user__c, Stock_apply_time__c from Opportunity_File__c where id in:OpporFileIds ]; if(OpporList.size()>0){ for(Opportunity_File__c ofc : OpporList){ if(!ofc.Is_Locked__c){ ofc.Last_upload_user__c = UserInfo.getUserId(); ofc.Last_upload_time__c = System.now(); System.debug(ofc+'23333'); update ofc; }else{ OpporFileUndeleteMap.put(ofc.id,ofc.id); System.debug(ofc+'23333444444'); } } } } System.debug(Trigger.isDelete+'Trigger.isDelete'); System.debug(Trigger.isBefore+'Trigger.isBefore'); if(Trigger.isDelete && Trigger.isBefore){ List idList = new List(); for(ContentDocument doc : Trigger.old){ idList.add(doc.Id); } List link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList]; Id enId; for(ContentDocumentLink lin: link){ if(lin.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){ enId = lin.LinkedEntityId; } } for(ContentDocument doc : Trigger.old){ if(OpporFileUndeleteMap.containsKey(enId)){ doc.addError('备货已审批,不可删除附件'); return; }else{ DeleteReIds.add(enId); } } } // if(Trigger.isDelete && Trigger.isAfter){ // List idList = new List(); // for(ContentDocument doc : Trigger.old){ // idList.add(doc.Id); // } // List linkList = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList]; // ContentDocumentLink link; // for(ContentDocumentLink lin: linkList){ // if(lin.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){ // link = lin; // } // } // for(ContentDocument doc : Trigger.old){ // // List link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: doc.Id]; // if(OpporFileUndeleteMap.containsKey(link.LinkedEntityId)){ // doc.addError('备货已审批,不可删除附件'); // return; // }else{ // DeleteReIds.add(link.LinkedEntityId); // } // } // } //20221014 yc 医院审批优化 start if (accAppIds.size() > 0) { Map attMap = new Map(); // List attList = [select Id, ParentId from Attachment where ParentId = :accAppIds order by ParentId]; List linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocument.FileType != 'SNOTE' and LinkedEntityId =: accAppIds order by LinkedEntityId]; for (ContentDocumentLink link : linkList) { // attMap.put(link.LinkedEntityId, link.LinkedEntityId); if(!delDocidList.contains(link.ContentDocumentId)){ attMap.put(link.LinkedEntityId, link.LinkedEntityId); } } List updAccAppList = new List(); //20221207 lt 医院审批优化 start List adaList = [select id,Name, Is_Active__c from Account_Delay_Apply__c where id in:accAppIds ]; //20221207 lt 医院审批优化 end for (String accappid : accAppIds) { if(adaList.size() > 0){ // Account_Delay_Apply__c accapp = new Account_Delay_Apply__c(Id = accappid); //20221207 lt 医院审批优化 start for(Account_Delay_Apply__c accapp : adaList){ if(Trigger.isUpdate && Trigger.isBefore){ for(ContentDocument doc : Trigger.New){ System.debug('状态1=》' + accapp.Is_Active__c); if(accapp.Is_Active__c != '草案中'){ doc.addError('非草案中,附件不允许编辑。'); return; } } } if(Trigger.isDelete && Trigger.isBefore){ for(ContentDocument doc : Trigger.old){ // if(attMap.containsKey(att.ParentID) && accapp.Is_Active__c != '草案中'){ System.debug('状态2=》' + accapp.Is_Active__c); if(accapp.Is_Active__c != '草案中'){ doc.addError('非草案中,附件不允许删除。'); return; } } } } } //20221207 lt 医院审批优化 end if(adaList.size() > 0){ // Account_Delay_Apply__c accapp = new Account_Delay_Apply__c(Id = accappid); //20221207 lt 医院审批优化 start for(Account_Delay_Apply__c accapp : adaList){ accapp.Is_upload_file__c = attMap.containsKey(accappid); updAccAppList.add(accapp); } } } try { if (updAccAppList.size() > 0) update updAccAppList; } catch (Exception e) { linkList[0].addError(e.getMessage()); } } //20221014 yc 医院审批优化 end // List atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds]; if (DeleteReIds.size() > 0) { List linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId =:DeleteReIds]; List idList = new List(); for (ContentDocumentLink link : linkList) { idList.add(link.ContentDocumentId); } List docList = [select Id,CreatedDate,CreatedById from ContentDocument where Id =: idList]; System.debug(DeleteReIds+'222222222222'); List oldList = new List(); for(ContentDocument doc : Trigger.old){ oldList.add(doc.Id); } if(docList.size()>0){ for(String ids : DeleteReIds){ for(ContentDocument doc : docList){ if(oldList.contains(doc.Id)){ continue; } if(OppordeleteReTimeMap.containsKey(ids)){ if(doc.CreatedDate > OppordeleteReTimeMap.get(ids)){ OppordeleteReTimeMap.put(ids,doc.CreatedDate); OppordeleteReUserMap.put(ids,doc.CreatedById); }else{ continue; } }else{ OppordeleteReTimeMap.put(ids,doc.CreatedDate); OppordeleteReUserMap.put(ids,doc.CreatedById); } } } List reDateList = [select id,Last_upload_time__c, Is_Locked__c, Last_upload_user__c from Opportunity_File__c where id in:DeleteReIds ]; if(reDateList.size()>0){ for(Opportunity_File__c ofc : reDateList){ ofc.Last_upload_time__c = OppordeleteReTimeMap.get(ofc.id); ofc.Last_upload_user__c = OppordeleteReUserMap.get(ofc.id); System.debug(ofc+'233333333'); } update reDateList; } }else{ List reDateList = [select id,Last_upload_time__c, Is_Locked__c, Last_upload_user__c from Opportunity_File__c where id in:DeleteReIds ]; if(reDateList.size()>0){ for(Opportunity_File__c ofc : reDateList){ ofc.Last_upload_time__c = null; ofc.Last_upload_user__c = null; } update reDateList; } } } if (oFOrderIdMap.size() > 0) { List oFOrderList = [select Id,Name,LatestUploadUser__c,LatestUploadTime__c from OpportunityFileOrder__c where Id In : oFOrderIdMap.keySet()]; if (oFOrderList.size() > 0 ) { for (OpportunityFileOrder__c oFOrder :oFOrderList) { if (oFOrderIdMap.get(oFOrder.Id)) { oFOrder.LatestUploadUser__c = UserInfo.getUserId(); oFOrder.LatestUploadTime__c = System.now(); } else if (oFOrderIdMap.get(oFOrder.Id) == false){ oFOrder.LatestUploadUser__c = null; oFOrder.LatestUploadTime__c = null; } } update oFOrderList; } } if (accIds.size() > 0) { Map attMap = new Map(); // List attList = [select Id, ParentId from Attachment where ParentId = :accIds order by ParentId]; List linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocument.FileType != 'SNOTE' and LinkedEntityId =: accIds order by LinkedEntityId]; System.debug('客户Link的条数:' + linkList.size()); for (ContentDocumentLink link : linkList) { if(!delDocidList.contains(link.ContentDocumentId)){ attMap.put(link.LinkedEntityId, link.LinkedEntityId); } } System.debug('attMap的条数:' + attMap); List updAccList = new List(); //20221207 lt 医院审批优化 start List accList = [select id,Name, Is_Active__c from Account where id in:accIds ]; //20221207 lt 医院审批优化 end for (String accid : accIds) { if(accList.size() > 0){ // Account acc = new Account(Id = accid); //20221207 lt 医院审批优化 start for(Account acc : accList){ if(Trigger.isUpdate && Trigger.isBefore){ for(ContentDocument link: Trigger.Old){ System.debug(link + 'old'); } for(ContentDocument link : Trigger.New){ System.debug(link + 'new'); if(attMap.containsKey(acc.Id) &&acc.Is_Active__c != '草案中'){ System.debug('lt123---草案中---'); link.addError('非草案中,附件不允许编辑。'); return; } } } if(Trigger.isDelete && (Trigger.isBefore)){ for(ContentDocument link : Trigger.old){ if(acc.Is_Active__c != '草案中'){ link.addError('非草案中,附件不允许删除。'); return; } } } acc.Is_upload_file__c = attMap.containsKey(accid); System.debug('是否上传附件:' + attMap.containsKey(accid)); updAccList.add(acc); } } //20221207 lt 医院审批优化 end } // for (String accid : accIds) { // Account acc = new Account(Id = accid); // acc.Is_upload_file__c = attMap.containsKey(accid); // updAccList.add(acc); // } if (updAccList.size() > 0) update updAccList; } // 20231103 陈京武 Lightning文件修改 End }