Attach Excel file to email

This article shows step-by-step configurations of exporting a data page list to an Excel file and then sending the Excel file as an email attachment.

This demo was initially developed using Pega Platform 8.8.2 and later updated to 23.1.1.

Here is a high-level view of the activity. See the details of each step below.

Step 1 – Set an output Excel file name.

  • Notice the file extension (.xlsx) used.

Step 2 – Generate an Excel file by using an OOTB pxGenerateExcelFile activity.

  • Do NOT select the DownloadFile checkbox.
  • Refer to this article to learn more about how to export a data page list to an Excel file.

Step 3 – Set an output Excel file location.

  • System automatically saves the output Excel file to pxProcess.pxServiceExportPath.

Step 4 – Generate an output Excel file stream byte. This Java step converts the Excel output file to a byte stream.

  • A local variable (strFileData, String) must be defined in the Parameters tab of the activity.
  • A local variable (attach, Boolean) must be defined in the Parameters tab of the activity.

  • Java source code:
String strFileName = tools.getParamValue("OutputLocation");
PRFile objFile = new PRFile(strFileName); 

//some error checking
if (!objFile.exists())
{
  attach = false;
} 

if(!objFile.isFile())
{
  attach = false;
}

tools.putParamValue("OutputLocation", objFile.getName()); 

if (!objFile.canRead())
{
  attach = false;
  throw new PRRuntimeException("Can't continue with file upload. File \"" + strFileName + "\" is unreadable.");
} 

//read the file into a buffer.
java.io.DataInputStream dis = null;
byte buffer [] = null;

try
{
  dis = new java.io.DataInputStream(new PRInputStream(objFile));
  buffer= new byte[dis.available()];                
  dis.readFully(buffer);
  dis.close();
}
catch (Exception e)
{
  attach = false;
  throw new PRRuntimeException("Can't continue with file upload. Can't read File \"" + strFileName + "\"");            
} 

//encode the file to Base64 so that we can store it on the database
strFileData = Base64Util.encodeToString(buffer);

if (strFileData == null)
{
  attach = false;
  throw new PRRuntimeException("Can't continue with file upload. Couldn't encode the file to Base64 so that we can store it on the database");            
}

  • Best practice - Put the Java code in a Rule-Utility-Function rule (see attached doc) and call it from the activity to eliminate the severe Java guardrail warning.

Step 5 – Set the attachment properties.

  • Under ‘Pages & Classes’ of activity, define a page name (Attachment) with Class (Embed-EmailAttachment).
  • Note that .pyData property is set with local.strFileData which contains the Excel file stream byte generated by the previous Java step.

Step 6 – Add the Attachment page to AttachmentList page.

  • Under ‘Pages & Classes’ of activity, define a page name (AttachmentList) with Class (Data-EmailAttachments).
  • CopyInto = AttachmentList.pyAttachments(1)
    • To add another attachment, copy it to AttachmentList.pyAttachments(2) and so on..

Step 7 – Open an Email Account instance, which will be used to send an email with attachment.

  • Under ‘Pages & Classes’ of activity, define a page name (Email) with Class (Data-EmailAccount).

For this demo, a Gmail account was used as Sender.

Step 8 – Set the email parameters.

Step 9 – Call an OOTB SendEmailNotification activity to send out an email with attachment.

Step 10 – Clean up temporary pages.

Click “Run” to test the activity. It should send an email with an Excel attachment.

GetExcelStreamByteFromFunction.docx (237 KB)

@Will Cho

If we use java code pega will not allow use to checkin the rule.
How to overcome this?

Hi @Will Cho

I am Working on Pega 8.7 version , all this steps you mentioned are working well and helped me for my requirement except the last

step “Call SendEmailNotification” is giving me error on Java step itself so instead of this I have used

“Call SendEmailWithAllAttachments” OOTB Activity and its working fine for me.

Thanks

Vighnesh

@AkhilTejaK You will need to add a local parameter to the activity, called attach, of type Boolean

@RameshBattini try Property-Set-Corr method? Pegasystems Documentation?

We have used that successfully at our client. We are sending an attachment using a correspondence rule.

@Will Cho works like a charm - thanks for this useful article. Only difference we have put the java code in a RUF.

@Will Cho How can we put the java code in Rule-Utility-Function as by pasting this code in java block my activity is getting stuck at the time of checkin.

Have already added the local variables still facing this issue.

@TanyaS58 @RameshBattini I just refactored the code using a Rule-Utility-Function rule, and it works fine. Please see attached. I had to add a bit more Java code to make it work using R-U-F, which also removed the severe Java guardrail warning from the activity.

GetExcelStreamByteFromFunction.docx (237 KB)

@AkhilTejaK - not sure if i understood the question. The activity (with Java step) documented above was checked in and ran successfully. There will be a guardrail warning, but it doesn’t prevent check-in. If you elaborate your question further or send any screenshot, i may be able to better provide inputs.

@Vighnesh Nimbalkar Glad that ‘Call SendEmailWithAllAttachments’ made it work for your implementations.

@Will Cho Hello,

Sorry, I deleted my question without refreshing the post as I figured out the solution.

I used Property-Set-Stream method instead of Property-Set-Corr. it did worked. Please let me know if there are any cons with Property-Set-Stream.

Anyway thanks for letting me to know the new thing :slight_smile:

For future reference:

Question: How to use the above approach to send an email with an attachment by passing correspondence as parameter instead of Message as parameter.

@Saikat Dasgupta Glad it worked out well! Putting Java code in Rule-Utility-Function is a great idea.

@Saikat Dasgupta how to put it in RUF?

@TanyaS58 can you first make it work without Rule-Utility-Function as documented in this article? Once worked, then i would try it using RUF. Let’s see what Saikat says. Myself haven’t tried RUF but i would copy & paste the Java code and do some refactoring to make it work.

@Saikat Dasgupta Hello,

Could you please share the modified java code that will work if we put it in a RUF? So that it will be helpful to all :slight_smile:

Thanks

@Will Cho Unfortunately it didn’t work for me so I tried a different approach.

@RameshBattini yes please @Saikat Dasgupta

@Will Cho Great, it worked!!!

Thank you :blush:

@Will Cho I will try this approach once and will let you know.

Thank you

@TanyaS58

This article is very helpful. It guides you through generating a file with data from a specific data type, attaching the generated file to an email, and finally sending the email with the attachment.

Thank You