0
votes

I have a method to retrieve configuration details from a table MyConfiguration. The code currently being used is:

Query query;
QueryRun queryRun;
QueryBuildDataSource qbds;
MyConfiguration config;
int rowCount;

query = new Query();
qbds = query.addDataSource(tableNum(MyConfiguration));
queryRun = new QueryRun(query);
rowCount = SysQuery::countTotal(queryRun);

The table has 0 or 1 rows; there is an if statement of what process to use if there are configuration settings or to use the defaults.

Issue

Although there is a row in the table the query is intermittently returning 0 rows.


Update

Thanks to David's input I have simplified the code:

MyConfiguration config;

select firstOnly useSettings, firstField, secondField from config;

// This wasn't included in the original example, but demonstrates how it's used.
if(config){
    // These variables are defined in classDeclaration
    useCustom = config.useSettings;
    first = config.firstField;
    second = config.secondField;
}
else
{
     // No custom configuration, use defaults.
     useCustom = 0;
}

This code is in a method that is called when the primary method is called to find the configuration to be used.

When I run my test methods in the development environment all the tests pass (the configuration is being read for each test). However when the primary method is called from a button's click event the select isn't returning anything (I've checked this in the debugger). This causes the application to run using the defaults instead of the configured values. If I manually, in the debugger, move the execution past the if the second select also doesn't return any values.

Both the test and the form execute the method in the same way, but are getting different results from the select statement.

1
is MyConfiguration a temp table? Have you tried adding code into the counttotal method to catch the zero occasions and aid debugging? Is there a reason you are not using an X++ select statement? - David Lawson
MyConfiguration is a permanent custom table. Zero rows is a valid state (the system administrator is using the standard process), which is the issue. My knowledge of X++ is extremely limited, so the method I'm currently using is from code found on the web (I'll lookup the SELECT statement now). - KevinManx
What happens if you change "select firstOnly useSettings, firstField, secondField from config" to "select firstOnly config". If you use a field list in the select statement, only those fields are available in the table variable, as per Kennys comment, if the rec id is not present, the if will fail - David Lawson
That had the same issue. It was this way originally and refactorrd it down to the single SELECT statement. It still works from the Test methods, but not from the actual CLICK method. - KevinManx

1 Answers

1
votes

Your code looks right. However the following may be easier to work with and debug

MyConfiguration config;
int rowCount;
;
select firstonly config;
if(config)
{
  //Record exists
}
else
{
  //Record does not exist
}