timeConsumed()–a very useful function in Global class in AX 2012 [x++]

Friends,

There is a very useful function timeConsumed() in Global class which we can use to calculate the time taken to execute business logic in AX 2012.

This function will return time consumed string in the form of X hours X minutes X seconds. If X is 0 – will not include the value + text.
It handles up to a 24 hour time difference not dependent on start/end time. If time consumed > 24 hours will only report time over 24 hour intervals.

Below is the example:

static void SGX_timeConsumed(Args _args)

{

    FromTime startTime = timeNow();

    int i;

    str dummyStr;

    ;

   

    for (i = 1 ; i <= 500000; i++)

    {

        dummyStr += int2str(i);     

    }

       

    info(strFmt("Total time consumed is  %1", timeConsumed(startTime, timeNow())));

}

Output:

time consumed 

Happy Dax6ng,

Sreenath Reddy G

Advertisements

SysGlobalObjectCache class in AX 2012 [x++]

Friends,

There is a new class SysGlobalObjectCache that has been introduced in AX 2012, which will help for Global object caching with the help of scope,  Key and values.

Three important parameters are:

Scope: A string type that specifies the scope or the owner of the cached object.

Key :  A container type that specifies the key to the cached object.

value: A container type that has the object to cache.

Let us work with a class example to help you better understand.

For this post, we will create a class by name SR_CurrentWorkerDetails with a simple public method that will get the current worker

class SR_CurrentWorkerDetails

{

}

Create a new public method getCurrentWorker() as shown below.

This method uses scope as “CurrentWorker”, Key as curUserId() and value as “WorkerId” to be cached.

Some important methods:

insert – insert the values in to cache if it’s not already cached/inserted

find – used find method by passing scope and key to get the cached values

remove : sysGlobalObjectCache.remove(scope, key);

In order to clear the cached values based on scope – use

classfactory.globalObjectCache().clear(#CURRENT_WORKER_ID); // scope to be passed as per below example

To clear all caches – we can use

SysGlobalObjectCache::clearAllCaches(); // You can call this method on client/server to clear caches on client or server

Refer to SysFlushAOD class methods to clear caches on client and server:

clearGlobalObjectCaches() – client

clearServerGlobalObjectCaches() – server

public HcmWorkerRecId getCurrentWorker()

{

    // We can easily get current worker from Global::currentWorker() method as well. Below logic is same.

   

    SysGlobalObjectCache    sgoc;

    container               result;

    userId                  currentUserId = curUserId(); // Key

    recId                   workerId;

    HcmWorker               hcmWorker;

    DirPersonUser           dirPersonUser;

   

    // scope

    #define.CURRENT_WORKER_ID("CurrentWorker")

 

    #DEFINE.Values(workerId) // Caching only Worker Id

 

    //Try put pull from cache first

    if (classfactory)

    {

        sgoc = classfactory.globalObjectCache();

    }

    else

    {

        // Workaround for SysQueryRangeUtil usage under IL

        // the class factory is not initialized in the interpreter

        // when called from IL.This is OK as the global cache is a kernel

        // singleton

        sgoc =  new SysGlobalObjectCache();

    }

    result = sgoc.find(#CURRENT_WORKER_ID, [currentUserId]); // use scope and key to find the value cached

    if(result != conNull())

    {

        [#Values] = result;

        return workerId;

       

    }

 

    //Calculate current worker value

    select firstonly RecId from hcmWorker

        join PersonParty, User from dirPersonUser

            where (hcmWorker.Person == dirPersonUser.PersonParty) &&

            (dirPersonUser.User == currentUserId);

 

    //Cache current worker value

    workerId = hcmWorker.RecId;

    sgoc.insert(#CURRENT_WORKER_ID, [currentUserId], [#Values]);

 

    return workerId;

 

}

PriceDisc class uses the same concept – to find the prices and discounts in AX 2012. Refer to priceDisc class for more details.

Happy Dax6ng,

Sreenath

 

One click “DEPLOY TO EP” – AX Client forms using Menu items in Dynamics AX 2012

Friends,

Ax client forms can easily be published to enterprise portal by the new feature from the Menu item >> Deploy to EP.

Let me help with an example:

Create a new List Page by using the template option provided in AX 2012 as shown below and name it as SR_EcoProductListPage as shown below

List page

Change the Name of the newly created list page to SR_EcoProductListPage as shown below.

ECOListPage

Now, let us use the query property on the data sources property to get the linked data sources from the query

Right click on the Data sources Node >> properties >> set the query property to ecoResProductListPage as shown below

DS Query

Next, Go to Design Node >> and give the caption, title data source as shown below.

Design

Next, On the grid set the data source as EcoResProduct as shown below.

grid

Drag and drop some fields on to grid from the Data sources >> EcoResProduct. I have dragged and dropped DisplayProductNumber and ProductType fields on to grid as shown below.

Grid fields

Next, we will add this form to the menu items of type display by dragging and dropping the form to the Menu items >> Display

menu item

Right click on the newly created menu item and go its properties and set the label as “SR Product details” as shown below.

menu item label

Wow, we are good to deploy to EP now. All you have to do is Right click on the newly created menu item >> chose option “Deploy to EP” as shown below

deploy to ep

I am going to add this form to Home\sales as shown below and click on Ok button

sales

You should get an info log that your aspx has been successfully deployed as shown below.

deployed

Now, lets go to Web node in AOT and check whether it has created a URL type web menu item as shown below.

web menu items

Now lets launch this to Sales quick launch as we have selected Home\Sales earlier.

Go to AOT >> Web node, and expand the Web Menus node.

Then expand EPSalesQuickLaunch >> Common

Drag and drop the SR_EcoProductListPage URL below Return orders as shown below.

 

return orders

Open the EP Home page > Administration>> Select the option Refresh AOD as shown below

SAVE TO AOD

Go to Sales Menu/Tab and you will find SR Product details in the quick launch menu as shown below

sr product details

This is just an example of how to deploy to EP forms from AX. Please don’t get in to functionalities or business process as the above form is just a sample example without any functionality.

Happy Dax6ng,

Sreenath

User Relations [Dynamics AX 2009 vs AX 2012, UserExternalParty and DirPersonUser Tables in AX 2012]

Friends,

We all know that how we used to manage user’s relationship to the organization in AX 2009.

Users can be internal, such as employees, or external, such as vendors, customers, or business relations. After you specify user relations, a user’s information (such as employee ID or customer account ID) is automatically displayed in fields when that user opens an Enterprise Portal page. [MSDN]
Below is the screen shot from AX 2009.

r 50

And to find out whether user is linked as Customer/Vendor etc. – we used to use EP class static methods

EP::IsVendor()

Code : SysCompanyUserInfo::current().VendAccount != ”;

EP::IsCustomer()

Code: SysCompanyUserInfo::current().CustAccount != ”

We were using SysCompanyUserInfo table to get the information in AX 5.0

But, in AX 2012 : We make use of UserExternalParty Table to get the relation details. Below is the screen shot of AX 2012 – User relations

image

EP::Isvendor()

Code in AX 2012 : UserExternalParty::existUserEntityType(curUserId(), UserExternalPartyEntityType::Vendor);

EP::IsCustomer()

Code in AX 2012 : UserExternalParty::existUserEntityType(curUserId(), UserExternalPartyEntityType::Customer);

We can find out whether the user is external user by using EP::IsExternalUser() method in AX 2012.

Also we have a table called DirPersonUser which will help to get the details of the custAccount/VendAccount/BusRelAccount etc

AX 2012:

DirPersonUser    dirPersonUser = DirPersonUser::current();

dirPersonUser.custAccount();

dirPersonUser.busRelAccount();

vendor account : VendAccountManager::getSelectedAccount();

AX 2009:

SysCompanyUserInfo      sysCompanyUserInfo = SysCompanyUserInfo::current();

sysCompanyUserInfo.CustAccount;

SysCompanyUserInfo.BusRelAccount;

SysCompanyUserInfo.VendAccount;

You can compare EPQuery::makeMyCustomerSelfServiceQuery method in AX 2009 and AX 2012 for more details.

Happy Dax6ng,

Sreenath Reddy G