Skip to main content
Business Central forum
Answered

Create mandatory field

editSubscribe (0) ShareShare
ReportReport
Posted on by 543

Hi experts,

I would like to create a mandatory field on the table "Shipping Agent". It is an integer.

If I write my validation code in the OnInsert trigger on the table extension, it is too soon. When the user leaves the first field, it is validating the field that the user hasn't come to yet. That doesn't work.

If I write my validation code in the OnModify trigger on the table extension, it is not executed if the user simply enter a value in the first field and then leave the record. That doesn't work either.

I have another table, that I have created in my app. That table has a page, where I have set "DelayedInsert = true". That works, but I cannot set this in a page extension, but only directly on the page.

How do I make a simple and secure mandatory field, so that it is impossible for the user to create a record with zero in my integer field?

Categories:
  • Morten Steengaard Profile Picture
    Morten Steengaard 543 on at
    RE: Create mandatory field

    Hi ZHU,

    Yes, I think your suggestion is the way to do it in Business Central.

    I just think that it would be more nice for the user, if the program as soon as possible told that there is a problem.

    Thank you.

    Best regards,

    Morten

  • Verified answer
    YUN ZHU Profile Picture
    YUN ZHU 59,873 Super User on at
    RE: Create mandatory field

    Sorry I didn't fully understand what you meant.
    Well can you change your mind and prompt an error when the data on this list page is to be used?
    For example, when selecting Customer No. in Sales Order, the prompt must have "VAT Registration No.". This should be a bit simpler than working directly in the list.

    Hope this can give you some hints.

    Thanks.

    ZHU

  • Morten Steengaard Profile Picture
    Morten Steengaard 543 on at
    RE: Create mandatory field

    Hi ZHU,

    Thank you for your reply.

    In the page you link to, I cannot see any description of how to solve my problem.

    OnQueryClosePage would probably work if the page was a card page, but it is a list page, so that does not work.

    And the NotBlank property might also work, but if it is an integer with the value "0", then it is "not blank" and therefore valid according to Business Central. (I think that is an error.) That means that NotBlank can only be used on integers, if the BlankZero property is also set.

    And the ShowMandatory property has nothing to do with validation. It is only UI.

  • YUN ZHU Profile Picture
    YUN ZHU 59,873 Super User on at
    RE: Create mandatory field

    Hi, hope the following helps as well.

    https://yzhums.com/4940/

    Thanks.

    ZHU

  • Morten Steengaard Profile Picture
    Morten Steengaard 543 on at
    RE: Create mandatory field

    That didn't work either :-(

    With this code I can create a record with a value in the field "Code" and no value in my mandatory field.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 20,984 Super User on at
    RE: Create mandatory field

    try with event

    [EventSubscriber(ObjectType::Table, Database::Yourtable, 'OnAfterModifyEvent', '', false, false)]

  • Morten Steengaard Profile Picture
    Morten Steengaard 543 on at
    RE: Create mandatory field

    It is still called at the wrong time.

    When the user enters something in the first field ("Code") and leaves that field, then it should not give an error telling the user that my field has no value. The user has not reached to my field yet.

    The error message must come when the user has had the chance to enter a value in my field, but did not do it.

    I have a feeling, that it is not possible in Business Central.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 20,984 Super User on at
    RE: Create mandatory field

    Do the yellow changes.

    [EventSubscriber(ObjectType::Table, Database::"Shipping Agent", 'OnBeforeInsertEvent', '', true, true)]

      local procedure MyProcedure(var Rec: record "Shipping Agent"; RunTrigger: Boolean)

      begin

          Error('event...');

      end;

  • Morten Steengaard Profile Picture
    Morten Steengaard 543 on at
    RE: Create mandatory field

    Now, I have made this in a codeunit:

       [EventSubscriber(ObjectType::Table, Database::"Shipping Agent", 'OnBeforeInsertEvent', '', true, true)]

       local procedure MyProcedure(var Rec: record "Shipping Agent"; RunTrigger: Boolean)

       begin

           Message('event...');

       end;

    It is called when the user leaves the first field ("Code"). That didn't do the trick.

  • Nitin Verma Profile Picture
    Nitin Verma 20,984 Super User on at
    RE: Create mandatory field

    Ok,

    Try to subscribe the Event of that table "OnBeforeInsert" and then check

    Thanks.

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Sundar…

Industry experts answer burning questions directly from our amazing Community…

Enabling Copilot Case and Conversation…

Agents can easily recap an ongoing chat, transcribe a voice conversation…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,030 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,562 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans