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 = "";
      String subject = "Some subject";
      String emailBody ="Hello, This email was sent from the Java API";
      String fromName = "Mounika";
      String fromAddress = "";
      String related_to = objectId+":"+recordId;
      Result result = Functions.execSQL("SELECT id,related_to,file_field FROM attachments where related_to = '"+
      int resultCode = result.getCode();
      if (resultCode < 0)
        // Error occurred
        String msg = "Sample: Error during SQL search";"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 =;  // Use a loop if Limit > 1      
          String fileName = params.get("file_field");
"Sample: fileName value = " + fileName, "SQL");  
          if(fileName != ""){
            String tempAttachmentId = fileName.split(":")[1];
        attachmentIdList = StringUtils.join(attachmentsArray,",");
      Functions.sendEmail(objectId, recordId, toAddress,"", "", subject, emailBody, attachmentTemplateIdList,
    catch (Exception e) {
      //Functions.throwError("There was an error. Examine the debug log for details.");
      Functions.throwError("An Error Occurred : " + e.getMessage());