Tuesday 30 December 2014

Job to get Financial dimension Values in AX 2012

static void FinancialDimension(Args _args)
{
    DimensionAttributeValueSet      dimensionAttributeValueSet;
    DimensionAttributeValueSetItem  dimensionAttributeValueSetItem;
    DimensionAttributeValue         dimensionAttributeValue;
    DimensionFinancialTag           dimensionFinancialTag;
    ProjTable                       projTable;
   
    Select projTable  
       join dimensionAttributeValueSet
           where dimensionAttributeValueSet.RecId == projTable.DefaultDimension
           && projTable.ProjId == "000002"
       join dimensionAttributeValueSetItem
           where dimensionAttributeValueSetItem.DimensionAttributeValueSet == dimensionAttributeValueSet.RecId
       join dimensionAttributeValue
           where dimensionAttributeValue.RecId == dimensionAttributeValueSetItem.DimensionAttributeValue
       join dimensionFinancialTag 
           where dimensionFinancialTag.RecId == dimensionAttributeValue.EntityInstance;
          
       info(strFmt("Description%1", dimensionFinancialTag.Description));
}
  

Friday 26 December 2014

Creating Customer from AIF c# in AX 2012 R3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Data;
using System.Data.OleDb;
using CON_ReadCustomer.customer;

namespace CON_ReadCustomer
{
    class Program
    {

        static void Main(string[] args)
        {
            CustomerServiceClient proxy = new CustomerServiceClient();
            proxy.ChannelFactory.Credentials.UserName.UserName = "user@domain";
            proxy.ChannelFactory.Credentials.UserName.Password = "password";

            CallContext context = new CallContext();
            context.Company = "usmf";
            context.LogonAsUser = @"domain\username";
            AxdCustomer customer = new AxdCustomer();

            AxdEntity_CustTable custTable = new AxdEntity_CustTable();
            custTable.AccountNum = "123456";
            custTable.Currency = "USD";
            custTable.CustGroup = "20";
            custTable.OneTimeCustomer = AxdExtType_OneTimeCustomer.Yes;
            custTable.OneTimeCustomerSpecified = true;

            ////Create the party record
            AxdEntity_DirParty_DirPerson party = new AxdEntity_DirParty_DirPerson();
            party.NameAlias = "FirstName";

            ////Set the name fields
            AxdEntity_PersonName personName = new AxdEntity_PersonName();
            personName.FirstName = "FirstName ";
            personName.MiddleName = "MiddleName ";
            //personName.LastName = "LastName ";

            ////Add the names to the party record and set the name sequence
            party.PersonName = new AxdEntity_PersonName[1] { personName };
            party.NameSequence = "FirstLast";

            ////Create a postal address
            AxdEntity_DirPartyPostalAddressView address = new AxdEntity_DirPartyPostalAddressView();
            address.LocationName = "Location Name";
            address.Street = "Street name";
            address.City = "ciyt name";
            address.State = "state name";
            address.CountryRegionId = "country name";
            address.ZipCode = "90211";
            address.Roles = "Address roles";

            ////Create an electronic address
            AxdEntity_DirPartyContactInfoView electronicAddress = new AxdEntity_DirPartyContactInfoView();
            electronicAddress.LocationName = "Contact Email";
            electronicAddress.Type = AxdEnum_LogisticsElectronicAddressMethodType.Email;
            electronicAddress.TypeSpecified = true;
            electronicAddress.Locator = "name@company.com";
            electronicAddress.Roles = "company";

            ////Add the addresses to the party record and the party to the CustTable record
            custTable.DirParty = new AxdEntity_DirParty_DirPartyTable[1] { party };
            custTable.DirParty[0].DirPartyPostalAddressView = new AxdEntity_DirPartyPostalAddressView[1] { address};
            custTable.DirParty[0].DirPartyContactInfoView = new AxdEntity_DirPartyContactInfoView[1] { electronicAddress };

            //Add the CustTable record to the Customer entity
            customer.CustTable = new AxdEntity_CustTable[1] { custTable };

            //Create the customer
            EntityKey[] key = null;
            key = proxy.create(context, customer);

        }

    }
}

Wednesday 17 December 2014

Table collections in AX 2012


Dynamics Ax Table collections & Virtual company
If using more than one company, sometimes, it will be useful to share data from tables with general information, like, tables storing data like zip codes and country codes. The most basic way to share data from a table among all companies is to set the table property SaveDataPerCompany to No.
This will merge data for the table and make the data accessible from all companies. In practice, the kernel will delete the system field dataAreaId for the table.
Another way to sharing data without having to change any settings in the existing tables is by using Table Collections. A table collection is just a template for tables to be shared by any number of companies and Table collections shared using a virtual company.

The form SysDataAreaVirtual is used to define virtual companies. Virtual company is a term used for sharing table collections among a set of companies and does not exist in reality, and therefore, you cannot switch to a virtual company like any normal company.

When using TC for setup tables (like customer groups), setting up a VC will be easy. But, if you are going to share data from main tables (like the inventory table), you should do more investigation as you cannot only share the table InventTable. You must include all tables which are related to InventTable.

Note: Before creating a VC you should export data for the tables used in the table collection, as existing data will be deleted from these tables when added to a virtual company.

Virtual Company setup:

Step 1: Create Table Collection:
Decide which tables you want to share and create a Table collection for these functionally related tables. For example; if you want to share 'Global Address Book' across companies then you can utilize the existing table collection "DirPartyCollection".

To create a table collection, go to AOT\Data Dictionary\Table Collections and on right click select "New Table Collection", then just drag your required tables in this collection.

Step 2: Create Virtual Company, configure/attach normal companies and table collection:
Create a virtual company that will hold the shared data for normal companies.
Note: Before doing the below steps, make sure you are the Ax administrator and the only user online.
1.       Go to Administration > Setup > Virtual company accounts, and create a virtual company.

2.      Decide which companies needs to share data and attach those normal companies with this virtual company.

3.      Attach the table collection with this virtual company and save form.

Your Ax client will re-start and you are done with setting up the virtual company account.

Now, when you have virtual company in place, all new data will be saved in this virtual company. Only companies attached to the virtual company can use this shared data. All other companies which are not attached will work normally, these companies will continue to read/write data as per company bases.

How to move existing data to virtual company?
When you setup a new virtual company, Ax does not move data automatically from normal company to virtual company. This is done by system administrator manually.

There are many ways to do this data move, let’s see only two such approaches here.
Approach 1: Ax Import / Export
This is standard Ax approach.
1.        Manually export existing normal company data from Ax.
2.        Remove duplicate records from this exported data set.
3.        Delete exported data from normal companies.
4.        Import the exported data back in Ax, while logged into one of the participating companies.
5.        Create records deleted in point 2 again in Ax using your logic. How you want to handle duplicate? For example, if you have customer 'Customer' in more than one normal company, what you want to do with this?
Approach 2: Direct SQL
Use this approach if you have good knowledge about SQL queries and Ax table structures/relationships. Below are steps you can follow.
1.        All Ax tables store data as per company unless otherwise specified. For this, Ax uses a special field called DataAreaId. In case of virtual company, it does not matter from which normal company you log-in, it is always the virtual company id which is stored in DataAreaId field of shared tables.
2.        Ax also assigns a unique 64bit number to each record in table. For this, Ax uses a special field called RecId. This RecId is unique in the table and is generated by Ax when you insert a new record in Ax. It is not related to DataAreaId / Company.
3.        For unique records between all participating normal companies, update the DataAreaId to the virtual company id.
For duplicate records, create them again in Ax using some Ax job or Ax import/export technique.

Macros in AX 2012



Microsoft Dynamics Ax Macros
In MorphX macros are not commonly used. A few places make use of macros such as keeping track of the list of fields stored when using dialogs. It is recommended only to use macros to define constants. Also supports code, but is not recommended as reusing code from macros is not flexible.
The main difference between a macro and a method is that a macro has no variable declaration part, and the code in a macro is not validated for errors before executed from a method. This is another reasons for not putting code in macros.

When using macros in your code, the macros must be declared after the variable declaration. And the common place to put the definition is in the ClassDeclaration of a class, form or a report. This will make the macro definition available for all parts of the object.
 
Macro commands: 
For writing macros a set of simple macro commands are used. Below are few examples.

Command
Description
#define
Used to define a constant.

AxExample: See macro HRMConstants.

Syntax: #define.myConstant100('100')

#if.empty

Will return true if the macro has not been called with the parameter validated in the statement.

#if.notempty

Will return true if the macro has been called with the parameter validated in the statement.

AxExample: See macro InventDimJoin

Syntax: #if.notempty(%3)
print %3;
#endif

#endif

Ends a #if.empty or a #if.notempty statement.

AxExample: See macro InventDimJoin

Syntax: #endif (following any if statement)

#globalmacro

No difference whether declaring the start of a macro with #localmacro or #globalmacro. #globalmacro is not used in the standard package, consider using #localmacro instead.

#localmacro

Specify the start of a local macro.

AxExample: See macro BOM

Syntax: #localmacro.BOMDateSelect
(((BOM.fromDate <= %1 || ! BOM.fromDate) && (BOM.toDate >= %1  || ! BOM.toDate))  || ! %1 )
#endmacro

#endmacro

Ends a #LOCALMACRO or a #GLOBALMACRO.

#linenumber

Returns the current line number of the macro. Can be used while debugging, but not of must use.

#macrolib

Used to load an AOT macro from code.

AxExample: See class method BOMHierarchy.searchDownBOM()

Syntax: #macrolib.MyMacro

#undef

Undefine a constant declared with #DEFINE. A defined constant cannot be used if #undef is called with the define name.

Syntax: #define.MyConstant(100)
print #MyConstant;
#undef.MyConstant
print #MyConstant; // will fail, as #MyConstant is not defined.


Defining Constants:Instead of using text in your code it is strongly recommend defining your text as constants. Often you will need an integer or a text for setting a value. If you are going to set RGB color it is not easy to read the following:
            myStringColor(255, 255, 255)
Instead you should consider defining a constant with a descriptive name:
myStringColor(#RBGColorWhite)

Using a macro as a constant rather than entering the value in code makes it easier to maintain. A good way to organize the constants used in your modifications is by creating a macro in the AOT for keeping all your constants in one place, one good example is HRMConstants.

Creating Macros:
Macros are either created directly in the code, or put in an AOT macro and then the AOT macro is declared in the code.

Example(1): #localmacro created in the code
static void Macros_LocalMacro(Args _args)
{
CustTable custTable;
;
#localmacro.selectCustTable  //Macro definition starts here
#ifnot.empty(%1)
while select %1
#ifnot.empty(%3)
order by %3
#endif
{
info(queryValue(%1.%2));
}
#endif
#if.empty(%1)
info("No table specified.");
#endif
#endmacro  //Macro definition ends here
#selectCustTable(CustTable, accountNum)  //Calling Macro with valid parameters
#selectCustTable  //Calling Macro with no parameters – output will be text “No table specified” as per the validation.
}
The macro will select records from a table ‘CustTable’ and print a field from the same as the table to be fetched, and the fields (%1, %2, %3) to be printed are specified in the parameters for the first Macro call.
In the second Macro call, the table has not been specified so text will be printed to the Infolog.
As you cannot declare variables in a macro, integer values prefixed with a percentage sign such as %1 are used instead. This is the common way of using the macros. The validations must be before calling the macro. Notice that you can call a macro without entering the parentheses after the macro name.
 
Example(2): Create and use an AOT Macro
If your macro is going to be used in several places it would make sense creating the macro in the AOT as you will then be able to reuse the macro.
To create a new macro in the AOT:
1.       Unfold the Macro node, right-click and choose New Macro.
2.      A new empty macro node will be created. You can rename the new macro by opening the property sheet to “MyFirstMacro”
3.      Now paste the code of the #localmacro (from above) to your new macro.
4.      Save the Macro, and it is ready to use. 

To use the macro created in the AOT the macro command #macrolib is used. Here the AOT macro is name MyFirstMacro, and it can be used in the code as shown below.
static void Macros_MacroLib(Args _args)
{
CustTable custTable;
;
#macrolib.MyFirstMacro
#selectCustTable(CustTable, accountNum)
#selectCustTable
}
Even this will result in the same output as explained above, the only difference is that we are calling a Macro created in AOT using #macrolib command.

Calculate ledger balance by dimension set in X++ in AX2012/Dynamics 365 FO

There are a variety of ways users can view balances in the general ledger. Some of the most common options are: 1. Trial balance 2. Financia...