Announcement

Collapse
No announcement yet.

Example: BPM with Case entity - sending email

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Example: BPM with Case entity - sending email

    Just sharing this as an example for others. This is a BPM Script Task for a case entity that sends an email to the Contacts specified on the Case, as well as the case attachment.
    I couldn't find any examples for the Case entity, and it took awhile to figure out since I didn't know much about attachment behavior.

    Background on our use case: using a workflow to better automate our subscription renewal process with customers. The approach is to create a Case on the Account, specify the appropriate customer contacts (to receive a renewal email), include an attachment containing the customer specific renewal quotation, and utilize an email template that has Placeholder fields. The flow is triggered when the Case status is put in a certain state. Ultimately the flow will go through multiple email reminders as well as past due escalation.​

    Code:
    // Case.attachments is a field of type Attachment Multiple. Get the first one from the array attachmentsIds.
    $attachmentId = array\at(attachmentsIds, 0);
    
    // Clone it
    $clonedAttachmentId = record\create('Attachment',
        'role', 'Attachment',
        'type', record\attribute('Attachment', $attachmentId, 'type'),
        'size', record\attribute('Attachment', $attachmentId, 'size'),
        'global', record\attribute('Attachment', $attachmentId, 'global'),
        'name', record\attribute('Attachment', $attachmentId, 'name'),
        'sourceId', $attachmentId,
        'storage', record\attribute('Attachment', $attachmentId, 'storage')
    );
    
    // Get all Case.contacts emailAddress's
    // Case.contacts field is Link Multiple. Attribute: contactsIds is array of id's.
    $i = 0;
    $toStr= '';
    while (
      $i < array\length(contactsIds),
      (
        $Id = array\at(contactsIds, $i);
        $emailAddr = record\attribute('Contact', $Id, 'emailAddress');
        ifThen(
          $i > 0,
          $toStr=string\concatenate($toStr, ';')
        );
        $toStr=string\concatenate($toStr, $emailAddr);
        $i = $i + 1;
      )
    );
    
    $subject= 'subject placeholder';
    $body= 'body placeholder';
    
    $emailId = record\create(
    'Email',
    'from', 'accounting@mydomain.com',
    'to', $toStr,
    'subject', $subject,
    'body', $body,
    'isHtml', true,
    'status', 'Sending',
    'attachmentsIds', list($clonedAttachmentId),
    'parentId', id,
    'parentType', 'Case'
    );
    
    // Note, when using a template, the following parameters do not need to be defined: 'subject', 'body', 'is Html'
    // Make sure Placeholders are appropriate for Case entity.
    ext\email\applyTemplate($emailId, '655aaea4d69a3d4e4');
    
    // Send the email
    ext\email\send($emailId);​
    Last edited by qpoint; 11-23-2023, 12:34 AM.

  • #2
    Hello @qoint
    thank you for sharing I think we've published video about sending emails with attachments
    How to send email with multiple attachments in EspoCRM - YouTube

    Comment

    Working...
    X