API Page-OnInsertRecord(BelowxRec: Boolean): Boolean

IvonaKIvonaK Member Posts: 115
Hi all,

I have created a new custom API page, and I have seen in a book and in the standart API pages that

exit(false) is used in OnInsertRecord(BelowxRec: Boolean): Boolean on API pages.

vx3lsnxa198i.png


Could you please tell me why is like that. In which cases I have to use exit(false) ?

Thanks
Ivona

Answers

  • vaprogvaprog Member Posts: 1,144
    You have to return FALSE if you have inserted the record yourself in code, or if you don't want the record to get inserted at all.
    The return value behaves the same for all 3 database related triggers (i.e. the ones shown above).

    Microsoft's documentation on this is completely wrong. Here is, what the documentation for NAV 5.0 had to say (This was for a Form, not a Page, but it is still the same.)
    OnInsertRecord
    The C/AL code in this trigger is executed before the system inserts a new record into the table.

    [Ok]:= OnInsertRecord
    Ok

    Data type: boolean

    This return value tells the system whether it should insert the record. The system checks the return value after each function call.
    If Ok is...  The system will...
    TRUE         Insert the record (default)
    FALSE        Not insert the record
    

    Even back then, the documentation for the BelowxRec parameter was wrong, as it still is, and even missing from the function's signature as given in the documentation. But, at least, the effect of the return value is described correctly here.
  • IvonaKIvonaK Member Posts: 115
    Hi,

    thanks for the feedback, but just as Info:

    when I removed the exit(false) from OnInsertRecord(BelowxRec: Boolean) on API page, I was not even able to insert a new record from the postman.

    I was getting the following error message:

    9ch7kvnih4n0.png

    Regards
  • vaprogvaprog Member Posts: 1,144
    edited 2023-12-07
    Well, yes, if any code executed before the exit inserts the record, you must return false, or you will get the error you got.

    As the documentation says:

    - the default return value is true
    - if you return true, the system will insert the record

    It is standard behavior that you (or the system in this case) get the error "The record ... already exists" if you try to insert a record with a primary key that already exists in the database, which is true as soon as you INSERTed the record.
  • IvonaKIvonaK Member Posts: 115
    Hi,

    Thanks a lot. I got your point now :smile:

    Have a nice weekend
Sign In or Register to comment.