Set BPM context field to second most recent record

I am trying to pull the second to last record from the partTran table and specifically set the BPM context field equal to its attribute set short description. Does anyone know how to do this in a data directive?

Not sure how to get to the attribute set, but you can isolate the second latest record in PartTran with

var ptSecondLatest = Db.PartTran.Where( x => x.Part == "myPartNum" ).OrderByDescending( x => x.TranNum ).Skip(1).First();
2 Likes

Hi Kve, I get this error when I try and use this code. Any advice?

System.Drawing.Bitmap CS1061 ‘PartTran’ does not contain a definition for ‘Part’ and no accessible extension method ‘Part’ accepting a first argument of type ‘PartTran’ could be found (are you missing a using directive or an assembly reference?)

Ach, my apologies, the field is PartNum, not Part, so the correct statement is

var ptSecondLatest = Db.PartTran.Where( x => x.PartNum == "myPartNum" ).OrderByDescending( x => x.TranNum ).Skip(1).First();

This worked great, thanks!

1 Like

Glad it worked!
drakecode

Would you know how to select only transaction type “ADJ-QTY” and order that dataset and pick the second latest one?

Sure, just add it the the “Where” method:

var ptSecondLatest = Db.PartTran.Where( x => x.PartNum == "myPartNum" && x.TranType == "ADJ-QTY" ).OrderByDescending( x => x.TranNum ).Skip(1).First();

Or if you don’t want to also filter by PartNum:

var ptSecondLatest = Db.PartTran.Where( x => x.TranType == "ADJ-QTY" ).OrderByDescending( x => x.TranNum ).Skip(1).First();
1 Like