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
trigger SetAttToDocForEmail on SMARM__c (before update) {
    
    //deloitte-zhj 20231124 本地化导入 start
    if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
        return;
    }
    //deloitte-zhj 20231124 本地化导入 end
    
    for (SMARM__c s : Trigger.new) {
        if (Trigger.oldMap.get(s.Id).get('Status__c') != s.Status__c && s.Status__c == '填写完毕') {
            Schema.DescribeSobjectResult smarm = SMARM__c.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> fieldMap = smarm.fields.getMap();
            
            List<Document> docList = new List<Document>();
            // AttachmentをDocumentに
            for (Attachment att : ControllerUtil.getSmarmAtts(s.Id)) {
                docList.add(new Document(
                    Name = att.Name,
                    DeveloperName = 'sm_' + s.Id + '_' + ControllerUtil.generateRandomStr(18),
                    Type = 'png',
                    Body = att.Body
                ));
            }
            ControllerUtil.createDocForEmail(docList);
            Map<String, String> docMap = new Map<String, String>();
            for (Document doc : docList) {
                docMap.put(doc.Name, doc.Id);
            }
            
            for (Integer i = 1; i <= Integer.valueOf(System.Label.SmarmClipNum); i++) {
                String reportNameApi = 'ReportName' + i + '__c';
                if (fieldMap.get(reportNameApi) != null && s.get(reportNameApi) != null) {
                    if (docMap.containsKey(i + '.' + s.get(reportNameApi))) {
                        s.put('Doc' + i + '__c', docMap.get(i + '.' + s.get(reportNameApi)));
                    }
                }
            }
            s.DocSaveFinish__c = true;
        }
    }
}