Using Function to run BAQ and update Part_UD fields

I’m learning to use Functions for the first time and can’t quite get it to work. I have a BAQ that calculates the first Go Negative Date (mirrors the time phase). It’s huge and takes too long to run in a dashboard so I want to take the information and populate it into Part UD Fields (GoNegDate & GoNegUpdated).

My Function runs without errors but the Part Table isn’t being updated. I have function DB Access from Code set to Read/Write and the Updateable box checked on my Part table reference.

BAQ:

Invoke Ice.DynamicQuery.ExecuteByID then run custom code:


Code:

var resultsTable = baqResults.Tables["Results"];

foreach (System.Data.DataRow row in resultsTable.Rows)
{
    var partNum = row["Part2_PartNum"].ToString().Trim();  // trim to prevent mismatch
    var goNegDate = row["Calculated_GoNegMain"] as DateTime?;
    var updatedDate = row["Calculated_UpdatedDate"] as DateTime?;

    var part = Db.Part.FirstOrDefault(p => p.PartNum == partNum);
    if (part != null)
    {
        part.GoNegDate_c = goNegDate;
        part.GoNegUpdated_c = updatedDate;
        updatedCount++;
        lastPart = partNum;
    }
}
Db.SaveChanges();
1 Like

Is that the replacement for Db.Validate();? Just curious, we use Db.Validate(); on everything because an Epicor consultant did long ago and we found we had issues if we didn’t do it. If Db.SaveChanges(); is the new version, I want to start using that instead :slight_smile: (I don’t REALLY know what Db.Validate() does other than it does save the changes as part of it)

It is (and make sure your func is set to read\write on DB). Also, I highly recommend ALWAYS joining\filtering on COMPANY!

4 Likes