Thursday, May 24, 2012

DAX 2012 Data Model - Global Address Information

In 2012 address table is obsolete (no longer used) and new tables have been  introduced which follow normalization and avoid storing of redundant data.

So this post basically
       - Describes about new data model
       - X++ job to illustrate how address and contact information can be migrated to AX 2012.

- New Data Model

-- X++ Job

 Create CSV File from Dynamics AX 2009 as Customer.csv and place it on Desktop,
           Format :
           [CustAccount, Name, Address, ZipCode, State, County, Email, MobileNo]


static void Demo_CustomerAddrImport(Args _args)
{
    #WinAPI
    CustTable                   custTable;
    DirPartyTable               dirPartyTable;
    LogisticsAddressZipCode     zipCode;
    LogisticsLocation           logisticsLocation;
    DirPartyLocation            partyLocation;
    LogisticsPostalAddress      postalAddress;
    LogisticsElectronicAddress  contactTable;
    DirPartyLocationRole        partyLocationRole;
    LogisticsLocationRole       locationRole = LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Invoice);
    container                   roleId = [locationRole.RecId];
    LogisticsPostalAddressView  postalAddrView;


    CommaIO        commaIO = new CommaIO(strfmt("%1%2", WinAPI::getFolderPath(#CSIDL_DESKTOPDIRECTORY), "\\Customer.csv"), "r");
    Container      record;
    Name           name;


    void createPostalAddr(LogisticsLocationRecId _recId, LogisticsPostalAddressView _postalAddrView)
    {
        LogisticsPostalAddressEntity postalAddrEntity = LogisticsPostalAddressEntity::construct(_recId);
        ;


        postalAddrEntity.createPostalAddress(_postalAddrView);


    }
    ;




    while (commaIO.status() == IO_Status::Ok)
    {
        record = commaIO.read();
        name = conPeek(record, 2);


        custTable = custTable::find(conPeek(record, 1));
        if (custTable)
        {


            // Create postal address
            logisticsLocation.clear();
            logisticsLocation = LogisticsLocation::create("Legacy Postal Address", NoYes::Yes);


            postalAddress.Location = logisticsLocation.RecId;
            postalAddress.Address  = conPeek(record, 3);
            postalAddress.ZipCode  = conPeek(record, 4);
            postalAddress.State    = conPeek(record, 5);
            postalAddress.County   = conPeek(record, 6);
            postalAddress.CountryRegionId = "DNK";
            postalAddress.ValidFrom = DateTimeUtil::newDateTime(01\01\2010, 01);
            postalAddress.insert();


            // Attach to party and sepecify address type like Delivery, Invoice
            partyLocation.clear();
            partyLocation.initValue();
            partyLocation.Party = custTable.Party;
            partyLocation.Location = logisticsLocation.RecId;
            partyLocation.IsPostalAddress = NoYes::Yes;
            partyLocation.insert();




            DirPartyLocationRole::createPartyLocationRoles(partyLocation.RecId, roleId, false);


            if (conPeek(record, 7) != "")
            {
                // Create postal address
                logisticsLocation.clear();
                logisticsLocation = LogisticsLocation::create("Email Contact", NoYes::Yes);


                contactTable.clear();
                contactTable.Location = logisticsLocation.RecId;
                contactTable.Locator  = conPeek(record, 7);
                contactTable.Type     = LogisticsElectronicAddressMethodType::Email;
                contactTable.insert();


                // Attach to party
                partyLocation.clear();
                partyLocation.initValue();
                partyLocation.Party = custTable.Party;
                partyLocation.Location = logisticsLocation.RecId;
                partyLocation.insert();
            }


            if (conPeek(record, 8) != "")
            {
                // Create postal address
                logisticsLocation.clear();
                logisticsLocation = LogisticsLocation::create("Phone Contact", NoYes::Yes);


                contactTable.clear();
                contactTable.Location = logisticsLocation.RecId;
                contactTable.Locator  = conPeek(record, 8);
                contactTable.Type     = LogisticsElectronicAddressMethodType::Phone;
                contactTable.insert();


                // Attach to party
                partyLocation.clear();
                partyLocation.initValue();
                partyLocation.Party = custTable.Party;
                partyLocation.Location = logisticsLocation.RecId;
                partyLocation.insert();
            }
       
        }
    }
}

Dynamics AX 2012 - Workflow Development in 2 hrs

You all might be aware/heard of new changes related to worklow architecture and development.
So this post basically addresses,

1. Workflow Architecture changes
2. Workflow Development  wizards
3. Generic class for submit/resubmit events on all workflows in AX.


1. Workflow Architecture changes

In 2012, AOS service has gone tremendous changes like

- Now you can directly host WCF services on AOS.
- Earlier version we were using IIS to interact with WWF(Windows workflow foundation) and      
           WCF (Windows communication foundation). 
- Because of above change, now no need to install workflow component separately
- Just run the workflow configuration wizard (available in System administration->Setup)
- Workflow templates/configuration were made available across companies/per company level.
- Workflow template is renamed to Workflow types

2. Workflow Development through wizards.

Let us develop workflow for
        'Customer approval- New created customer should be approved through workflow'

Step 1:
          Create a query for custTable say 'CustApprWorkflowQry' in AOT->Queries

Step 2:
          Duplicate enum 'PurchReqWorkflowState' and rename it to 'CustApprWorkflowState' and drag it to CustTable Table.

Step 3:
          Override 'canSubmitToWorkflow' method on Table->CustTable and paste below code.

//BP Deviation Documented
                public boolean canSubmitToWorkflow(str _workflowType = '')
                {
                    boolean ret;

                    ret = this.RecId != 0 && this.CustApprWorkflowState ==
                    CustApprWorkflowState::NotSubmitted;

                    return ret;
                 }


Create new method on CustTable as below to update worklow state during its life cycle.

                //BP Deviation documented
                public static void UpdateCustWorkflowState(RefRecId _recId, CustApprWorkflowState _state)
                {
                    CustTable custTable = CustTable::findRecId(_recId, true);

         ttsBegin;

         custTable.CustApprWorkflowState = _state;
         custTable.update();

         ttsCommit;
}


Step 4:
            Create workflow type (template/configuration) through wizard available in 2012.

Run wizard as below:

AOT->Workflow-> Workflow types  (Right click ->Add-Ins-> workflow type wizard...

Specify values as shown in below image


Click on next, as shown in below image wizard will create workflow eventhandlers class and menu items for you.

Paste below code to update workflow state.

//BP Deviation Documented
public void started(WorkflowEventArgs _workflowEventArgs)
{
    CustTable::UpdateCustWorkflowState(_workflowEventArgs.parmWorkflowContext().parmRecId(), CustApprWorkflowState::Submitted);
}

//BP Deviation Documented
public void completed(WorkflowEventArgs _workflowEventArgs)
{
    CustTable::UpdateCustWorkflowState(_workflowEventArgs.parmWorkflowContext().parmRecId(), CustApprWorkflowState::Completed);
}

//BP Deviation Documented
public void canceled(WorkflowEventArgs _workflowEventArgs)
{
CustTable::UpdateCustWorkflowState(_workflowEventArgs.parmWorkflowContext().parmRecId(),   CustApprWorkflowState::PendingCancellation);
}


Step 5:
           Create workflow approval through wizard available in 2012.

Run wizard as below:

AOT->Workflow-> Approvals  (Right click ->Add-Ins-> Approval wizard...

Specify values as shown in below image

Click on next, as shown in below image wizard will create workflow element eventhandlers and menu items for you.

Paste below code to update workflow state.

//BP Deviation Documented
public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{
    CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),   CustApprWorkflowState::Submitted);
}

//BP Deviation Documented
public void returned(WorkflowElementEventArgs _workflowElementEventArgs)
{
    CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),  CustApprWorkflowState::Returned);
}

//BP Deviation Documented
public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
         CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),  CustApprWorkflowState::ChangeRequest);
}

//BP Deviation Documented
public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
    CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(), CustApprWorkflowState::Approved);
}

//BP Deviation Documented
public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{
         CustTable::UpdateCustWorkflowState(_workflowElementEventArgs.parmWorkflowContext().parmRecId(),  CustApprWorkflowState::PendingCancellation);
}

Step 6:
          Enable workflow on CustTableListPage form and alos drag field CustApprWorkflowstate on form overview grid, as below

          CustTableListPage->Design, set properties as below

Step 7:
          Download below class to activate workflow on submit and resubmit.

          This is generic class and with little modification can be used for all different worklow's in AX.
          Download Class XPO :
         
         Once you download and import class,  change below menu-items associated with submit and resubmit actions. 



Step 8:
          Run Incremental CIL generation, AOT->Right click-> Incremental CIL generation

You are done with development and can configure workflow in AR->Setup
After activating above workflow configuration test it by creating new customer in CustTableListPage form.

Microsoft Dynamics AX 2012: Client Performance Options


Microsoft Dynamics AX 2012 includes many UX enhancements, such as list pages, ribbons, fact boxes, preview panes, fast tabs, etc. These UX enhancements can help present relevant information at one place and reduce form switches. However, they come with certain performance characteristics. In this post, we’ll explain some of the issues and how to fix them with Client Performance Options.

Potential issues with Factboxes and Preview Panes

In the following figure, you can see the layout of a typical AX2012 list page, e.g. the Customer list page. In the center of the screen is the customer list itself. On the right side of it you can find a series of Factboxes, which present various information about the highlighted customer, such as its address, recent activities, statistics, etc. On the bottom of the list you can find the Preview Pane, which shows some detail information about the customer.

image

These information can be very helpful if you need to quickly find out some facts about the customer without opening the detail form. However, from a performance point of view, it has the following potential issues:
  • Each factbox and the preview pane is a separate form, so you will incur cost of form initialization (FormInit) and rendering (FormRun) for each and every one of them;
  • Factboxes and preview pane are rendered sequentially one by one;
  • If a factbox is expanded and it includes a  SQL query, such as the 3 factboxes shown in the above picture, each will send the query to AOS and SQL and increase load on the servers. In fact, some of the factbox queries include summary information and are quite expensive from SQL perspective.

 

Client Performance Options

To mitigate these performance issues, there are a few options.
For partners/developers, you can change the form and remove unnecessary factboxes. You should also make sure the query for the factboxes performs well, especially when there are a lot of data and a lot of users running the form.
For users, you can collapse less frequently used factboxes using the ^ symbol on the top right corner of the factbox. When you collapse a factbox, it no longer executes its query, thus reducing impact to SQL server. However, FormInit is still called. You can also Hide the factbox using the rectangle symbol on the left of the ^ symbol. When you hide a factbox, no FormInit or query cost will be incurred. This is a per user and per form setting, i.e. your setting only affect you and only affect this form.
For System Admin, if you want to set up performance settings system wide, open the Client Performance Options form from System Administration\Setup\System:
image
There are 4 options here. You can uncheck the Factboxes and Preview Pane Enabled options to remove them from the entire system. You’ll need to restart the client to pick up the change. After removing factboxes and preview pane, the CustListPage look like the following. You get more screen real estate for the list and it loads faster.

image

There’s also a Timeout setting for Factboxes. If you set it to a non-zero value, it will limit the time SQL server spend on the factbox queries. If SQL server can not finish the query within the timeout limit (e.g. 1 second as shown on my system), the query is cancelled. This will limit the load on SQL server and the response time on the form UI.

Enable/Disable Fact boxes, Preview Panes in AX2012 UX

In Microsoft Dynamics AX 2012, if you want to disable factboxes/preview panes across application then you can do so.
It's not recommended to do this activity but this will help in troubleshooting performance issues assoicated with opening forms in ax2012 by controlling 'Timeout' parameter as below.

Go to ->System Administration->Setup->Client performance options 

Visit  Ax2012-client-performance-options for detailed explaination by Ax performance team.

Dynamics AX 2012 - X++ code to create General Journal

static void Demo_CreateGLJournal(Args _args)
{
    AxLedgerJournalTable journalTable;
    AxLedgerJournalTrans journalTrans;
    container            accEntryPattern;
    container            offSetEntryPattern;
    ;

    journalTable = new AxLedgerJournalTable();
    journalTrans = new AxLedgerJournalTrans();

    //Journal Name
    journalTable.parmJournalName("GenJrn");
    journalTable.save();

    journalTrans.parmJournalNum(journalTable.ledgerJournalTable().JournalNum);
    journalTrans.parmTransDate(systemDateGet());
    journalTrans.parmAccountType(LedgerJournalACType::Ledger);

    //LedgerDimension  => Ledgeraccount, DAX 2009
    accEntryPattern = ["601501", "601501", 0]; 
    journalTrans.parmLedgerDimension(AxdDimensionUtil::getLedgerAccountId(accEntryPattern));

    journalTrans.parmAmountCurDebit(2000);

    //OffsetLedgerDimension => OffsetLedgerAccount,  DAX 2009
    offSetEntryPattern = ["401100", "401100", 0];
    journalTrans.parmOffsetAccountType(LedgerJournalACType:: Ledger );
    journalTrans.parmOffsetLedgerDimension(AxdDimensionUtil::getLedgerAccountId( offSetEntryPattern));
  
    journalTrans.save();

    info(strFmt("Journal %1 created", journalTable.ledgerJournalTable().JournalNum));
}

How to post with Financial dimensions:

Format : 
["DisplayValue", "MainAccount", NumberOfDimensions, "DimensionName", "DimensionValue"]; 

E.g:
accEntryPattern = ["601501-Test", "601501", 1, "Department", "OU_1"]

AX 2012 - Editor tips and tricks for fast development/coding

Long back i seen video on new improvements made to existing X++ Editor. Lot of features have been introduced to get look and feel of visual studio editor.
Among all, I am very much impressed with 'Inserting Code Snippets' feature.
           - During coding in editor, if you want to create main method
           - You will make use of template and select main method or construct
           - But in 2012, just type main and click tab will insert main method prototype.

So thought of making video for same, watch it


Really you will enjoy this new feature and finish development much faster compare to 2009.


How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...