// 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); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
} 
 |