Trying to stop POTF on PO but code isn't working as expected

Trying to stop people adding a part if its not in the part master on PO line type other. This code isn’t working and I’d appreciate any pointers:

bool bNotExists = false;

foreach (var dr in ttPODetail.Where(x => x.Added() || x.Updated()))
{
  bNotExists = !Db.Part.Where(x => x.Company==dr.Company && x.PartNum==dr.PartNum && !x.InActive).Any();
  
  if (bNotExists)
    break;
}

return bNotExists;

Where do you have this code? Method directive or Data Directive?

Also, You should be raising an exception if you are wanting to interrupt the current method from firing. Breaking from your custom code, will just stop the foreach block from continuing to process and continues on with the Epicor method.

throw new Exception("Tell them what you are preventing") 

Oops, I normally include that in with a screenshot. Its a method directive, PO.Update, Pre-Processing

Okay I figured. In that case my above suggestion should still work. Let me know if you have issues!

Hmm. Trying to do to many things this morning. Ignore previous. PO ChangeDetailPartnum

image

It is firing when the part exists in the part master though

Hi Chance,

I am using changedetailpartnum but its still firing when the part exists. I am assuming that since this BO only gets used when adding a new line to a PO that there is only one part to check if it exists in the db already.

Can you show your condition widget logic? If it is still firing when the part number exists, it sounds like your condition is always returning false

Posted screenshot of logic. Thanks

Instead of having custom code in a condition (not familiar with it) i would just use a custom code block and something like the below. **untested

Erp.Tables.Part Part;

foreach(var x in this.ds.PODetail)
{
  var doesItExist = (from row in Db.Part
                      where x.Company == row.Company && x.PartNum == row.PartNum
                      select row);
  if(doesItExist == null)
  {
    throw new Exception("Not allowed");
  }
}