binxie
2024-01-18 0e0dd1e20e7211f3c3c11d77a41090d998dfd06c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// before update のみ対応した
trigger TemporaryFileBoxUpdate on TemporaryFileBox__c (before update) {
    
    //deloitte-zhj 20231124 本地化导入 start
    if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
        return;
    }
    //deloitte-zhj 20231124 本地化导入 end
    
    List<Id> tfbIds = new List<Id>();
    for(TemporaryFileBox__c tfb : Trigger.new) {
        if (tfb.Status__c == '已申请' || tfb.Status__c == '已批准') {
            tfbIds.add(tfb.Id);
        }
    }
    if (tfbIds.size() > 0) {
        //20231026 于梦辉  附件修改  start
        // List<ContentDocumentLink> linkList = [SELECT LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN: tfbIds];
        // Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>(
        //         [SELECT ParentId Id, Count(Id) Cnt_Id
        //            FROM Attachment
        //           WHERE ParentId IN :tfbIds
        //           GROUP BY ParentId
        // ]);
        
        Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>(
            [SELECT LinkedEntityId Id, Count(ContentDocumentId) Cnt_Id
               FROM ContentDocumentLink
              WHERE LinkedEntityId IN :tfbIds
              GROUP BY LinkedEntityId
        ]);
        //20231026 于梦辉  附件修改  end
        
        for(TemporaryFileBox__c tfb : Trigger.new) {
            AggregateResult ar = arMap.get(tfb.Id);
            if (ar == null || Integer.valueOf(ar.get('Cnt_Id')) == 0) {
                tfb.addError(System.Label.TemporaryFileBoxNeedAttachment);
            }
        }
    }
}