How to see if a record is empty?

jensthomsenjensthomsen Member Posts: 173
Is it possible to check if a record- variable isn't empty? This is what I want to do:

Record.GET(som primary key value);

The check would return TRUE here, since we have a record.

CLEAR(Record);

The ckeck should now return FALSE, since the record-pointer points to "no-where"...

Comments

  • DenSterDenSter Member Posts: 8,305
    edited 2008-04-14
    IF MyRecord.GET(PrimaryKeyValue) THEN
      MESSAGE('the record exists')
    ELSE
      MESSAGE('the record does not exist');
    
    If you do MyRecord.GET on a record that doesn't exist, you will get a runtime error.
  • tinoruijstinoruijs Member Posts: 1,226
    Maybe the ISEMPTY-function?

    From help:
    ISEMPTY (Record)
    Use this function to find out whether a C/SIDE table or a filtered set of records is empty.

    Empty := Record.ISEMPTY
    Record

    Data type: record

    Refers to the table or the filtered set of records to be checked.

    Empty

    Data type: boolean

    Comments
    This function tells you whether the table is empty or not if you have not applied filters to the record. If you have applied filters, the function determines whether the filtered set of records is empty.

    The number of filters that you have applied to the records affects the speed of the ISEMPTY function. The fewer the number of filters, the faster the system performs the operation.

    When you are using SQL Server, this function is faster than using the Record.COUNT function and then testing the result for zero.

    Tino Ruijs
    Microsoft Dynamics NAV specialist
  • EugeneEugene Member Posts: 309
    1) Record.GET without parameter returns a record with empty primary key which is possible and is used for various setup tables (those tables that hold one record of data)

    2) your record variable Record may hold outdated values both for primary key (table record got deleted or renamed by someone) and for other field (someone modified your current record)

    one thing you could do to update your information is to reget your record

    Record.GET(Record."Primary Key Field(s)")
  • kinekine Member Posts: 12,562
    Is it possible to check if a record- variable isn't empty? This is what I want to do:

    Record.GET(som primary key value);

    The check would return TRUE here, since we have a record.

    CLEAR(Record);

    The ckeck should now return FALSE, since the record-pointer points to "no-where"...

    For me main question is "Why you need that"?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • jensthomsenjensthomsen Member Posts: 173
    kine wrote:
    Is it possible to check if a record- variable isn't empty? This is what I want to do:

    Record.GET(som primary key value);

    The check would return TRUE here, since we have a record.

    CLEAR(Record);

    The ckeck should now return FALSE, since the record-pointer points to "no-where"...

    For me main question is "Why you need that"?

    I want to prevent to many database calls: For example a setup table is being used many places. If the table have been read once from the database, theres no need to read it again....
  • PerJuhlPerJuhl Member Posts: 55
    Hi

    What do you mean with "points to no-where" ?

    Your initial question was "if a record is empty".

    BR Per
  • tinoruijstinoruijs Member Posts: 1,226
    I want to prevent to many database calls: For example a setup table is being used many places. If the table have been read once from the database, theres no need to read it again....

    I don't think a couple of extra recSetup.GET(); will influence the performance of the database.
    Off course it's important you place the recSetup.GET(); in the right place. Not for example in a repeat-until-loop.

    Tino Ruijs
    Microsoft Dynamics NAV specialist
  • kinekine Member Posts: 12,562
    Setup reading optimalization is done in standard codeunits ant tables through special function e.g.:
    GetGLSetup()
    IF NOT GLSetupRead THEN BEGIN
      GLSetup.GET;
      IF GLSetup."Additional Reporting Currency" <> '' THEN BEGIN
        Currency.GET(GLSetup."Additional Reporting Currency");
        Currency.TESTFIELD("Unit-Amount Rounding Precision");
        Currency.TESTFIELD("Amount Rounding Precision");
      END;
    END;
    GLSetupRead := TRUE;
    

    It means that special global variable GLSetupRead is used to flag if the setup was read or not. You can use SingleInstance codeunit with such a function which will return the record as a parameter to read the setup table just once in one session (but it means that when you change the setup, you need to restart the client to have new setup) and call that codeunit's function everywhere in code when you need to get the setup...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.