I have a BPM with a variable that gets set in a Execute Custom Code block, and then reference the variable in a Send Email block. But the variable doesn’t seem to pass to the Send Email block.
Variable is defined in the BPM via
The variable is set in the Exec Code block via
`this.TestBody = "Hello World! (TestBody)";`
And then referenced in the Send Email block via
The Execute Code Block sends out an email (via mailer.Send(message); ) just fine. This second email is to let me know the first one processed.
The email from the Send email block does execute, but all the variables are blank.
(highlighting mine)
I know I could do the second email in the Exec Code, to get the desired functionality.
But why doesn’t the variable work in the Send Email block.
I inserted another Exec Cust Code (ECC) block between the original one and the Send Email block, and set the variables in there. These appear in the email that’s sent.
So it must be something in my main ECC block.
The main ECC block has a foreach loop to cycle through Packers matching my condition. An email is generated for each packer found.
Erp.Tables.ShipHead ShipHead;
foreach (var ShipHead_iterator in (from ShipHead_Row in Db.ShipHead
where ShipHead_Row.Company == Session.CompanyID && ShipHead_Row.ShipStatus == "SHIPPED" && (ShipHead_Row.ShipDate == today || (ShipHead_Row.ShipDate == yesterday && ShipHead_Row.ChangeTime >= ((12+4) * 3600)))
orderby ShipHead_Row.PackNum
select ShipHead_Row)){
.... code here to build the email ....
mailer.Send(message);
this.TestBody = "Hello World! (TestBody)";
}
The Send Email block has the TestBody variable in the body of the email. Without the second ECC block (which just sets TestBody to a static string) , the email doesn’t show the TestBody variable. But when the 2nd ECC is there, the email shows the TestBody variable - with the static string from the 2nd ECC.
I know the foreach loop is processing, because I’m getting the emails it generates. I expect TestBody to be “Hello World! (TestBody)”.
To be clear, the code fragment above is from an Exec Cust Code block that creates and sends an email for each packer found. The variable TestBody is used in a separate BPM block (a Send Email block).
I’ve since set the last line of the code of ECC#1 to
this.TestBody = "Hello World! (TestBody outside of foreach loop)";
The entire code of ECC#2 is
this.TestPackNum=26697; // this.TestBody="Hi There!\n"; // this.PackNumList = "TPN set in ECC #2";
the TestBody line is commented out. I’d expect it to be unchanged from when it was set in ECC#1
With the above, the email I get (from the Send Email block) has:
In order for the Execute Custom Code(ECC) block to process, it has to be set to Asynchronous. Setting it to Synchronous, yields an error at run time. Why, is a question for a different thread.
So with the ECC block set to Asynchronous, the next block (Send Email), executes before the variables are set within the ECC. Seems logical, so I’ll accept that as the reason.