Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Rec.ISEMPTY vs Rec.HASRECORDS

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Anyone else having their sanity chipped away by repeatedly writing a bug into code by using "NOT Rec.ISEMPTY" incorrectly? ISEMPTY does exactly what it says, and in a timely manner, but somehow just feels like it goes against the whole positive naming convention.

I doubt there will be much force behind any attempt to have an HASRECORDS or like named function added, but it really would keep things consistent seeing as every devloper I meet makes the same mistake on occasion, and it sits there in code as the brain of every developer happily filters the ISEMPTY line out when they look at the logic.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Rec.ISEMPTY vs Rec.HASRECORDS

    Well I'm definitely lost in double negatives now.

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Rec.ISEMPTY vs Rec.HASRECORDS

    No one here autogenerate responses.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Rec.ISEMPTY vs Rec.HASRECORDS

    I'm going to take a wild stab that you guys are autogenerating replies and not reading the posts.

  • Robert B. Profile Picture
    Robert B. 257 on at
    RE: Rec.ISEMPTY vs Rec.HASRECORDS

    Stefano is right, if you iterate for sure you want to use something along the lines of findfirst or findset, depending on what you need to achieve.

    My previous suggestion was strictly to the point of what's the most efficient alternative to see if you have records in a record variable, without necessarily doing something with it afterwards, but triggering another event in case no record is found. And the fastest one is a method that doesn't retrieve a record from the database.

    Of course considering BC has the Azure platform behind the scenes and assuming that there is no crazy iteration of the code in question, you would assume a find is actually best fit at the end of the day, it won't impact your performance.

    I personally prefer using findfirst, or findset, depending on what I want to do after I find the records, but there are situations when using isempty makes perfect sense.

    For instance if a certain filtered record.isempty then I may want to create it with an insert.

    Cheers,

    Robert

  • Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Rec.ISEMPTY vs Rec.HASRECORDS

    If the scope is to check if a table contains records, ISEMPTY is faster that COUNT (and it's the fastest way).

    The onlything to remember is that ISEMPTY or COUNT do not fetch any record from the table.

    So, a bad code is the following:

    Customer.SETFILTER(“Currency Code”, ‘<>%1’, ”);

    IF NOT Customer.ISEMPTY() THEN

    REPEAT

                   …

    UNTIL (Customer.NEXT = 0);

    Use one of the FIND() functions if you want to iterate through, or use the filtered records.

  • Robert B. Profile Picture
    Robert B. 257 on at
    RE: Rec.ISEMPTY vs Rec.HASRECORDS

    Hi Mark,

      Have you tried rec.Count or Countapprox methods?

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/recordref/recordref-count-method

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/recordref/recordref-countapprox-method

    Cheers,

    Robert

    If this helps in resolving your issue, please verify my answer!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans