Hello, I made a server function that validates if an invoice number already exists, I get inspiration from the community’s question How to Remove or Add Record.
The statement “if (recs.recordCount() > 0)” was always false even when the record exists and the query returns 1.
I finally get this working by parsing the queryresult recs (thanks Chatgpt) with:
const total = parseInt(recs.records[0]?.RecCount || “0”, 10);
Is this a normal way of doing things?
Thank you!
Hello,
For documentation on recordCount, refer to
I may need you to supply the FDF or show me some code snippets to verify this one if possible!
Thanks,
Riley.
I have a table with three records that the Number value is 2, so I expect the count WHERE Number = 2 is 3.
This is the SQL query:
function SelectCount(five, context, result) {
const recs = five.executeQuery(‘SELECT count(*) as RecCount FROM Person WHERE Number = 2’);
five.log("RecordCount = "+recs.recordCount());
const total = parseInt(recs.records[0]?.RecCount || “0”, 10);
five.log("RecordCountParsed = "+total);
return five.success(result);
}
And this is the log:
The recs.recordCount() function always returns 1 no matter how many records where Number = 2.
Thanks for the hint, the function recordCount() is easier to use than select count(*).
I also attached de fdf file.
TestCount-20250118-155229845870058.fdf (3.4 MB)
Hello,
recordCount() is always returning 1 as your query uses SELECT count(*) which will always only return one record (unless of course you are using something like a group by).
If you wanted to use recordCount() to check how many records have a number equal to 2, you would need to use something like:
SELECT * FROM Person WHERE Number = 2
Or you could also use something like
SELECT PersonKey FROM Person WHERE Number = 2
Hope this helps,
Thanks,
Riley.
1 Like
Yes that helps very much, thanks you!