0

As the title implies. Here is the code.

List<Messaging.SingleEmailMessage> attachmentEmails = new List<Messaging.SingleEmailMessage>();
    Map<Id, Case> caseMap = new Map<Id, Case>();
    Map<String, List<Attachment>> caseNoToAttachmentMap = new Map<String, List<Attachment>>();

    for(Case c : [SELECT Id, CaseNumber
                  FROM Case 
                  WHERE CaseNumber = '00015126']){
        caseMap.put(c.Id, c);
    }
    for(Attachment att : [SELECT Id, Body, Parent.Name, ContentType, Name
                       FROM Attachment where ParentId IN: caseMap.keySet()]){
        if(caseNoToattachmentMap.containsKey(att.Parent.Name)){
            caseNoToattachmentMap.get(att.Parent.Name).add(att);
        }
        else{
            caseNoToattachmentMap.put(att.Parent.Name, new List<Attachment>{att});
        }    
    }
    for(Id caseId : caseMap.keyset()){
        Case c = caseMap.get(caseId);
        if(caseNoToattachmentMap.containsKey(c.CaseNumber)){
            //create Attachments
            Messaging.SingleEmailMessage singEmail = new Messaging.SingleEmailMessage();
            singEmail.setToAddresses(new List<String>{'david.ryan@redpointcrm.com'});
            singEmail.setSubject(c.CaseNumber);
            List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
            Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
            for(Attachment a : caseNoToattachmentMap.get(c.CaseNumber)){
                Messaging.EmailFileAttachment att = new Messaging.EmailFileAttachment();
                String fileName = c.CaseNumber +' '+ a.Name;
                att.setFileName(fileName);
                att.setBody(a.Body);
                string contenttype = a.Contenttype;
                if(contenttype == null){
                    integer lastPeriod = a.Name.lastIndexOf('.');
                    contenttype = a.Name.substring(lastPeriod + 1).toLowerCase();
                }
                system.debug(contenttype);
                if(contenttype == 'pdf'){
                    contenttype = 'application/pdf';
                }
                else if(contenttype == 'jpeg' || contenttype == 'jpg'){
                    contenttype = 'image/jpeg';
                }
                else if(contenttype == 'png'){
                    contenttype = 'image/png';
                }
                else if(contenttype == 'doc' || contenttype == 'docx'){
                    contenttype = 'application/msword';
                }
                else if(contenttype == 'xls' || contenttype == 'xlsx'){
                    contenttype = 'application/vnd.ms-excel';
                }
                else if(contenttype == 'tif' || contenttype == 'tiff'){
                    contenttype = 'image/tiff';
                }
                att.setContenttype(contenttype);
                attachments.add(att);
            }
            if(attachments.size() == 0){
                continue;
            }
            singEmail.setFileAttachments(attachments);
            singEmail.setPlainTextBody('See attachments for case files');
            attachmentEmails.add(singEmail);
        }
    }
    Messaging.SendEmailResult [] r = Messaging.sendEmail(attachmentEmails);

I've seen one similar issue to this on here at Email attachment coming through as .bin

But making some relevant adjustments has not solved the problem. So I'm hoping someone has some insight.

  • So I think I have identified the problem. Attachments were saved in Salesforce with bad names. The ones returning as .url have names that look like "filename.morename.pdf" or in the case of .html the name looks like "filename_morename_pdf" – davidjryan Dec 13 '18 at 22:26
  • Actually, it looks like I'm hitting the limitations of what Salesforce can actually send as attachments per email. Still doesn't explain the .url issue – davidjryan Dec 13 '18 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.