BPM C# Sanity Check

Trying to get adapted to doing work in Kinetic browser and started working on the backlog of BPM requests I have.

Wanted to do a simple overreceipt against PO check following the instructions here:

That works in the instance of a single receipt but not in the case of a mass receipt. So I thought I would just make a data directive and use a little C# to get the sum of all prior receipts agianst the po using the following bpm and code:

//initialize the sum variable
decimal summmm = 0;
//itterator for all receipts that match the po poline and porel num that are currently being updated
foreach (var iter in (from r in Db.RcvDtl where r.Company == Session.CompanyID && r.PONum == ttRcvDtl[0].PONum  && r.POLine == ttRcvDtl[0].POLine && r.PORelNum == ttRcvDtl[0].PORelNum select r)) 
{ 
//add to sum whenever a match is found in the iterator
    summmm += iter.OurQty;

}
//pass the sum over to the already received variable I have within the bpm 
poRelRcvQty_c = summmm;
//add the previously received qty to the current qty being added to calculate against further down in the bpm. 
qtySum_c = summmm + ttRcvDtl[0].OurQty;

The code works from a syntax side, and the bpm validates with no errors. But when I do a receipt line addition, I get the following error:

Firstly, when did we start using the “[0]” inside bpms? I swear it used to be “ttRcvDtl.POLine” or “ttRcvDtlRow.POLine”.

Second, for those in the cloud how do we get to the “See more info in the Inner Exception section of the Exception Details” bit of this error? Is it just telling me to run a trace when I do the receipt or is there a module/table that has this data stored that I can hit?

That’s just a bad practice to index into the first row in a table. It’s always been a thing, just not usually recommended. Usually you want a foreach() loop or a .FirstOrDefault() to get into that row.

2 Likes

That’s what I thought. But, if you use tools on the side in the kinetic browser that’s what it gives you.

If you change it to “ttRcvDtl.PONum” you get the following error:

"‘List’ does not contain a definition for ‘PONum’ and no accessible extension method ‘PONum’ accepting a first argument of type ‘List’ could be found (are you missing a using directive or an assembly reference?)
"

If you change it to “ttRcvDtlRow.PONum” you get this:

“The name ‘ttRcvDtlRow’ does not exist in the current context”

Im kind of at a loss for what I should use for context.

So, I usually assign the row to a variable before I get into the linq because you can’t run a method within linq.

var myRow = ttRcvDetail.FirstOrDefault();

Then you can use

myRow.PoNum... etc

in your linq query.

ttRcvDtl is a datatable. It has rows, so you have to tell the system what your you are looking for, not just the column. That’s why [0] works, assuming there is a row. It’s just the first one. In a lot of places, you might have 2 rows, one for the before image, and one for the after image, so you might need to be more selective one which row you are using. Or maybe you have even more, and you have to select it somehow.

var myRow = ttRcvDetail.where(z=>z.RowMod == "U").FirstOrDefault();

for example to get the updated row if there are both.

6 Likes

Aaaah! That makes perfect since. Let me give that a whirl. Thanks Brandon :heart:

That did it! Thanks for helping with the writers block!

Solution to you good sir! :tada:

1 Like

one additional fix you might want to consider is using SQL to do the summing of OurQty. You can change the logic so that instead of itterating through all the records one at a time, let SQL just return the value you want.

New code would look something like this (UNTESTED)

//This first line gets the modified RcvDtl record.
var thisReceipt = ttRcvDtl.Where(x =>x.RowMod == 'U').firstOrDefault();
//this next line of code returns the one value wanted... the summmm
decimal summmm = Db.RcvDtl.Where(x = 
    x.Company == Session.Company && 
    r.Company == Session.CompanyID && 
    r.PONum == thisReceipt.PONum  && 
    r.POLine == thisReceipt.POLine && 
    r.PORelNum == thisReceipt.PORelNum).Select(x =>x.OurQty).Sum();

//pass the sum over to the already received variable I have within the bpm 
poRelRcvQty_c = summmm;
//add the previously received qty to the current qty being added to calculate against further down in the bpm. 
qtySum_c = summmm + thisReceipt.OurQty;
2 Likes