Help with Custom code in BPM

Good Day,
I have created a BPM to send an email through custom code when a Shipment in Customer shipment entry gets set to Shipped. My issue is it needs to been sent to the ShipTo contact in the Sales Order and I am having an issue with the code to query that information.
Forgive me I am new to C# and would appreciate any guidance!

Hi Eric,
Could you give us what you have so far?

Sure … very little honestly see below. Im not sure how to reference the OrderNumber from the Shipping Entry in the first query (or if the query is even correct). Then I think I would need to query the Contact table to grab the email address…

Erp.Tables.OrderHed OrderHed;

var orderRow = (from OrderHed_Row in OrderHed where OrderHed_Row.Company == Session.CompanyID
&& OrderHed_Row.OrderNum == orderNum);

var mailer =

I would not let me post the mailer code lol something about links… anyway I know that part works…

The ShipDtl has the OrderNum link, not the ShipHead

image

Indeed it is my bad… so in my query do I need to reference the ShipDtl table in the Where clause like OrderHed_Row.OrderNum == ShipDtl.OrderNum

Yes, just keep in mind, if there will ever be a case where there may be more than 1 ShipToContact on the order, you will need some special handling.

For example, if you have 2 lines with diff contacts, you would have to make 2 emails. But, if you have 2 lines with same contacts, I assume you wouldnt want to send that person 2 emails.

Yes thanks makes sense will need to code for that exception for sure!

so now I have the following code and I am getting 2 exceptions - Invalid Expression term ‘select’ and Use of unassigned local variable ‘ShipDtl’

Erp.Tables.OrderHed OrderHed;
Erp.Tables.ShipDtl ShipDtl;
var orderRow = (from OrderHed_Row in Db.OrderHed where OrderHed_Row.Company == Session.CompanyID
&& OrderHed_Row.OrderNum == ShipDtl.OrderNum &&
select OrderHed_Row).FirstOrDefault();

What BO and method are you using to trigger this?

you’re most likely going to want something like:

var myDtls = ttShipDtl.Where(d => d.Added() || d.Updated());
var uniqueOrders = myDtls.GroupBy(g => g.OrderNum);
foreach(var ordernum in uniqueOrders)
{
      //from here you can lookup the orders
      //Your email sending code here, will send one for each unique company
}

I am using the Erp.CustShip.UpdateMaster method to trigger the BPM. Our company for the majority uses one line orders so when the PackSlip is set to shipped we want an email to go to the order ship contact.

Erp.Tables.OrderHed OrderHed;
Erp.Tables.CustCnt CustCnt;

var myDtls = ttShipDtl;
var uniqueOrders = myDtls.GroupBy(g => g.OrderNum);
foreach(var ordernum in uniqueOrders)
{

 var orderRow = (from OrderHed_Row in Db.OrderHed where OrderHed_Row.Company == Session.CompanyID 
    && OrderHed_Row.OrderNum == 93777
    select OrderHed_Row).FirstOrDefault();
 var contactRow = (from contact_Row in Db.CustCnt where contact_Row.ShipToNum == orderRow.ShipToNum select contact_Row).FirstOrDefault();
 
 

 var body = "testing mail " + contactRow.EMailAddress;

 var mailer = this.GetMailer(async: true);
 var message = new Ice.Mail.SmtpMail();
 message.SetFrom("shipping@craigattachments.com");
 message.SetTo("eporter@craigattachments.com");
 //message.SetCC()
 //message.SetBcc() 
 message.SetBody(body);
 
 mailer.Send(message);

}

So I am getting the email address now however I am stumped as to how to reference the OrderNum in the first query without hard coding it. I tried declaring a variable and setting it to ttShipDtl.OrderNum and it works but only when there is one line in the order otherwise I get an error. Im sure it is something really simple I am overlooking here… when I try to put ttShipDtl.OrderNum in the == query it throws an error?

ttShipDtl is a collection of rows, not just one so you cant do:
ttShipDtl.OrderNum

If you have the expectation they are all the same, you can do:
ttShipDtl.FirstOrDefault().OrderNum

Otherwise you need to pick the proper record based on some criteria. Also, it always best to do a null check of FirstOrDefault() before trying to access it, since it can be null if no record exists, which leads to an exception

1 Like

Awesome that works! Thanks for the help!

Hi can you please share how this was done? I have a similar requirement. Did you start with BPM on ShipHead table? Thank you.