| | |
| | | trigger ContentDocumentTrigger on ContentDocument (before delete) { |
| | | trigger ContentDocumentTrigger on ContentDocument (before delete,before update) { |
| | | if(StaticParameter.ContentDocumentTrigger == false){ |
| | | return; |
| | | } |
| | | if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){ |
| | | return; |
| | | } |
| | | |
| | | // LHJ CHAN-BCLD4P 20190618 Start |
| | | List<String> specIds = new List<String>(); |
| | | // LHJ CHAN-BCLD4P 20190618 End |
| | | System.debug('enter ContentDocumentTrigger'); |
| | | List<String> rentaldelIds = new List<String>(); |
| | | List<String> attdelIds = new List<String>(); |
| | | // LHJ CHAN-BCLD4P 20190618 Start |
| | | List<String> specDelIds = new List<String>(); |
| | | // LHJ CHAN-BCLD4P 20190618 End |
| | | |
| | | List<String> accIds = new List<String>(); |
| | | //20221014 yc 医院审批优化 start |
| | | List<String> accAppIds = new List<String>(); |
| | | //20221014 yc 医院审批优化 end |
| | | List<String> OpporFileIds = new List<String>(); |
| | | Map<String,String> OpporFileUndeleteMap = new Map<String,String>(); |
| | | List<String> DeleteReIds = new List<String>(); |
| | |
| | | String invoiceStart = System.Label.invoiceStart; |
| | | List<String> rentaldelIdsZhj = new List<String>(); |
| | | List<String> attdelIdsZhj = new List<String>(); |
| | | if (Trigger.isBefore && Trigger.isUpdate) { |
| | | List<Id> idList = new List<Id>(); |
| | | for(ContentDocument doc: Trigger.old){ |
| | | idList.add(doc.Id); |
| | | } |
| | | List<ContentDocumentLink> 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (Trigger.isBefore && Trigger.isDelete) { |
| | | List<Id> idList = new List<Id>(); |
| | | for(ContentDocument doc: Trigger.old){ |
| | | idList.add(doc.Id); |
| | | } |
| | | List<ContentDocumentLink> 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')) { |
| | | accAppIds.add(link.LinkedEntityId); |
| | | } |
| | | //20221014 yc 医院审批优化 end |
| | | } |
| | | for (ContentDocument cd : Trigger.old) { |
| | | System.debug('cd.Id = ' + cd.Id); |
| | | List<ContentDocumentLink> cdlList = [SELECT id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId = :cd.Id]; |
| | |
| | | for (ContentDocument doc : Trigger.old) { |
| | | idList.add(doc.Id); |
| | | } |
| | | List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList limit 1]; |
| | | List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList]; |
| | | List<ContentVersion> 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(link[0].LinkedEntityId).startsWith('a2K')) { |
| | | rentaldelIds.add(link[0].LinkedEntityId); |
| | | if (String.valueOf(cid).startsWith('a2K')) { |
| | | rentaldelIds.add(cid); |
| | | attdelIds.add(version[0].Id); |
| | | } |
| | | if(String.valueOf(link[0].LinkedEntityId).startsWith(System.Label.Oppor_sepKey)){ |
| | | OpporFileIds.add(link[0].LinkedEntityId); |
| | | if(String.valueOf(cid).startsWith(System.Label.Oppor_sepKey)){ |
| | | OpporFileIds.add(cid); |
| | | } |
| | | // LHJ CHAN-BCLD4P 20190618 Start |
| | | if(String.valueOf(link[0].LinkedEntityId).startsWith(System.Label.OppSpecialApply)){ |
| | | specDelIds.add(link[0].LinkedEntityId); |
| | | if(String.valueOf(cid).startsWith(System.Label.OppSpecialApply)){ |
| | | specDelIds.add(cid); |
| | | } |
| | | // LHJ CHAN-BCLD4P 20190618 End |
| | | if(String.valueOf(link[0].LinkedEntityId).startsWith('a3Z')){ |
| | | oFOrderIds.add(link[0].LinkedEntityId); |
| | | oFOrderIdMap.put(link[0].LinkedEntityId,false); |
| | | if(String.valueOf(cid).startsWith('a3Z')){ |
| | | oFOrderIds.add(cid); |
| | | oFOrderIdMap.put(cid,false); |
| | | } |
| | | //2023-2-10 sx备品 备品检测分析报告 start |
| | | Id cid = link[0].LinkedEntityId; |
| | | String parentLabel = cid.getSObjectType().getDescribe().getName(); |
| | | System.debug('...' + cid.getSObjectType().getDescribe().getName()); |
| | | if(parentLabel.equals('Rental_Apply_Fault__c')){ |
| | | rentaldeleteApplyFaultIds.add(link[0].LinkedEntityId); |
| | | conDeleteList.add(link[0]); |
| | | rentaldeleteApplyFaultIds.add(cid); |
| | | conDeleteList.add(link); |
| | | if(deleteImages.containskey(version[0].Id)){ |
| | | deleteImages.get(link[0].LinkedEntityId).add(version[0].Id); |
| | | deleteImages.get(cid).add(version[0].Id); |
| | | }else{ |
| | | List<Id> temp = new List<Id>(); |
| | | temp.add(version[0].Id); |
| | | deleteImages.put(link[0].LinkedEntityId,temp); |
| | | deleteImages.put(cid,temp); |
| | | } |
| | | } |
| | | //2023-2-10 sx备品 备品检测分析报告 end |
| | | } |
| | | //2023-2-10 sx备品 备品检测分析报告 start |
| | | List<Rental_Apply_Fault__c> rafList = [SELECT Id, Name FROM Rental_Apply_Fault__c WHERE Id IN: rentaldeleteApplyFaultIds AND (status__c = '已发送' OR status__c = '已反馈')]; |
| | | List<Id> docList = new List<Id>(); |
| | | for (Rental_Apply_Fault__c raf : rafList) { |
| | | for (ContentDocumentLink link1 : conDeleteList) { |
| | | if (link1.LinkedEntityId == raf.Id) { |
| | | // ContentDocument doc = [select Id from ContentDocument where Id =: link.ContentDocumentId]; |
| | | link1.addError('非草案中的检测分析报告不允许删除已上传的故障图片'); |
| | | docList.add(link1.ContentDocumentId); |
| | | } |
| | | } |
| | | } |
| | | for(ContentDocument doc: Trigger.old){ |
| | | if(docList.contains(doc.Id)){ |
| | | doc.addError('非草案中的检测分析报告不允许删除已上传的故障图片'); |
| | | } |
| | | } |
| | | //2023-2-10 sx备品 备品检测分析报告 end |
| | |
| | | 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); |
| | |
| | | idList.add(doc.Id); |
| | | } |
| | | List<ContentDocumentLink> 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(link[0].LinkedEntityId)){ |
| | | if(OpporFileUndeleteMap.containsKey(enId)){ |
| | | doc.addError('备货已审批,不可删除附件'); |
| | | return; |
| | | }else{ |
| | | DeleteReIds.add(enId); |
| | | } |
| | | } |
| | | } |
| | | if(Trigger.isDelete && Trigger.isAfter){ |
| | | List<Id> idList = new List<Id>(); |
| | | for(ContentDocument doc : Trigger.old){ |
| | | idList.add(doc.Id); |
| | | // if(Trigger.isDelete && Trigger.isAfter){ |
| | | // List<Id> idList = new List<Id>(); |
| | | // for(ContentDocument doc : Trigger.old){ |
| | | // idList.add(doc.Id); |
| | | // } |
| | | // List<ContentDocumentLink> 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<ContentDocumentLink> 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<String, String> attMap = new Map<String, String>(); |
| | | // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accAppIds order by ParentId]; |
| | | List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: accAppIds order by LinkedEntityId]; |
| | | for (ContentDocumentLink link : linkList) { |
| | | attMap.put(link.LinkedEntityId, link.LinkedEntityId); |
| | | } |
| | | List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList]; |
| | | for(ContentDocument doc : Trigger.old){ |
| | | // List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: doc.Id]; |
| | | if(OpporFileUndeleteMap.containsKey(link[0].LinkedEntityId)){ |
| | | link[0].addError('备货已审批,不可删除附件'); |
| | | return; |
| | | }else{ |
| | | DeleteReIds.add(link[0].LinkedEntityId); |
| | | List<Account_Delay_Apply__c> updAccAppList = new List<Account_Delay_Apply__c>(); |
| | | |
| | | //20221207 lt 医院审批优化 start |
| | | List<Account_Delay_Apply__c> 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){ |
| | | 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 != '草案中'){ |
| | | if(accapp.Is_Active__c != '草案中'){ |
| | | doc.addError('非草案中,附件不允许删除。'); |
| | | |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | //20221207 lt 医院审批优化 end |
| | | |
| | | } |
| | | try { |
| | | if (updAccAppList.size() > 0) update updAccAppList; |
| | | } catch (Exception e) { |
| | | linkList[0].addError(e.getMessage()); |
| | | } |
| | | |
| | | } |
| | | //20221014 yc 医院审批优化 end |
| | | |
| | | |
| | | |
| | | // List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds]; |
| | |
| | | for (ContentDocumentLink link : linkList) { |
| | | idList.add(link.ContentDocumentId); |
| | | } |
| | | List<ContentDocument> docList = [select Id,ArchivedDate,ArchivedById from ContentDocument where Id =: idList]; |
| | | List<ContentDocument> docList = [select Id,CreatedDate,CreatedById from ContentDocument where Id =: idList]; |
| | | System.debug(DeleteReIds+'222222222222'); |
| | | List<Id> oldList = new List<Id>(); |
| | | 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.ArchivedDate > OppordeleteReTimeMap.get(ids)){ |
| | | OppordeleteReTimeMap.put(ids,doc.ArchivedDate); |
| | | OppordeleteReUserMap.put(ids,doc.ArchivedById); |
| | | if(doc.CreatedDate > OppordeleteReTimeMap.get(ids)){ |
| | | OppordeleteReTimeMap.put(ids,doc.CreatedDate); |
| | | OppordeleteReUserMap.put(ids,doc.CreatedById); |
| | | }else{ |
| | | continue; |
| | | } |
| | | }else{ |
| | | OppordeleteReTimeMap.put(ids,doc.ArchivedDate); |
| | | OppordeleteReUserMap.put(ids,doc.ArchivedById); |
| | | OppordeleteReTimeMap.put(ids,doc.CreatedDate); |
| | | OppordeleteReUserMap.put(ids,doc.CreatedById); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | } |
| | | } |
| | | if (accIds.size() > 0) { |
| | | Map<String, String> attMap = new Map<String, String>(); |
| | | // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accIds order by ParentId]; |
| | | List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: accIds order by LinkedEntityId]; |
| | | for (ContentDocumentLink link : linkList) { |
| | | attMap.put(link.LinkedEntityId, link.LinkedEntityId); |
| | | } |
| | | List<Account> updAccList = new List<Account>(); |
| | | |
| | | //20221207 lt 医院审批优化 start |
| | | List<Account> 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); |
| | | 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; |
| | | |
| | | } |
| | | } |