24 December 2016

Understanding Objects and Sample Code Snippet Extensions for New Development Environment

Hi All,

In my last post I discussed on how to setup Azure 30 days free trial subscription and install a New VM for trying out New Development Environment. Below is the link for the same post.

https://nandeshgowda-navblog.blogspot.in/2016/12/developing-extensions-using-new.html


Understanding Objects in Dynamics NAV

All functionality in Dynamics NAV is coded in objects. Table objects define the table schema that holds data, page objects represent the pages seen in the user interface and codeunits contain code for logical calculations and for the application behavior. These objects are stored as code, known as AL code, and are saved in files with the .al file extension.

Note
A single .al file may contain multiple objects.

For Table Object please refer below MSDN Link
https://msdn.microsoft.com/en-us/dynamics-nav/newdev-table-object

For Page Object please refer below MSDN Link
https://msdn.microsoft.com/en-us/dynamics-nav/newdev-page-object

For Codeunit Object please refer below MSDN Link
https://msdn.microsoft.com/en-us/dynamics-nav/newdev-codeunit-object

There are two other special objects which are specifically used for building extensions. Table extension objects and page extension objects are used for defining additive or overriding changes to table or page objects.

For example, an extension for managing a business that sells organic food may define a table extension object for the Item table that contains an additional field Organic and Produced Locally. The Organic and Produced Locally fields aren't usually present in the Item table, but through the table extension these data fields will now be available to store data in and to access from code.

You can then use the page extension object to display the fields that you added to the table object.

Note :
Table and page extension objects can have a name with a maximum length of 30 characters.


1) Table Extension Object :

The table extension object allows you to add additional fields or to change some properties on a table provided by the Dynamics NAV service. In this way, you can add data to the same table and treat it as a single table.

For example, you may wish to create a table extension for a retail winter sports store. In your solution you want to have ShoeSize as an additional field on the customer table. Adding this as an extension allows you to write code for the customer record and also include values for the ShoeSize.

Along with defining other fields, the table extension is where you write trigger code for your additional fields.

Snippet support :
Typing the shortcut ttableext will create the basic layout for a table extension object when using the AL Extension in Visual Studio Code.

Properties:
Using a table extension allows you to overwrite some properties on fields in the base table. The following properties can be changed.

  • OptionCaptionML
  • ClosingDates
  • Description
  • Width

Table Extension example :
This table extension object extends the Customer table object by adding a field ShoeSize, with ID 70000900 and the data type Integer. It also contains a procedure to check if the ShoeSize field is filled in.
tableextension 70000020 RetailWinterSportsStore extends Customer
{
    fields
    {
        field(70000900;ShoeSize;Integer) 
        {
            trigger OnValidate();
            begin
                if (rec.ShoeSize < 0) then
                begin
                   message('Shoe size not valid: %1', rec.ShoeSize);
                end;                    
            end;
        }
    }

    procedure HasShoeSize() : Boolean;
    begin
        exit(ShoeSize <> 0);
    end;

    trigger OnBeforeInsert();
    begin
        if not HasShoeSize then
            ShoeSize := Random(42);
    end;
}

2) Page Extension Object

The page extension object extends a Dynamics NAV page object and adds or overrides the functionality.

The structure of a page is hierarchical and breaks down into three sections. The first block contains metadata for the overall page; the type of the page and the source table it is showing data from. The next section; the layout, describes the visual parts on the page. The final section details the actions that are published on the page.

In the layout section, you can use the following functions to place page fields and groups on the page. Similarly, in the actions section, you use these functions to place actions in the ribbon.

Function exampleApplies to...
addfirst(General)Groups only
addlast(General)Groups only
addafter(AddressDetails; "Post Code")Fields and groups
addbefore(AddressDetails; "Post Code")Fields and groups
movefirst(General)Groups only
movelast(General)Groups only
moveafter(AddressDetails; "Post Code")Fields and groups
movebefore(AddressDetails; "Post Code")Fields and groups

If you want to modify existing fields and groups on a page, you use the modify() function. See the code example below for syntax.

Snippet support :
Typing the shortcut tpageext will create the basic layout for a table object when using the AL Extension in Visual Studio Code.

Page Extension example :
The following page extension object extends the Customer Card page object by adding a field control ShoeSize to the General group on the page. The field control is added as the last control in the group using the addlast function. In the actions area, you can see what the syntax looks like for actions that execute triggers and actions that run objects.

pageextension 70000020 CustomerCardExtension extends "Customer Card"
{
    layout
    {
        addlast(General)
        {
            field("Shoe Size"; ShoeSize)
            {
                CaptionML = ENU='ShoeSize';

                trigger OnValidate();
                begin
                    if ShoeSize < 10 then
                        Error('Feet too small');
                end;
            }
        }

        modify("Address 2")
        {
            CaptionML = ENU='New Address 2';
        }
    }

    actions
    {
        addlast(Creation)
        {
            group(MyActionGroup)
            {
                Action(MyAction1)
                {
                    CaptionML = ENU='Hello!';

                    trigger OnAction();
                    begin
                        Message('My message');
                    end;
                }

                Action(MyAction2)
                {
                    RunObject = codeunit "Activities Mgt.";
                }
            }
        }
    }

   var
        Msg : TextConst ENU='Hello from my method';

    trigger OnOpenPage();
    begin
        Message(Msg);
    end;
}



Thanks & Regards,
Nandesh Gowda

No comments:

Post a Comment