BPM Part Track SN

I am trying to build a Pre-Proc BPM on Part.Update that would Raise an Exception when a user checks the Track Serial Number checkbox for a part with 0 quantity on hand. I have been trying to use custom code more often but I am still pretty much a beginner at coding in C#.
The exception message doesn’t get triggered when the BPM is enabled. What Am i doing wrong ?



foreach (var part in ds.Part.Where(o => o.Added() || o.Updated()))
{
    // Check if TrackSerialNum changed from false to true
    bool trackSerialNumChanged = part.Updated() && part.TrackSerialNum == false && part.TrackSerialNum == true;

    // Check if PartWhse.OnHandQty is zero
    bool onHandQtyZero = ds.PartWhse.Any(pw => pw.PartNum == part.PartNum && pw.OnHandQty == 0);

    if (trackSerialNumChanged && onHandQtyZero)
    {
        var msg = $"The Stock for this part is equal to zero, please do not check the Track Serial Number option";
        throw new Ice.BLException(msg);
    }
}

It would probably be better if you put your code directly in your post and used the code markdown.

like this

Done. Thanks for the heads up

I am no expert, so don’t put too much stock in this. But that does not look right. How can a check box be both false and true?

you are probably right, I don’t really know how to code this condition…

You are getting into the area where you need to compare what the value used to be to what it is being changed to. Only advice I can give is to do a trace and look at the datasets to see if there is a way to identify if the field was changed.

the default value for this field is False so maybe changing the condition to this might work?

bool trackSerialNumChanged = part.Updated() && part.TrackSerialNum == true;```

I believe that will check every time someone does an update and the part is flagged, which you may want.

Ok, I am going to follow your advise and run a trace.

Eric - just curious about the business scenario this addresses. Seems like this would mean you wouldn’t/couldn’t add a new SN-tracked part. And if you already had QOH, how would you assign SNs to the quantity already on hand, as they would not have SNs.