Pages

Monday, July 15, 2019

SugarCRM Customization: Sending Emails - New and Improved

It seems like an eternity since I posted examples on programmatically sending emails through Sugar.

As some of you might have already correctly surmised, a number of changes have occurred to Sugar since those blog entries were written. One of the major additions to Sugar since then has been SugarBPM, which itself includes an email template engine.

Let us revisit the original problem of programmatically sending emails and examine how we can accomplish the same via SugarBPM. This time around, we will assume our intention is to send an email about an Account record.

We will first need to create the email template containing the content to be sent. Said template can be static or, if desired, may also include references to specific fields from the target module, to be parsed at send time. For our example, we may wish to make an email template that reads as follows:

ACME Corporation
123 Main Street
Some City, CA 90000

...but rather than typing specific values, we would instead insert references to the corresponding fields that contain the values we want from the Accounts module -- our Target Module. That would make the template much more flexible.

Note that the process of creating such a template for SugarBPM differs than that for a standard email template. To create an email template for SugarBPM, select Process Email Templates from the Sugar navigation bar and then click Create. Further information pertaining to this process can be found here:

https://support.sugarcrm.com/Documentation/Sugar_Versions/9.0/Ent/Administration_Guide/SugarBPM/Process_Email_Templates/index.html


For the purposes of our example, we would want to make sure to set the Target Module field to Accounts upon creating the new template.

Once you have finished creating the template, take note of the ID value assigned to the entry as we will reference it in our code that sends the message. It should appear in the address bar for your browser and looks like a standard Sugar GUID, similar to this:

940cd9e0-56cc-11e9-b791-0800273eeb0f

Now we get to write some code. To simplify our example a slight bit, we will simply create a logic hook that triggers our code that in turn sends the email message with the Accounts content.

The purpose of our code will be to perform the following tasks:


  1. Define an after_save logic hook
  2. Instantiate a SugarBean object to load the previously created Process Email Template record
  3. Parse the Subject and Body fields of the email template to populate field references with actual Account data
  4. Prepare and send the email message to the desired email addresses


...and here it is...



Note that the SugarBean object containing the Account information can be obtained through a variety of means. It is not necessary to obtain it by way of a logic hook. For example, it is possible for me to create a custom entry point that loads one or more SugarBean Account objects and then uses them at send time.

Hope this helps with any similar customization needs you may have planned!

No comments:

Post a Comment

Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.