FUYU
2023-12-13 4488f711dbc01a8db6753907cae2ef4021dede68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 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) {
        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);
            }
        }
    }
}