Can you provide an example?
This sets JobHead.ShortChar09 with codes based on the materials in the job.
/* Set Routes from AAA and Mold on Job Header */
Erp.Tables.Part Part;
Erp.Tables.JobHead JobHead;
foreach (var ttJobMtl_iterator in (from ttJobMtl_Row in ttJobMtl
where ttJobMtl_Row.Added()
select ttJobMtl_Row))
{
var ttJobMtlRow = ttJobMtl_iterator;
Part = Db.Part.Where(Part_Row => ttJobMtlRow.PartNum == Part_Row.PartNum).FirstOrDefault();
if (Part != null)
{
ttJobMtlRow.ShortChar01 = Part.ShortChar01; // Assign Part Class to Job Material
JobHead = Db.JobHead.Where(JobHead_Row => JobHead_Row.JobNum == ttJobMtlRow.JobNum && JobHead_Row.Company == ttJobMtlRow.Company).FirstOrDefault();
if (JobHead != null)
{
if (ttJobMtlRow.PartNum.Contains("CLS") && !JobHead.ShortChar09.Contains("C"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "C";
}
if ((ttJobMtlRow.PartNum.Contains("CO-AX") || ttJobMtlRow.PartNum.Contains("COAX")) && !JobHead.ShortChar09.Contains("X"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "X";
}
if (ttJobMtlRow.PartNum.Contains("BOX") && !JobHead.ShortChar09.Contains("B"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "B";
}
if (ttJobMtlRow.PartNum.Contains("HARN") && !JobHead.ShortChar09.Contains("H"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "H";
}
if (ttJobMtlRow.PartNum.Contains("TROUGH") && !JobHead.ShortChar09.Contains("T"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "T";
}
if (ttJobMtlRow.PartNum.Contains("WHEEL") && !JobHead.ShortChar09.Contains("W"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "W";
}
if ((Part.RefCategory == "MM" || Part.ClassID == "00MM") && !JobHead.ShortChar09.Contains("M"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "M";
}
if (ttJobMtlRow.PartNum.ToUpper().Trim() == "CP-A240-HTHF")
{
Ice.Diagnostics.Log.WriteEntry("CP-A240 FOUND");
if (!JobHead.ShortChar09.Contains("M"))
{
JobHead.ShortChar09 = JobHead.ShortChar09 + "ML";
}
if (JobHead.ShortChar09.Contains("M"))
{
Ice.Diagnostics.Log.WriteEntry("TRYING REPLACE TO ML");
JobHead.ShortChar09 = JobHead.ShortChar09.Replace("M","ML");
}
}
Ice.Diagnostics.Log.WriteEntry($"JobMtl Update: Set Other Route {ttJobMtlRow.JobNum} Part: {ttJobMtlRow.PartNum} route {JobHead.ShortChar09}");
}
}
}
And that updates the JobHead table?
I’m super green at LINQ, so bear with me…
Doesn’t the following just fetch the record from table JobHead?
JobHead = Db.JobHead.Where(JobHead_Row => JobHead_Row.JobNum == ttJobMtlRow.JobNum && JobHead_Row.Company == ttJobMtlRow.Company).FirstOrDefault();
Doesn’t there need to a call to update the actual DB table?
I am green at Linq and C# both, so I can’t explain how it works, but that is the code and it does the update. I was concerned it maybe it was done someplace else so I searched all of my source and that is the only routine that sets the field. I just looked at my data and the contains for M doesn’t stop it from adding another M so I have MMM if there are three mold materials on a job LOL
@gpayne -
Okay, seeing is believing … Using you code as an example, I’m able to set a UD field of an OrderHed record, while in an In-Tran DD of OrderDtl.
Now this has me worried…
If I use something like:
Erp.Tables.JobHead JobHead;
...
JobHead = Db.JobHead.Where(...).FirstOrDefault();
That variable JobHead
is actually linked to the DB table? Doesn’t this allow for changes to records outside of BO’s ??
If I fetched an OrderDtl record, could I change the OrderNum value in code? Or are there things that are builtin to prevent that, and only UD fields can be updated this way. For example:
Erp.Tables.OrderDtl OrderDtl;
OrderDtl = Db.OrderDtl.Where(OrderDtl_Row => OrderDtl_Row.OrderNum == 10000).FirstOrDefault();
if (OrderDtl != null)
{
OrderDtl.OrderNum = 10001;
}
Would that actually change the OrderNum field of the found record of OrderDtl?
edit
maybe changing OrderDtl.OrderNum
was a bad example as that’s a key field. What if it was some other non-key field, Like PartClass?
Yes you could, but you should not. I hardly ever touch any Epicor fields and you would not want to do those this way. This discussion started with an update of a UD adding line 1 of a detail record.