BPM custom code assistance

I have a post-processing directive in the Quote.CreateOrder method directive. I am needing to trigger an email by ordernum and orderline based on prodcode criteria. I have custom code used to bring in the order table fields onto the quote BO. I am only getting one email triggered even when I have multiple lines on an order with all matching the prod code criteria. Example, if a order has two lines with included prodcode there would be two separate emails sent for each line of that order. I am assuming I need to incorporate the “for each” syntax used in c# to add per order and line for a loop. I am very new to C# so any assistance with my current custom code would be ideal. Thank you.

can you include the code in TEXT form instead of a screenshot? We can adjust and return it to you… note that if you put three backwards single quotes followed by the letter c (```c) on a line by itself in your post here, then the code below that will be formatted as c#

OH… and I think i see your problem… your code says to get the “First or default” which is probably only returning the first line that matches your requirements, and not a whole set.

c(’’'c)

Erp.Tables.OrderHed OrderHed;
Erp.Tables.OrderDtl OrderDtl;
Erp.Tables.QuoteHed QuoteHed;

    OrderHed = (from OH_row in Db.OrderHed
        where OH_row.Company == Session.CompanyID && OH_row.OrderNum == orderNum
        select OH_row).FirstOrDefault();
    if (OrderHed != null)
    {
     ordernum = OrderHed.OrderNum;
   orderdate = OrderHed.OrderDate;
   shipbydate = OrderHed.RequestDate;
   needbydate = OrderHed.NeedByDate;
     OrderDtl = (from od_row in Db.OrderDtl
            where od_row.Company == OrderHed.Company && od_row.OrderNum == OrderHed.OrderNum 
      && 
      (
      prodcode != "1010" ||
      prodcode != "1020" ||
      prodcode != "1030" ||
      prodcode != "1040" ||
      prodcode != "1050" ||
      prodcode != "1060" ||
      prodcode != "1090" ||
      prodcode != "2090" ||
      prodcode != "3010" ||
      prodcode != "4010" ||
      prodcode != "4020" ||
      prodcode != "9000" 
      )
            select od_row).FirstOrDefault();
      if (OrderDtl != null)
      {
      orderline = OrderDtl.OrderLine;
    partnum = OrderDtl.PartNum;
    prodcode = OrderDtl.ProdCode;
    orderqty = OrderDtl.OrderQty;
    
        QuoteHed = (from qh_row in Db.QuoteHed
            where qh_row.Company == OrderHed.Company && qh_row.QuoteNum == OrderDtl.QuoteNum
            select qh_row).FirstOrDefault();
        if (QuoteHed != null)
        
        {
         
        }
      }
      Db.Validate(OrderHed);
    }

ok… here is your code, revised:

  1. i changed the queries to a different format that uses lambda code, making it shorter
  2. I simplified the list of Product Groups
  3. after retrieving the list of order details (oDtl_Rows) then I do a foreach through all the found records.
var oHed = Db.OrderHed.Where(x => x.Company == Session.CompanyID && x.OrderNum == orderNum).FirstOrDefault();

if (oHed != null) {
    ordernum = oHed.OrderNum;
    orderdate = oHed.OrderDate;
    shipbydate = oHed.RequestDate;
    needbydate = oHed.NeedByDate;

    //lets get all the rows that have the right product group
    var oDtl_Rows = Db.OrderDtl.Where(x =>
        x.Company == oHed.Company &&
        x.OrderNum == oHed.OrderNum &&
        "1010,1020,1030,1040,1050,1060,1090,2090,3010,4010,4020,9000".Contains(x.ProdCode);
    );
    //now lets process each row that was found above
    foreach (var oDtl in oDtl_Rows) {
        orderline = oDtl.OrderLine;
        partnum = oDtl.PartNum;
        ProdCode = oDtl.ProdCode;
        orderqty = oDtl.OrderQty;
        var qHed = Db.QuoteHed.Where(x => x.Company == oHed.Company && x.QuoteNum == oDtl.QuoteNum).FirstOrDefault();
        if (QuoteHed != null) {

        }
    }
    Db.Validate(OrderHed);
}
1 Like

Thanks Tim, I receive the below errors when validating your code.

Oops… a few typos…
Try this:

var oHed = Db.OrderHed.Where(x => x.Company == Session.CompanyID && x.OrderNum == orderNum).FirstOrDefault();

if (oHed != null) {
    ordernum = oHed.OrderNum;
    orderdate = oHed.OrderDate;
    shipbydate = oHed.RequestDate;
    needbydate = oHed.NeedByDate;

    //lets get all the rows that have the right product group
    var oDtl_Rows = Db.OrderDtl.Where(x =>
        x.Company == oHed.Company &&
        x.OrderNum == oHed.OrderNum &&
        "1010,1020,1030,1040,1050,1060,1090,2090,3010,4010,4020,9000".Contains(x.ProdCode)
    );
    //now lets process each row that was found above
    foreach (var oDtl in oDtl_Rows) {
        orderline = oDtl.OrderLine;
        partnum = oDtl.PartNum;
        prodcode = oDtl.ProdCode;
        orderqty = oDtl.OrderQty;
        var qHed = Db.QuoteHed.Where(x => x.Company == oHed.Company && x.QuoteNum == oDtl.QuoteNum).FirstOrDefault();
        if (qHed != null) {

        }
    }
    Db.Validate(OrderHed);
}
1 Like

Thanks Tim, there is still one error resulting.

the “Db.Validate” line at the end is unnecessary at this point. You can comment it out, UNLESS you are actually modifying data in the OrderHed… in that case, you need to change it to:

Db.Validate(oHed);
``

Great, thanks Tim.

Hey Tim, any idea as to why I would only get 1 line emailed for this? For example, I have a 3 line order, 2 of the 3 lines match the product group criteria being line 1 and line 3. However, I am only getting an email for line 3, but I should be getting an email for lines 1 and 3.