Hi Matt. Below is the code we use to insert rows into UD04 as a Post Process on SalesOrder.MasterUpdate. We get an order for multiple items, and we decide where we want to build the order based on the product mix. I had help with this, and the comments are a little clumsy, but hopefully you can make sense of it.
CODE
using(var UD04svc = Ice.Assemblies.ServiceRenderer.GetService<UD04SvcContract>(Db))
foreach (var CurrentOrder in ttOrderHed) // Identify the Order that we have in ttOrderHed, should only ever be one OrderHed
{
bool addMetaRow = true; /Initialize our variables/
string orderNumChar = CurrentOrder.OrderNum.ToString();
string headerMfgLocation = "";
bool mainAlways = false; // Are ANY of the lines on the order MA?
bool mainDefault = false;// Are ANY of the lines on the order MD?
bool hnvrAlways = false;// Are ANY of the lines on the order HA?
bool hnvrDefault = false; // Are ANY of the lines on the order HD?
bool hnvrCB = false; //01-26-19 If HNVR, then set the checkbox
// Now find all the lines on the order that we are working with, that donāt have āNoneā for a location and put them into a list (ToList)
var CurrentOrderLinesList= (from row in Db.OrderDtl where row.Company == callContextClient.CurrentCompany && row.OrderNum == CurrentOrder.OrderNum && row.MfdLocation_c != "None" select row).ToList();
// Evaluate one line at a time, and set the Order Level Variables above based on what you find on each line
foreach (var CurrentOrderLine in CurrentOrderLinesList)
{
if (CurrentOrderLine.MfdLocation_c == "MA") {
mainAlways = true;
}
else if (CurrentOrderLine.MfdLocation_c == "MD") {
mainDefault = true;
}
else if (CurrentOrderLine.MfdLocation_c == "HA") {
hnvrAlways = true;
}
else if (CurrentOrderLine.MfdLocation_c == "HD") {
hnvrDefault = true;
}
}
// Depending which Order Level Variables are true, decide how to set headerMfgLocation
if (!mainAlways && !mainDefault && !hnvrAlways && !hnvrDefault) {
headerMfgLocation = āNONEā;//whenever we find a match, we set the location, and then stop, no more ELSE IFās after that
}
else if (mainAlways && hnvrAlways) {
headerMfgLocation = "CONFLICT"; // This is not allowed by policy - no orders should have a mix of HA/MA
}
else if (mainAlways) {
headerMfgLocation = "MAIN";// if it has any MA and the rest are HD or MD, we make in MAIN
}
else if (hnvrAlways) {
headerMfgLocation = "HNVR"; // if it has any HA and the rest are HD or MD, we make in Hanover
hnvrCB = true; //01-26-19
}
else if (!mainDefault && hnvrDefault) {
headerMfgLocation = "HD";
}
else {
headerMfgLocation = āMDā;// if we didnāt find a match yet, then it will do this. The final Else is āMDā
}
// Now only if we have a headerMfgLocation other than MAIN or NONE do we want to proceed with creating rows in UD04 if they donāt exist yet, update them if they d
//if (headerMfgLocation != "MAIN" && headerMfgLocation != "NONE") {
// we are moving the line above to evaluate when ADDING a new line - we always want to update existing lines
// loop through each line in the order (we already have the list from before)
foreach (var CurrentOrderLine in CurrentOrderLinesList) {
bool addDtlRow = true;
string orderLineChar = CurrentOrderLine.OrderLine.ToString();
// create a database scope - this will tell the subsequent Db.Validate and txScope.Complete what table it is updating
using (System.Transactions.TransactionScope ourScope = IceDataContext.CreateDefaultTransactionScope()) // part of Synch Location logic below
{
// look to see if if the Row exists in UD04 for the Current Order Line, and loop through - 1 row or none
foreach(var ud04ExistingRow in (from ud04 in Db.UD04 where ud04.Company == callContextClient.CurrentCompany && ud04.Key1 == orderNumChar && ud04.Key2 == orderLineChar && ud04.Key3 == "0" select ud04))
{
// We found it, so we donāt want to add it, but we want to make sure the SC01 is up to date
addDtlRow = false;
//START - Synch the Location if already present:
// since it was already there, letās perform the actual update if it doesnāt match the latest re-calc
var ud04ExistingRowForUpdate = ud04ExistingRow;
if (ud04ExistingRowForUpdate.ShortChar01 != headerMfgLocation) {
ud04ExistingRowForUpdate.ShortChar01 = headerMfgLocation;
Db.Validate();
ourScope.Complete();
}
// END -Synch the Location if already present
}
}
// if we didnāt find a ud04 row, add one
if (addDtlRow)
{
if (headerMfgLocation != "MAIN" && headerMfgLocation != "NONE") {
// if we donāt have any existing rows, we donāt want to write them if MAIN or NONE (to keep UD04 as clean as possible)
UD04Tableset ds = new UD04Tableset();
UD04svc.GetaNewUD04(ref ds);
ds.UD04[0].Key1 = orderNumChar;
ds.UD04[0].Key2 = orderLineChar;
ds.UD04[0].Key3 = "0";
ds.UD04[0].ShortChar01 = headerMfgLocation;
ds.UD04[0].CheckBox08 = hnvrCB; //01-26-19
UD04svc.Update(ref ds);
}
}
}
//}
// see comment above - we are moving the line above to evaluate when ADDING a new line - we always want to update existing lines
}