Book Image

Mastering Microsoft Dynamics AX 2012 R3 Programming

By : Simon Buxton, Mat Fergusson
Book Image

Mastering Microsoft Dynamics AX 2012 R3 Programming

By: Simon Buxton, Mat Fergusson

Overview of this book

<p>Due to its interesting capabilities such as finance and supply chain management, business intelligence and reporting, project management, and so on, Microsoft Dynamics is one of the most widely used solutions adopted by many organizations across the globe. This book is a step-by-step tutorial that covers elements from the initial design through to development. Each concept is fully explained and demonstrated by the creation of a real-world project. You will learn some really useful, advanced development techniques such as extending your code with metadata and exception handling.</p> <p>This book is an invaluable tutorial if you are moving from another ERP system or language, or if you are a technical consultant with a desire to create efficient functional designs and business solutions.</p>
Table of Contents (21 chapters)
Mastering Microsoft Dynamics AX 2012 R3 Programming
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Code snippet – the find method


In this example, we'll create a simple find method using information about the currently selected AOT node.

When the script methods are called, they are created with an instance of an Editor class. This class contains, among other information, the tree node of the method we are editing. Like this, for example:

\Data Dictionary\Tables\ConFMSVehServiceTable\Methods\method1

The following code uses this to get the table name. Again, add this to the EditorScripts class:

public void contoso_method_find(Editor editor)
{
    str             path = editor.path();
    TreeNode        treeNode;
    TableName       tableName;
    str             prefix = strRep(" ", 4);
    #TreeNodeSysNodeType

    treeNode = path ? TreeNode::findNode(path) : null;

    if (treeNode)
    {
        treeNode = treeNode.AOTparent();
        if(treeNode)
            treeNode = treeNode.AOTparent();
        if (!treeNode)
            return;
        if (treeNode.treeNodeType().id() == #NT_DBTABLE)
        {
            tableName = treeNode.treeNodeName();
        }
    }
    if(tableName == "")
        return;

    editor.insertLines(
        strFmt("public static %1 find(%2 _id, boolean _forUpdate = false)\n", 
                tableName, "RefRecId"));
    editor.insertLines("{\n");
    editor.insertLines(prefix + strFmt("%1 table;\n\n", 
                                       tableName));
    editor.insertLines(prefix +
                       "table.selectForUpdate(_forUpdate);\n");
    editor.insertLines(prefix + 
                   strFmt("select table where table.%2 == _id;\n",
                             tableName, "RecId"));
    editor.insertLines(prefix + "return table;\n");
    editor.insertLines("}");
}

We can extend this further by interrogating the table's metadata, using DictTable to find the keys in the primary key.

Finally, you might wonder how the scripts work where you just type the script name. We can do this using the following steps:

  1. Rename the method to template_method_find.

  2. On the ConFMSVehicleTable table, create a new method.

  3. Delete the content of the method, type find, and press Tab. We'll see that it creates a find method for us.

We have broken our naming convention by dropping the prefix, and doing this can cause some issue when you perform an upgrade. And, in this particular case, it could also be affected by cumulative updates or service packs.