C# code in RcvDtl Data Directive issues with Date Compare

I am trying to create an in-tran BPM to stop users from receiving a PO earlier than 30 days before the promised date.
They mainly use mass receipts to receive the POs. No matter how I try to compare the dates. Nothing happens. Any thoughts?
Code Below.

tooSoon = false;

var rd = ttRcvDtl.Where(x => x.Updated() || x.Added()).FirstOrDefault();

var poRel = Db.PORel.Where(y => y.Company == rd.Company && y.PONum == rd.PONum && y.POLine == rd.POLine && rd.PORelNum == rd.PORelNum).FirstOrDefault();

DateTime myToday = DateTime.Today;



if(poRel.PromiseDt > (myToday.AddDays(30)))
{
  tooSoon = true;
}

First, I believe you have an error in the 3rd line, near the end, you are comparing rdPORelNum to rd.PORelNum… it should be y.PORelNum.

But I also converted this to use an “Any” statement which reduces the number of lines needed by multiple lines of code. I did this by combining you last IF statement directly into the query (afterall, queries ARE big if statements). So now, the query returns a TRUE or FALSE if a record is found. Since that is the case, we dont have to set tooSoon to false first. it will automatically get set to true or false

I also added an IF to make sure we dont do this if the tt record is not found.

DateTime myToday = DateTime.Today.AddDays(30);
var rd = ttRcvDtl.Where(x => x.Updated() || x.Added()).FirstOrDefault();
if (rd != null) {
  tooSoon = Db.PORel.Any(y => 
    y.Company == rd.Company && 
    y.PONum == rd.PONum && 
    y.POLine == rd.POLine && 
    y.PORelNum == rd.PORelNum &&
    y.PromiseDt > myToday);
}

I sure did compare the same field on the same table. missed that one. Thank you. I will give this a try.

1 Like

That is one reason I always break up my queries into multiple lines rather than one long string. While it takes more vertical space, it makes it easier to spot simple mistakes like that. I only saw it when I was breaking into multiple lines.