Notifications
Announcements
No record found.
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!
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.
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)); }
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Martin Dráb 660 Most Valuable Professional
André Arnaud de Cal... 549 Super User 2025 Season 2
Sohaib Cheema 307 User Group Leader