This code put five records of the table CustTable in a map with the key AccountNum. Then the map is looped and the and AccountNum and customer Name is printed to the infolog.
The last part of the code finds the last AccountNum from from the loop directly from the Map, but after a check is done if the value is in the map. Performing a lookup for a value that doesn´t exist in map will result in a error.
One important thing to remember here, you should always use a unique key to the map, otherwise you will not be able to find specific records in a fast and orderly manner. In this case I use AccontNum which is a unique identifier for the CustTable table. If you don´t have a unique key, you can use the RecId field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | static void EP_Maps(Args _args) { Map foundInvoiceMap = new Map( Types::String, Types::Record); MapEnumerator enumerator; CustTable custTable; CustAccount account; int i = 1; ; while select custTable { i++; foundInvoiceMap.insert( custTable.AccountNum, custTable); if(i > 5) break; } enumerator = foundInvoiceMap.getEnumerator(); while(enumerator.moveNext()) { account = enumerator.currentKey(); custTable = enumerator.currentValue(); info(account + " " + custTable.Name); } custTable = null; if(foundInvoiceMap.exists(account)) { custTable = foundInvoiceMap.lookup(account); info(custTable.AccountNum + " " + custTable.Name); } } |
Last 5 posts in Dynamics AX
- How to call a web API from Dynamics 365 FO X++ - December 1st, 2020
- Cannot add or remove static keyword for this method - May 30th, 2016
- Sales line number not unique - May 27th, 2016
- DMF - DMF Service is unavailable - November 7th, 2012
- DMF - Error occured while doing bulkcopy from temp table to entity table - November 6th, 2012