BPM firing twice on Part Tran

I am trying to make sure the person who picks the part is picking the oldest lot. If they are not picking the oldest lot, I have a raise exception that will stop the transaction and give them the oldest lot. This is an Issue Material Transaction and when they do not pick the oldest lot, the BPM fires twice. I am not sure why this is happening. Not sure if it is in my BPM or if there is a way to stop it in the BPM. Everything in the BPM is below.

First Code Block is to make sure I pull in only parts that are lot tracked:

var pt = ttPartTran.Where(x => x.Added() || x.Updated()).FirstOrDefault();

var part = Db.Part.Where(z => z.Company == pt.Company && z.PartNum == pt.PartNum && z.TrackLots == true).FirstOrDefault();

if(part != null)
lotTracked = true;

Then I have a condition to check if it lot tracked and STK-MTL move.

After that is the code block to get the oldest Lot and run a check to make sure it is the oldest:
var tran = ttPartTran.Where(x => x.Updated()).FirstOrDefault(); //x.Added() ||

if (tran == null)
//msg += “cannot get the part Transaction”;

var oldestLot = Db.PartLot.Where(z =>
z.Company == tran.Company &&
z.PartNum == tran.PartNum &&
z.OnHand == true)
.OrderBy(z => z.ExpirationDate)

if (oldestLot != null)
if(tran.LotNum != oldestLot.LotNum)
alertMsg = true;
old = oldestLot.LotNum;

If AlertMsg is true it will fire the raise exception

it seems like i remember having trouble with PartTran… I think that it might first get ADDED and then UPDATED with additional informaiton. Because your BPM is checking for add OR updated, it may be run twice.
Try only looking for ADDED PartTran records.

ALSO… did you know that you can make this section of code more efficient by using the .any statement in the query? .Any() (instead of .FirstOrDefault()) will return a TRUE if it finds the condition. This is more efficient because it doesn’t return any other data, and also stops when it finds the first record that IS true.
Whenever checking for the presents of a record, it is always better to use .any

Like this:

var pt = ttPartTran.Where(x => x.Added() || x.Updated()).FirstOrDefault();
lotTracked = Db.Part.Any(z => z.Company == pt.Company && z.PartNum == pt.PartNum && z.TrackLots == true).Any();

As you can see, we also eliminted your if statment because of this.


1 Like

I have changed the code to look for x.Added(), but when I make that change, the BPM does not fire. It will fire if I use the .Updated(), but fires twice. I have made a change to look for TranQty > 0 as well but it still fires twice.

I think the BPM is working, except when I show message as warning and have the terminate on error checked true, the transaction still goes through. When I have raise exception in the BPM, it throws the error and stops the transaction, but the Application error does not give the user any details about why the error was thrown. example below. How can I change this to show the information the user needs to select the correct lot.