Email Notification BPM error

Hello guys i am working in a BPM that send an email notification to a person that place a maintenance request when the maintenance job get close.
this is my code
foreach(var job in (from ordrow in ttJobHead
where (string.Equals(ordrow.RowMod, IceRow.ROWSTATE_UPDATED, StringComparison.OrdinalIgnoreCase))
select ordrow ))
{
foreach(var mr in ( from mrrow in Db.MaintReq
where (mrrow.Company == job.Company)&& (mrrow.JobNum == job.JobNum)
select mrrow ))
{
foreach(var uf in ( from ufrow in ttUserFile
where ufrow.DcdUserID == mr.Requestor
select ufrow ))
{
if(job.JobClosed == true)
{
var mailer = this.GetMailer(async: true);
var message = new Ice.Mail.SmtpMail();
message.SetFrom(“EpicorTest10@ecoreintl.com”);
message.SetTo(uf.EMailAddress.ToString());
//message.SetCC()
//message.SetBcc()
message.SetBody(“

test message

”);
//Dictionary<string, string> attachments = new Dictionary<string, string>();
//attachments.Add(“MyFirstAttachment”, @“\MyServer\myFolder\MyFile.pdf”);
mailer.Send(message);
}
}
}
}

this is the error i get when i test it

Error Detail

Description: BPM runtime caught an unexpected exception of ‘EntityException’ type.
See more info in the Inner Exception section of Exception Details.
Inner Exception: New transaction is not allowed because there are other threads running in the session.
Program: System.Data.Entity.dll
Method: BeginDbTransaction
Original Exception Type: EntityException
Framework Method: SaveChanges
Framework Line Number: 328
Framework Column Number: 17
Framework Source: SaveChanges at offset 391 in file:line:column c:_Releases\ICE\3.1.400.20\source\Framework\Epicor.System\Data\IceDataContext.cs:328:17

Just guessing but you have three foreach loops running and inside the last one you are creating a mailer. It might not be able to do this. I wonder if you add your user mail to info into a list outside of your loops and then iterate through the list to mail, rather than trying to do it while you are iterating through all of the other stuff.

1 Like

I think Aaron nailed it

1 Like

Aaro, do you mean create the email outside the loop and put inside the loop the line that send the email?
thanks for your help

Aaron what i did was declare the mailer and the message variable outside of the loops and that fixed it

Glad to hear!

thanks for your help guys

2 Likes