Hey Everyone,
I am working on kind of an odd data directive. Basically we are putting the project ID on the OrderHed in a UD field. Almost all of our orders are going to have the order lines be on the same project (but not always) so I have some code that looks at any updated OrderDtl records on a data directive, and if there is only 1 unique project number, will set the header to that number. This will be used to default any new lines that are added.
If they change all of the project numbers, it will see when they change the last one, that they are back to a single unique project number, and change the header project accordingly.
If they have more than one project on the order, it just sticks with the first project it was set to. They’ll have to figure out what they want to do with orders on 2 different project.
Anyways, all of that works fine except (puts on QA hat) except for one possible situation. If the number of unique project ID’s is reduced to a singe ID by the act of deleting a line, I will still need to update the header project. What I’m finding though, is that when the rowMod is D (deleted) my Db.Validate() is not updating the database.
Is this is known thing? Anyone have any workarounds for this? Below is the code for this.
using (var txScope = IceDataContext.CreateDefaultTransactionScope())
{
List<string> projects = new List<string>();
foreach (var x in ttOrderDtl.Where(x=> x.RowMod == IceRow.ROWSTATE_UPDATED || x.RowMod == IceRow.ROWSTATE_DELETED))
{
int exOrdLine = 0;
if (x.RowMod == IceRow.ROWSTATE_DELETED)
{
exOrdLine = x.OrderLine;
}
projects = (from ordLine in Db.OrderDtl
where ordLine.Company == x.Company
&& ordLine.OrderNum == x.OrderNum
&& ordLine.OrderLine != exOrdLine
select ordLine.ProjectID).ToList();
if (x.RowMod == IceRow.ROWSTATE_ADDED)
{
projects.Add(x.ProjectID);
}
List<string> uniProj = new List<string>();
uniProj = projects.Distinct().Where(p=> !string.IsNullOrEmpty(p)).ToList();
int projCount = uniProj.Count();
if (projCount == 1)
{
var orderHead = (from oh in Db.OrderHed
where oh.Company == x.Company
&& oh.OrderNum == x.OrderNum
select oh).FirstOrDefault();
if (orderHead.ProjectID_c != uniProj[0])
{
//the message line in here are for debugging. Bothg of the them fire correctly during both update and delete, but the DB is only updated during an update
InfoMessage.Publish($"orderhead project id {orderHead.ProjectID_c.ToString()} uniProj: {uniProj[0].ToString()}");
orderHead.ProjectID_c = uniProj[0];
InfoMessage.Publish($"orderhead project id {orderHead.ProjectID_c.ToString()} uniProj: {uniProj[0].ToString()}");
Db.Validate();
}
}
InfoMessage.Publish(string.Join(",", uniProj));
}
txScope.Complete();
}