Odd Function behavior in reading Customer table

Version 2024.1 On-Premise

I’ve got an app studio customization where there’s a button that calls a function, passing it two parameters (pPkgCode, pCustID). In the function, I do some editing to ensure it’s a valid Customer ID and does some additional edits on the packaging code. I have an output parameter that’s a message that the calling application displays.

When I call this function the first time, it successfully reads the Customer table. However, if I turn around and call this a second time with the same parameters, it does NOT find the customer row. I’ve stripped out the rest of the code, and it’s consistent. If I close my calling app and come back in, it’s the same thing, I read the customer table successfully the first time, fails on all subsequent reads.

My stripped down code is as follows:

var context = (Erp.ErpContext)Ice.Services.ContextFactory.CreateContext();
var erpContext = new Erp.Internal.Lib.CCredChk(context);

pOutMessage = string.Empty;
// this line verifies the values of the parameters
pOutMessage = "Parameters: " + pMode + "-" + pPkgCode + "-" + pCustID + "<<\n";

var cust = ( from crow in erpContext.Db.Customer
     where crow.Company == Session.CompanyID
        && crow.CustID == pCustID
     select new { crow.Name } ).FirstOrDefault();
if (cust == null )     pOutMessage += "Invalid Customer ID: " + pCustID + ".\n";

if ( pOutMessage != string.Empty ) 
{
    erpContext.Dispose();
    context.Dispose();
    return;
}

So, the first time i run it, I just get the Parameters, so the “if cust == null” doesn’t execute as it found the customer ID. Click the button a second time with the same parameters, it does the same read, but this time the “if cust == null” executes.

Just curious. Why are you creating a DbContext?

In the function, just include the customer table and use Db Customer in the query expression instead.

var cust = ( from crow in Db.Customer
     where crow.Company == Session.CompanyID
        && crow.CustID == pCustID
     select new { crow.Name } ).FirstOrDefault();
3 Likes

I agree about the context, no need for that.

Test your function in the rest helper repeatedly. I have a feeling there is something off in the UI and not your function.

1 Like

Thanks for the notes on the context. I think when I first started doing table reads in functions, the code that I must’ve copied from had it - and I’ve just been doing it like that. I added the table to the references in the functions, was able to remove it.

I was typing up more details including the messages that were coming back, and I noticed a space in front of the customer ID the 2nd time. Makes sense - I added logic that I’m clearing out the field on the screen that I’m pulling it from. So, I was using the row-setter widget to set TransView.CustID to " " (with a space) instead of “” (empty string). When I went into the field the second time and put my custID back in, it sent a space + the custID, so it was validly kicking it out. Talking it through here helped - thanks guys!

4 Likes