yumenghui
2023-08-04 17bd5b18e21c9f46aefdbf3400262764f0def4da
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
// before update のみ対応した
trigger TemporaryFileBoxUpdate on TemporaryFileBox__c (before update) {
    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) {
        // List<ContentDocumentLink> linkList = [SELECT LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN: tfbIds];
        Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>(
            [SELECT LinkedEntityId Id, Count(ContentDocumentId) Cnt_Id
               FROM ContentDocumentLink
              WHERE LinkedEntityId IN :tfbIds
              GROUP BY LinkedEntityId
        ]);
        // Map<Id, AggregateResult> arMap = new Map<Id, AggregateResult>(
        //         [SELECT ParentId Id, Count(Id) Cnt_Id
        //            FROM Attachment
        //           WHERE ParentId IN :tfbIds
        //           GROUP BY ParentId
        // ]);
        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);
            }
        }
    }
}