Skip to main content

Notifications

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

Relation between two fields is not working when inserting records via x++

(0) ShareShare
ReportReport
Posted on by 1,309
Hi,

I have a parent table (TableX) with mandatory (Field1) which is also an index with allow duplicate No.
This Field1 has an EDT, which has TableRelations and TableReferences Nodes filled for it.

Now I created a new table(TableY) which has Field1(same Edt). Field1 in this table is not unique and not mandatory. It also has ignoreEdtRelation = true.
I also created a relation between these two tables on TableY based on Field1.

When I open the form for TableY, and try to fill Field1 with a value that doesn't exist in TableX, it doesn't allow me. it only allows empty value or an existing value from TableX -- which is the expected behaviour because of the relation.

However, If i try to insert a record for TableY by code, where I fill Field1 with a value that doesn't exist in TableX -- it allows me to do so... why is that? how can i prevent that and make it work like it works from the UI?
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    Relation between two fields is not working when inserting records via x++
    The system doesn't automatically make extra SQL statements to check the validity on saving. If the values come from an unreliable source, your code normally should validate them before using them.
     
    When you already have in your table buffer, you can validate them with validateField(), which checks the related table too. That's the method that forms call when you modify a value of a field (even before saving the record).
     
    If you wish, you call validateField() from validateWrite() as well.
  • Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,587 Super User 2024 Season 1 on at
    Relation between two fields is not working when inserting records via x++
    Ideally if you call validatewrite() then if there is some relation, it should give error even without giving code in validation if it is defined at table level. If it is not working means have you written code like skipdatamethods(true); something like this? Can you send your code?
  • .. Profile Picture
    .. 1,309 on at
    Relation between two fields is not working when inserting records via x++
    Hi Martin,

    I'm calling validateWrite before adding to the recordInsertList  and it's not working--- but do you mean i need to put this validation inside the validateWrite or should it work by itself?
  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    Relation between two fields is not working when inserting records via x++
    Didn't you forget to call validateWrite() method and check its result before inserting the (potentially invalid) record?

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans