Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Prevent duplicate record

(0) ShareShare
ReportReport
Posted on by 40

Greetings all,

I want to add a new method in my table to prevent the same value in the column from showing up. what method should i use?

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Prevent duplicate record

    Since it's your own code, just add code that checks whether a duplicate record already exists in the table. If it does, don't insert a new row.

    Please check this doc page to learn how to select (find) records in a table: hdocs.microsoft.com/.../select-statements

  • fireblaster Profile Picture
    fireblaster 40 on at
    RE: Prevent duplicate record

    Hi Nikolaos

    Sorry, when I said "filter out", it means that the table keeps inserting the same ID (which is the unique index I just did on the table that I want).

    so basically this data is created from importing .csv files, this import method is placed in a class. there's also a form and a button in it to import the files. the form and data source(same table) has 2 columns which one of is ID, so when I successfully imported the .csv file, the table or form appear to have duplicated records that have the same ID

    and quoting for the "it keeps inserting the same value of ID", it means that the .csv file contains various records with the same ID

    (eg: location    ID

          address     1

          city            1

    ,I just want to filter the records to select only one value of ID (doesn't matter which record because i have a second form to show all the record) to appear on my table

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Prevent duplicate record

    Hi fireblaster,

    what do you mean "filter out"? Earlier you wrote "prevent the same value in the column from showing up".

    You also wrote "it keep inserting the same value of ID"

    I think it would be good if you could share a bit more details about your scenario. What's "it"? How is this data created and where?

    Unique indexes are the standard way to prevent duplicates - not only in AX but in databases in general.

    I warmly recommend that you stick with the logic of the standard application, this way it's easy for the users to understand how things work. Users can try to insert duplicate records, and they will see an error if they try to do so.

    It's up to the user (or your code, depending how the data is created) to handle the error or check for duplicates. The target table's responsibility is to prevent duplicates from being inserted, by throwing an error.

  • fireblaster Profile Picture
    fireblaster 40 on at
    RE: Prevent duplicate record

    the ID that i created have a string value, so when i inserted into my table, it will filter out the same ID if existed

  • greengrimms Profile Picture
    greengrimms 1,400 on at
    RE: Prevent duplicate record

    Right, so the index itself won't generate a new Id for you, it'll only prevent duplicates from being inserted in the database by throwing an exception.

    What's your specific requirement? Remember you already have RecId for unique records, is this Id you created a numeric value that you want to auto increment just to show to the user? Is it a natural key?

  • greengrimms Profile Picture
    greengrimms 1,400 on at
    RE: Prevent duplicate record
    [deleted]
  • fireblaster Profile Picture
    fireblaster 40 on at
    RE: Prevent duplicate record

    I already done it but, it keep inserting the same value of ID

  • Suggested answer
    greengrimms Profile Picture
    greengrimms 1,400 on at
    RE: Prevent duplicate record

    Hi,

    That can be achieved by adding an index to the table. Just add the field/s that make up the key, and set the property Allow duplicates to No for that key.

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

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans