Aim: Get the latest TranDate for a given array of TranTypes then update UserDate1 with this TranDate
List<string> validTranTypes = new List<string> { "STK-CUS", "MFG-CUS", "KIT-CUS", "STK-KIT" }; // Add more if needed
// Retrieve the last item meeting the criteria
var lastPt = ttPartTran.Where(p => p.RowMod == "A" && validTranTypes.Contains(p.TranType))
.OrderBy(p => p.TranDate)
.LastOrDefault();
if (lastPt != null)
{
// Update properties of the last item
if (!string.IsNullOrEmpty(lastPt.PartNum))
{
var part = Db.Part.Where(p => p.Company == callContextClient.CurrentCompany && p.PartNum == lastPt.PartNum).FirstOrDefault();
if (part != null)
{
part.UserDate1 = lastPt.TranDate
part.UserDate1 = callContextBpmData.Date01;
}
}
}
an In-Tran directive in Part Tran is generally playing with fire. Unless you enjoy your entire inventory transaction system slowing down I would move this to either a Standard Directive (After the transaction)
Or better yet it looks like you are just keeping track of the last time a transaction was made. Run a function a couple of times a day to update this.
In a busy system you are going to be hitting this part tran table thousands and thousands of times a day, and now every time you are also adding a query to the Part Table.
Further more you are using callContextClient.CurrentCompany depending on where the transaction came from there a re lot of PartTran records that get inserted outside the client (user clicking a button) and so there may not be anything in callContextClient sometimes that is straight up null which means that is your query is writing above callContextClient.CurrentCompany will throw a null object exception in the middle of your part transaction. Same goes for CallContextData
If these calls come from REST, EMWW, ECC and others those records are not going to be populated.
I would go back to the drawing board and figure out what is it that you are trying to record here and why? It might be easier to just do a Dashboard or a FKV that does a MAX on TranDate by part that someone can run, or just add it as a BAQDataView to the Part Tracker screen.
Unfortunately I do, and I fought it tooth and nail, however it is super narrow scope and I hate that it exists. It is not something you should NEVER do, it is something you should avoid (IMO)
Since there are like 40 ways to get the company value I did some research into what would be the most consistent not too long ago, and from what I found Session Company was the best place to get the value from. A lot of the other methods pull their value from Session Company.
This. I mean, I don’t see why you’d really want this field. One can always hop over to Part Tran Hist Tracker. But if I had to add a last transaction date to the Part window, I’d look it up on-the-fly when users went into Part Entry/Tracker.
I always go with Method Directives first, but if its a fire and forget in your case ParTran Standard Directive is a good candidate it fires when PartTran is done, it is meant for Logging, Async Tasks, Emailing, Notifications etc…
Standard:
Executes when service method call has completed
Executes only if service method completes without exception
Processes batch of affected rows at once
Does not affect data save
Ideal place for integration operations (Audits, Email, Logging, Notifications, API)
Create BAQ that gets last transaction for a given part.
Add it as a DataView to Part form.
Assign results to control on form.
No UD fields that need updating. It only does the lookup when you’re pulling up a part in the specific form where people want that piece of info. Exact implementation depends on whether or not you’re running Classic or Web UI.