How to send record attachments in an email

This article helps users to send attachments in an email that are attached as files to a record. You can invoke this utility from many places such as event rules record update, owner update, and rule sets, and so on. Use the following utility to collect all the attachments of a record and send to the specific "toAddress". This "toAddress" can be a field in the record as well. You can filter the attachments by modify the "execSQL" where cluase in the code.

For example: If a person A is working on a case and after a while resigns from the organization and the case is transferred to another person called B. In this scenario, this utility is helpful in case of attachments which are related to the case. For this purpose, the user has to invoke this class on "Owner Changed" event rule. For every owner change action, this class is triggered and sends all the attachments.

Pre-requisites to use this utility:

  • Place "commons-lang-2.4.jar" in the AgileApps lib folder. Add this in the "Libraries supported in Java code" of "Configure service settings" of administrator.
  • Create the following class by navigating to   GearIcon.png> Customization > Developer Resources > Classes > New class
package com.platform.mounika.test;

import com.platform.api.*;
import com.platform.api.mail.*;
import com.platform.beans.*;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
public class sendWithAttachments
{
public void sendAttachments(Parameters p) throws Exception {
    try {
      String recordId = p.get("id");
      String objectId = p.get("object_id");
      String attachmentIdList = "";
      String attachmentTemplateIdList = "";
      String toAddress = "xxx@softwareag.com";
      String subject = "Some subject";
      String emailBody ="Hello, This email was sent from the Java API";
      String fromName = "Mounika";
      String fromAddress = "xxx@softwareag.com";
      String related_to = objectId+":"+recordId;
      Result result = Functions.execSQL("SELECT id,related_to,file_field FROM attachments where related_to = '"+
                                        related_to+"'");
      
      int resultCode = result.getCode();
      if (resultCode < 0)
      {
        // Error occurred
        String msg = "Sample: Error during SQL search";
        Logger.info("Sample:\n" + result.getMessage(), "SQL");
      }
      else if (resultCode > 0)
      {
        // A record was found. (Otherwise, resultCode == 0)               
        ArrayList<String> attachmentsArray = new ArrayList<String>();
        ParametersIterator it = result.getIterator();
        while (it.hasNext()) {
          Parameters params = it.next();  // Use a loop if Limit > 1      
          String fileName = params.get("file_field");
          Logger.info("Sample: fileName value = " + fileName, "SQL");  
          if(fileName != ""){
            String tempAttachmentId = fileName.split(":")[1];
            attachmentsArray.add(tempAttachmentId);
          }
        }
        attachmentIdList = StringUtils.join(attachmentsArray,",");
      }
      Functions.debug(attachmentIdList);
      Functions.sendEmail(objectId, recordId, toAddress,"", "", subject, emailBody, attachmentTemplateIdList,
                          attachmentIdList,fromName,fromAddress);      
    } 
    catch (Exception e) {
      Functions.debug(e);
      //Functions.throwError("There was an error. Examine the debug log for details.");
      Functions.throwError("An Error Occurred : " + e.getMessage());
    }
  }
}