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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Find if a record from the current table exists in other

(0) ShareShare
ReportReport
Posted on by 5

Hello! I'm still finding my way around d365, and am quite new. I have a scenario where i have a record (Primary key)  in the current table and i need to find out whether that specific value for the primary key field exists in a related table. For example i have a product record id of 5, and need to find out whether the product record id of 5 exists in the other table. Easiest way to do this? Thank you very much!

I have the same question (0)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    You can use select statements to check if a specific record exists in a table. For example:

    select RecId from myTable
        where myTable.Product = 5;
        
    if (myTable.RecId)
    {
        // do something
    }

    Note that the table may have helper methods wrapping such a select statement, therefore you can simply call the method instead of writing the query every time. And if it's your own table, you may want to write such a method.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi Jaymes,

    you'll need to write a simple select statement. The syntax of the x select statement is described here: https://docs.microsoft.com/en-us/dynamicsax-2012/developer/select-statement-syntax

    MyTable myTable;
    
    select firstonly RecId from myTable 
        where myTable.MyField == myvalue;
        
    if (myTable.RecId)
    {
        info(strFmt("Value %1 was found in the table.", myvalue));
    }
    else
    {
        info(strFmt("Value %1 was not found in the table.", myvalue));
    }

  • ergun sahin Profile Picture
    8,826 Moderator on at

    If your goal is to check at the time of deletion, it makes more sense to add deleteAction instead of writing code.

    If you are only going to check if it exists, I recommend the 1st one of the following codes.
    If you are going to use fields from two tables and there is a one-to-one relationship, I recommend the second one. If you are going to use fields in the case of a one to n relationship, you need to loop with while(3.).
    Finally, you don't need join if you have the first table record, directly select the second table(4.).

    MyTable         myTable;
    MyRelatedTable  myRelatedTable;
    
    ---------1-------------------
    select firstOnly RecId from myTable
    where myTable.RecId == 1234456778
    exists join myRelatedTable
    where myRelatedTable.ProductId == myTable.ProductId;
    
    ---------2-------------------
    select firstOnly myRelatedTable
     join myTable
    where myTable.RecId     == 1234456778
       && myTable.ProductId == myRelatedTable.ProductId;
    
    ---------3-------------------
    while select myRelatedTable
     join myTable
    where myTable.RecId     == 1234456778
       && myTable.ProductId == myRelatedTable.ProductId
    {
    }
    
    ---------4-------------------
    myTable = MyTable::find(1234456778); 
    select firstOnly RecId from myRelatedTable
    where myRelatedTable.ProductId == myTable.ProductId;//or this.ProductId

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans