RSS

Find record with Common

26 May

When we create a new table we add some useful methods like find and exist so the next developer can use these methods to find the a needed record.

When writing generic code and using Commons instead of specific declared tables, there are multiple ways to find a specific record.

Checking if a find method is present on the table and finding out which parameters it takes is the worst way.

For me the best way is to use KeyData.
KeyData is a container that contains the values of the unique fields to search for.

Let me show you an example:

static void findKeyDataExample1(Args _args)
{
Common record;
Map mapKeyData;
KeyData kd;

mapKeyData = new Map(Types::Integer, Types::String);
mapKeyData.insert(fieldNum(SalesTable, SalesId), ‘SO-00001’);

kd = mapKeyData.pack();

record = SysDictTable::findFromKeyData(tableNum(SalesTable), kd);
}
So this job initializes a new instance of SysDictTable for the table SalesTable and makes the common to be the same type. Then it selects the first record and retrieves the KeyData. This will return a map with the FieldCount and the value. The value is then written to a container and passed to the method SysDictTable::findFromKeyData which returns the same record as we selected first.

This is also working with tables that have more than one field as unique index.
For example PurchAgreementHeader.

To get KeyData from a record:

static void findKeyDataExample2(Args _args)
{
SalesTable salesTable;
Common recordCheck;
SysDictTable dictTable;
Map mapKeyData;
KeyData kd;
MapEnumerator mapEnum;

select firstOnly salesTable;

mapKeyData = SysDictTable::getKeyData(salesTable);

mapEnum = mapKeyData.getEnumerator();
while (mapEnum.moveNext())
{
info(strFmt(‘Field ID: %1 Fieldname: %2 Value: %3’, mapEnum.currentKey(), fieldId2name(tableNum(salesTable), mapEnum.currentKey()), con2Str(mapEnum.currentValue())));
}
}

Advertisements
 
Leave a comment

Posted by on May 26, 2015 in X++

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
All About Dynamics 365

Dynamics 365, D365, Implementor

Syed Rafay Ali

This blog contains information about Functional techniques and guidelines in Microsoft Dynamics AX, including tips, tricks, tutorials, tools and upcoming news enhancement in Microsoft Dynamics Ax

Philippsen's Blog

Everyday findings in my world of .net and related stuff

Microsoft Dynamics AX

A great WordPress.com site

Finite Minds

Adventures in IoT

Dynamics Ax

Technical Knowledge

timsaxblog

A blog about implementing Microsoft Dynamics AX and Dynamics 365 for Operations

Microsoft Dynamics 365 Blog

Strat your organization's digital translation with Dynamics 365, A Blog by Sandeep Chaudhury

DEVSerra - Dynamics AX development blog

Your official Microsoft Dynamics AX blog.

OrganicAX

Discovering Dynamics

AX

A blog by Hai Nguyen

Learn Dynamics Ax with Johnkrish

Live as if you were to die tomorrow. Learn as if you were to live forever - Mahatma Gandhi ****** The more I learn, the less I know - Albert Einstein

Twisted Untwirled

Just another WordPress.com site

ramdynamicsax

Just another WordPress.com site

guyterry's Dynamics AX blog

Just another Dynamics AX blog

%d bloggers like this: