Monday, May 22, 2017

X++ new features in D365 F&O

DECLARE ANYWHERE:-  Previously, all local variables had to be placed at the start of the method in which they’re used. Readability is enhanced. You can reduce the risk of reusing a variable inappropriately during long-term maintenance of the code.

Static Constructor and Static field:- Static constructors are guaranteed to run before any static or instance calls are made to the class.

Const/ReadOnly:-  the const is known by IntelliSense, The const is subject to access modifiers, either private, protected, or public. The accessibility of macros is not well understood or even rigorously defined. Consts have scope, while macros do not

Var:-  It’s only possible to use var on declarations where an initialization expressions are provided

Private and Protected member varaibles:- Previously, all member variables defined in a class were invariably protected. It’s now possible to make the visibility of member variables explicit by adding the private, protected, and public keywords.

Extension method:- The extension class must be static, The name of the extension class must end with the ten-character suffix _Extension. However, there’s no restriction on the part of the name that precedes the suffix, Every extension method in the extension class must be declared as public static, the first parameter in every extension method is the type that the extension method extends. However, when the extension method is called, the caller must not pass in anything for the first parameter. Instead, the system automatically passes in the required object for the first parameter. 

Finally in try/catch statements :- Try/catch statements can now include an optional finally clause. The statements in the finally clause are executed when control leaves the try block, either normally or through an exception.

Tuesday, February 21, 2017

How to add methods to table using extension in D365 FO

D365F&O won’t allow user to add new methods to table directly. So here I will be demonstrating how to add new methods to table using extension class.

Lets say our requirement is to add new method to VendTable to get address of Vendors.

First step is to create a class, have a name that has the _Extension suffix and should be static.

public static class VendTable_Extension
{
}

Next Step is to create method, method should always be public static and first parameter should be Table buffer.

public static class VendTable_Extension
{
    public static Notes getVendAddress(VendTable vendTable)
    {
        DirPartyTable       dirPartyTable;
        LogisticsLocation   logisticsLocation;

        select dirPartyTable where dirPartyTable.RecId == vendTable.Party
        join logisticsLocation
        where dirPartyTable.PrimaryAddressLocation == logisticsLocation.RecId;      
       
        return logisticsLocation.postalAddress();
    }

}

Once we will build our project, this method will be usable as if it is method of VendTable.

I have tested the same using job.

class VendTable_Demo
{        
    /// <summary>
    /// job to test vendtable extension method
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {    
        VendTable vendTable;

        vendTable = VendTable::find("1001"); 
        
        info(strFmt("Vendor Address - %1",vendTable.getVendAddress()));
      
    }



Output:-


THANK YOU
















Sunday, February 19, 2017

How to get current user information using x++ in D365 F&O | AX 2012

The code below will help out in fetching current user Information in D365 FO using X++.

class getCurrentUserInfo
{      
    /// <summary>
    /// this method used to get current user detail
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {  
        info(strfmt("Current User - %1",curUserId()));    
        info(strfmt("User Account Type - %1",xUserInfo::find(false,curUserId()).accountType));  
    }

}

Output will look something like this:






THANK YOU