Business rule doesn't run on insert sys_email

r_gissarl_
Mega Expert

Hi,

In order to custom emails, I need to add attachements, images when the email is on the outbox. So to intercept the email before it is sent, I created a business rule on insert / update on sys_email. But the business rule is not ran on insert (maybe the code creating the email has a setWorkflow(false)). It's run with a manual update of the email.

 

Do you have an idea on how to modify an email before it is sent ?

 

Business rule :

on insert and update

condition (valid conditions) : current.mailbox.name == 'Outbox' && (current.body + "").indexOf((new u_emailSpecialProcess()).tag) > -1

Script :

(function executeRule(current, previous /*null when async*/) {
    gs.log("in the BR before");

    var emailUtil = new u_emailSpecialProcess();
    emailUtil.waitForProcess(current);
    gs.log("in the BR after");

})(current, previous);

2 REPLIES 2

Dan English
Tera Contributor

Was just looking for something else and ran across this question.  Because emails are generated and sent so quickly, are you creating this rule as a BEFORE insert type?

I would expect you would want to make any updates prior to inserting the record, because as soon as you do it's going to start processing it.

Hi Dan,

 

Yes, the type was obviously BEFORE. I was never able to run a BEFORE business rule when a record is created in the sys_email table.

But, another trick is working, create the business rule with an AFTER type. And in the very first lines of your processing script, update the current record with an Ignored state (state) and a Processing type (type) [user setWorkflow(false) to avoid another business rules to run]. The email is not sent by the system (at least in Istanbul version).

When the process is over, you can set the two fields with :

      email.setValue("state", "ready");
      email.setValue("type", "send-ready");
      email.setWorkflow(false); //Do not need to run business rules again
      email.update();

And the email will be sent by the system.