Alfasith AX

Alfasith AX
اللَّهُمَّ انْفَعْنِي بِمَا عَلَّمْتَنِي، وَعَلِّمْنِي مَا يَنْفَعُنِي، وَزِدْنِي عِلْمًا

Tuesday, July 23, 2013

Change company in Dynamics AX 2009

Hi, 

In Microsoft Dynamics AX Axapta, sometimes, we can to insert/update/delete records in different companies or insert/update/delete according to the company we are... 

In Dynamics AX, exists the "changeCompany" function, that allow us to do that easily.

Here's an example:



static void main()
{
    CustTable custTable;
    ;

    //Assume that you are running in company 'aaa'.
    changeCompany('bbb') //Default company is now 'bbb'.
    {
        custTable = null;
        while select custTable
        {
            //custTable is now selected in company 'bbb'.
        }
    }


    //Default company is again set back to 'aaa'.

    changeCompany('ccc') //Default company is now 'ccc'.
    {  
        //Clear custTable to let the select work
        //on the new default company.
        custTable = null;
    

        while select custTable
        {
            //custTable is now selected in company 'ccc'.
        }  
    }

    //Default company is again 'aaa'.

}

Monday, July 22, 2013

Difference between two dates in days in Dynamic AX

To get the difference between two dates in dynamics ax just convert the dates into numbers and subtract one from the other. The result would be the amount of days between these dates.

today() - mkdate(31,01,2011) ;
or
days = date2num(Date123) - date2num(Date234);
or
TblName.FieldName= date2num(today()) - date2num(AnotherTblName.DatefeildName);


days = date2num(Date123) - date2num(Date234);

To get the difference between two DateTime Values just use the DateTimeUtil Class. The result is the difference in seconds. So just divide these through #secondsPerDay and you'll get the days

#timeConstants

days = DateTimeutil::getDifference(DateTime123, DateTime234) / #secondsPerDay;
-------------------------------------------------------------------
date2num(systemdateget()) - date2num(urtable.date);
---------------------------------------------------------------------------------------------------------
static void DateDiff(Args _args)
{
    TransDate   d1,d2;
    int daysdiff;
    ;
    d1  = 31\12\2010;
    d2  = today();
    daysDiff = d2 - d1;
    info(strfmt("%1",daysDiff));
}
-----------------------------------------------------------------------------------------------------
Or just override the field in datasource level as modified()

then
tablename.fieldName3 = tablename.fieldName2 - tablename.fieldName1;
filedName1 - Out put field,
filedName2 - To date field,
filedName2 - from date field,


SQL code to upate one Legal entity banner to all the legal entity in D365

 Hi, update companyimage set  companyimage.Image  = companyimageA.Image  from  ( select Image from companyimage where dataAreaid = 'USF...