web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

Making a field Unique

(0) ShareShare
ReportReport
Posted on by 700

Hi All,

In Employee Card, I have a field named 'Identification No'. How to make this field a unique one, so that when a user enters repeated value, an error should be thrown.

Please help !

Thanks !

Categories:
I have the same question (0)
  • Suggested answer
    Avinash B Profile Picture
    1,455 on at

    Hi

    Add field level validation to check the uniqueness.

    Regards,

    Avinash B

  • Dynamics 365 Business Central Profile Picture
    700 on at

    Can you please simplify your answer.

  • Verified answer
    Avinash B Profile Picture
    1,455 on at

    On the OnValidate trigger of the field, filter the records with the value entered. If found then throw error.

    In your case, OnValidate trigger of "Identification No." you can write the following code.

    trigger OnValidate()

    {

       Var

           Employee: Record Employee

       begin

           Employee.Setrange("Identification No.", Identification No.");

           Employee.SetFilter("No.",'<>%1',"No.");

           If Employee.FindFirst() then

               Error('The Identification No. must be unique');

       end

    }

    This might help.

    Regards,

    Avinash B

  • aacnsilva Profile Picture
    5 on at
    [quote user="Avinash B"]

    On the OnValidate trigger of the field, filter the records with the value entered. If found then throw error.

    In your case, OnValidate trigger of "Identification No." you can write the following code.

    trigger OnValidate()

    {

       Var

           Employee: Record Employee

       begin

           Employee.Setrange("Identification No.", Identification No.");

           Employee.SetFilter("No.",'<>%1',"No.");

           If Employee.FindFirst() then

               Error('The Identification No. must be unique');

       end

    }

    This might help.

    Regards,

    Avinash B

    [/quote]

    The code above works, but i suggest you to optimize it. Instead of using FindFirst(), make the if statement like this: if not Employee.IsEmpty() then

  • Dynamics 365 Business Central Profile Picture
    700 on at

    Thank you so much ! It worked :-)

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

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans