Announcement

Collapse
No announcement yet.

BPMN Script Task send email with applied template (missing variables)

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

  • BPMN Script Task send email with applied template (missing variables)

    **Describe the bug**
    When using BPMN Script Task to send an email, variables are not being passed to the applied email template. (Email is sent correctly but variables are not passed to the email template)


    **To Reproduce**
    Steps to reproduce the behavior:

    1. Create a BPMN and use Script Task (define some variables) - create email and apply email template and execute send email to the target entity, code below:

    Code:
    // define variables
    $period = datetime\today();
    $firstContact = "Lynda Doe";
    $secondContact = "James Doe";
    
    // create email
    $emailId = record\create(
    'Email',
    'to', entity\attribute('emailAddress'),
    'isHtml', true,
    'status', 'Sending',
    'parentId', id,
    'parentType', 'Account'
    );
    
    // apply existing template 
    ext\email\applyTemplate($emailId, '61a7fddee0219119e');
    
    //send email
    ext\email\send($emailId);
    2. Check email received, see attached screenshot

    **Expected behavior**
    When sending email using a Script Task via BPMN, if an email template is applied to the email we would like to have variables passed to the email template.


    **EspoCRM version**
    Espocrm version 7.0.8
    Advanced pack version 2.8.9
    Attached Files

  • #2
    It's not a bug. Variables was never supported when using formula to apply email template. Only when using Send Message task.

    Comment


    • #3
      As a workaround you can use string\replace function to substitute values into placeholders in the email body.

      Comment


      • #4
        Hi yuri

        Thanks for your reply, i can use string\replace to replace only one variable per section of my email body however i have a section that includes two variables and string\replace wouldn't work, can you please advise what i am missing:

        Code:
        // define my variables
        $period = datetime\today();
        $firstContact = "Lynda";
        $secondContact = "James";
        
        // using string\replace  (this section has only one variable to replace)
        $sectionOne = string\replace('We will now do a series of preliminary checks, including a review of sales process for period {period} to ensure there is sufficient basis to proceed, and will report back to you directly with the outcome.', '{period}', $period);
        
        // This section has two variable to replace - not sure how this code should be written
        $sectionTwo = string\replace('Assuming the outcome is favourable, we will then make contact with {firstContact} to discuss usage and with {secondContact} to define how we can proceed.', '{firstContact} {secondContact}', $firstContact $secondContact);
        
        $emailBody = string\concatenate(
                             $sectionOne, '\n',
                             $sectionTwo);
        
        $emailId = record\create(
        'Email',
        'to', entity\attribute('emailAddress'),
        'isHtml', true,
        'status', 'Sending',
        'body', $emailBody,
        'parentId', id,
        'parentType', 'Account'
        );
        
        // send email
        ext\email\send($emailId);
        So the email is sent and only period is passed to the email body as correct value but $firstContact and $secondContact are still not passed, can you please advise how to replace multiple variables within a section of the email ?

        Many thanks
        Attached Files
        Last edited by rabii; 12-02-2021, 09:24 PM.

        Comment


        • #5
          Hi rabii,

          Code:
          $period = datetime\today();
          $firstContact = "Lynda";
          $secondContact = "James";
          
          $sectionOne = string\replace('We will now do a series of preliminary checks, including a review of sales process for period {period} to ensure there is sufficient basis to proceed, and will report back to you directly with the outcome.', '{period}', $period);
          
          $sectionTwo = string\replace('Assuming the outcome is favourable, we will then make contact with {firstContact}', '{firstContact}', $firstContact);
          
          $sectionThree = string\replace(' to discuss usage and with {secondContact} to define how we can proceed.', '{secondContact}', $secondContact);
          
          $emailBody = string\concatenate(
          $sectionOne , '<br>',
          $sectionTwo, $sectionThree);
          
          $emailId = record\create(
          'Email',
          'to', entity\attribute('emailAddress'),
          'isHtml', true,
          'status', 'Sending',
          'body', $emailBody,
          'parentId', id,
          'parentType', 'Account'
          );
          
          ext\email\send($emailId);
          Documentation:
          https://docs.espocrm.com/administration/formula/#stringreplace
          Last edited by Vadym; 12-03-2021, 11:04 AM.

          Comment


          • #6
            Thanks Vadym i end up creating another custom formula function which accept arrays and works better. My email is very long and will hard to break it besides i want to use html version so my solution worked fine.

            Thanks again

            Comment


            • #7
              rabii, could you please share your solution?
              Perhaps this formula will be useful to someone.

              Comment


              • #8
                HI Vadym

                Sure, i just created another string\replace formula similar to existing one but i excluded the test for returning string (str_replace php function by default accept string/array values) and stored my html email template in a variable and send email, below code step by step:

                1 - create a new php file called NewReplaceType.php under custom\Espo\Custom\Core\Formula\Functions\StringGr oup\NewReplaceType.php
                Code:
                <?php
                namespace Espo\Custom\Core\Formula\Functions\StringGroup;
                
                use Espo\Core\Formula\{
                Functions\BaseFunction,
                ArgumentList,
                };
                
                class NewReplaceType extends BaseFunction
                {
                public function process(ArgumentList $args)
                {
                $args = $this->evaluate($args);
                
                if (count($args) < 3) {
                $this->throwTooFewArguments();
                };
                
                
                $search = $args[0];
                $replace = $args[1];
                $string = $args[2];
                
                return str_replace($search, $replace, $string);
                
                }
                }
                2 - Create formula.json under custom\Espo\Custom\Resources\metadata\app\formula. json

                Code:
                {
                "functionList": [
                "__APPEND__",
                {
                "name": "string\\newReplace",
                "insertText": "string\\newReplace(SEARCH, REPLACE, STRING)"
                }
                ],
                "functionClassNameMap": {
                "string\\newReplace": "Espo\\Custom\\Core\\Formula\\Functions\\StringGroup\\NewReplaceType"
                }
                }
                3 - use the new way replace function as below when sending your email

                Code:
                // Declare your variables (you can search for certain contact etc from within the crm)
                $salesPeriod = datetime\today();
                $firstContactName = "Lynda";
                $secondContactName = "James";
                
                // Store your email template into a variable like below
                $emailTemplate = '
                <p style="font-size: medium">Hi,</p>
                <p style="font-size: medium">Thank you for choosing our company as your sales partner.</p>
                <p style="font-size: medium">
                We will now start working on new sales process for your sales department starting <b>salesPeriod</b> to ensure there is sufficient basis to proceed, and will report
                back to you directly with the outcome <b>within 2 working days of receipt of both your Financial Statements and Tax Computation for the periods in question</b>.
                </p>
                <p style="font-size: medium">
                Assuming the outcome is favourable, your designated sales manager will then make contact with <b>firstContactName</b> to discuss current activities and with <b>secondContactName</b> to define work strategy
                </p>
                <p style="font-size: medium">Kind Regards,</p>
                ';
                
                // build arrays of searchable variables and variables to replace them
                $searchable = list('salesPeriod', 'firstContactName', 'secondContactName');
                $replacable = list($salesPeriod, $firstContactName, $secondContactName);
                
                // Prepare email body using the newReplace function
                $emailBody = string\newReplace($searchable, $replacable, $emailTemplate);
                
                // Create and send your email
                $emailId = record\create(
                'Email',
                'to', entity\attribute('emailAddress'),
                'isHtml', true,
                'status', 'Sending',
                'body', $emailBody,
                'parentId', id,
                'parentType', 'Contact'
                );
                ext\email\send($emailId);
                I hope this helps, code could be refactored as required. But this is how i did it and it worked as a charm

                Thanks
                Last edited by rabii; 12-04-2021, 06:49 PM.

                Comment

                Working...
                X