Code Help, calling any invoice that has specific Order

Greetings all!

Very amateur coder here. I’m trying to create a BPM that doesn’t allow users to make “engineered = true” if the Order is COD, unless the Order is fully paid. I tried to reference DocDepositBal but it’s not working as intended. It seems as though I’ll have to search the db for any existing invoice that is linked to the Order Num that is linked to the Job header.

I’ve attached the code below, but can paste it here as well.

var JobHead =(from i in ds.JobHead where i.Company==Session.CompanyID &&(i.RowMod=="A" ||i.RowMod=="U") select i).FirstOrDefault();
  if(JobHead!=null)
  {
    var JobProd =(from j in Db.JobProd where j.Company==Session.CompanyID && j.JobNum == JobHead.JobNum select j).FirstOrDefault();
    if(JobProd!=null)  
        {
         string orderNumber = Convert.ToString(JobProd.OrderNum);
         int OrdNum = Convert.ToInt32(orderNumber);
  
         var dbOrderHed =(from row in Db.OrderHed where row.Company == Session.CompanyID && row.OrderNum == OrdNum select row).FirstOrDefault();
         if(dbOrderHed != null)  
            {
              string termsCode = Convert.ToString(dbOrderHed.TermsCode);
              callContextBpmData.Character01 = termsCode;
              
              /*this is the problem area */
              var dbInvcRow =(from row in Db.InvcHead where row.Company == Session.CompanyID && row.OrderNum == OrdNum select row).FirstOrDefault();
              if(dbInvcRow != null)
                {
                callContextBpmData.Number01 = 1;
                }                  
            }
        }
    }

CODTerms.txt (1.1 KB)

I’m using callContextBpmData.Number01 to store as 1 if the invoice header exists. This way I can use it for testing, but I can also say in the final design, if it is 1, then do this, else Do this. The way it is currently written, it will always return 1.0.

Any coders out there that can help me get this right?

OK… i bit on this… the code can be simplified. but this is NOT TESTED (I may have done some typos).
Things I did:

  1. combined the three first queries into one query
  2. the one query only returns the two values you need (Order Number and Terms code)
  3. the 3rd query only looks for the data to exist (the quickest type of query). it also “correctly” looks in the invoice detail (instead of invoice header) to find the order numbers that are linked. This is because a single invoice could reference multiple orders.
var oHead = Db.dbOrderHed.Where(x=>x.Company == Session.Company &&
    ds.JobHead.Where(i=>i.Company == Session.Company && (i.RowMod=="A" || i.RowMod=="U") && 
    Db.JobProd.Where(j=>j.Company==Session.CompanyID && j.OrderNum == x.OrderNum && j.JobNum == i.JobNum)))
    .Select(x=>new{x.OrderNum,x.TermsCode}).FirstOrDefault();

if (oHead != null) {
    callContextBpmData.Character01 = oHead.TermsCode;
    //look in the invoice DETAIL to find out if there are any references to this order
    bool invoiceFound = Db.InvcDtl.Any(x=>x.Company == Session.CompanyID && x.OrderNum = oHead.OrderNum);
    if (invoiceFound) callContextBpmData.Number01 = 1;
}
1 Like

Hello Tim!

Thank you for the quick response. I never implemented point 1 of your fix, because it gave me some obscure error, and I found it a little more difficult to understand/follow the code, given my amateur level. However, I did use your point 3! This seems to have worked. After using what you suggested, when I was testing, it seemed to only output “1” no matter if there were invoices or not. I added a line before checking invcDtl to set callContextBpmData.Number01 = 0;
This seems to reset it every time this code runs, and will only change it to 1 if an invoice is found.

CODTerms.txt (1.1 KB)

Thanks for the help!

Dave