BPM on SalesOrderUpdate, want to update OrderDtl field when multiple criteria in OrderHed and OrderDtl met

I am not sure if I am taking the best path forward, but here is what I am trying to do.

When an order is created with a specific ShipTo location for a specific Part, I want to capture the OrderDtl.PickListComment, and add some additional verbiage on that comment for each line that matches the part number.

Initially I started looking at using multiple Method Directive on SalesOrder.ChangeShipToCustID and SalesOrder.ChangeShipToID and SalesOrder.GetNewOrderDtl and SalesOrder.ChangePartNum and SalesOrder.ChangePartNumMaster; trying to cover all situations like the CSR adding the lines then changing the ShipTo, or selecting the ShipTo and then changing or adding lines. I could not get that to work like I wanted.

So I tried the SalesOrder.Update pre-processing with:

The first condition: (ds.OrderHed.ReadyToFulfill field has been changed from False to True) AND (The ds.OrderHed.ShipToCustNum field of the changed row is equal to the XCUSTNUMX expression) AND (The ds.OrderHed.ShipToNum field of the changed row is equal to the “XSHIPTONUMX” expression)

The second condition: Number of rows in the MyQuery is more or equal to 1:
My query looks like this:
image
Where the criteria on the ERP.OrderDtl is PartNum is equal to the specific part I am looking for.
So if there is at least one line where the part number matches, then this condition is true.

I used a show message widget so I could see what was getting captured. Data only existed on the ds.OrderHed table and NO data existed on the ds.OrderDtl table (because the update only happened on the header table and not the detail table).

Then I started looking to capture some variables, thinking I could use those to update the OrderDtl.PickListComment on Every line of the Sales Order that matches this specific part number.

I grabbed the “original” OrderDtl.PickListComment with the following expression:

(from x in Db.OrderDtl where x.OrderNum == dsOrderHedRow.OrderNum && x.PartNum == "XPartNumberX" select x.PickListComment).FirstOrDefault()

But I suspect the FirstOrDefault is only finding the first line that matches, when there could be multiple lines.

I grabbed the Line number of the specific part with the following expression:

(from x in Db.OrderDtl where x.OrderNum == dsOrderHedRow.OrderNum && x.PartNum == "XPartNumberX" select x.OrderLine).FirstOrDefault()

Once again, I suspect this is only capturing the first and not all occurrences.

How would I update my code to capture an array of all matches and then how would I write those changes back to the correct lines on the OrderDtl table?

Or am I doing this all wrong and should use a different approach?

Yes, FirstOrDefault() will return the first line only. You can do something like this:

var odetails = (from x in Db.OrderDtl where x.OrderNum == dsOrderHedRow.OrderNum && x.PartNum == "XPartNumberX" select x.PickListComment);
foreach (var odetail in odetails)
{
     if odetail.PartNum=="something" ...
}

For updating, I don’t have all the info but I would try this:

  1. SO.Update.Pre BPM where you have all the conditions you need. If they are true, then enable the BPM below (use Enable Standard Directive widget)
  2. SO.Update.Post BPM that will do the actual updating of the SO lines (basically calling another update). Ideally, I would have this whole update in a separate function and just call it here.

So will that capture an array? How would I direct the updated comments to the correct lines on the Order?

Because we allow the flexibility to our CSRs to enter sales order in any order they want to (entering the ShipTo location first or after adding lines), I changed how I handled this task. I created Two Post-Processing Method Directives. One is on SalesOrder.ChangePartNumMaster and the other is on SalesOrder.ChangeShipToID. The one on ChangePartNumMaster I was able to update the comment box with the message for the CSR and provided a popup to notify the CSR that had occurred. If the CSR changes the ShipTo after the lines are added, I provide a PopUp Message, but I could not figure out how to update the correct lines on the order. Does anyone know how to cycle through the lines, find the lines that have matching part numbers, then not erase any possible existing text in the Lines-Comments-Pick-List/Job box but add a note to it?

Here is what my BPM looks like:

Because this update is occurring on the SalesOrder Header, no data exists on the ds.OrderDtl table.
How can I determine which lines match the part number I am looking for?
How can I copy and add to the existing comments on all the lines that match the part number I am looking for?