trigger SetAttToDocForEmail on SMARM__c (before update) { 
 | 
    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; 
 | 
        } 
 | 
    } 
 | 
} 
 |