Tuesday 9 August 2016

Combine XPOs into Single XPO in AX 2012


Microsoft has given a new feature called Combine XPO, which will mainly be used in big project where more than one technical is developing and keeping their own XPO in separate folder.

It is very hard to import one by one if we want to move code by using XPO. Of course, Modelstore is the best option but if there is any scenario where project has to move by XPO, its better we can organize those XPO and combined into single XPO by using the below command.

For example, you have organized all the XPOs and store in the below path,

C:\Users\erp3\Desktop\SourceXPOs\

For example, you want to place the finalized single XPO in the below path,

C:\Users\erp3\Desktop\New\AXDestinationfiles.xpo

Step1:

Open Command Prompt with Administrator mode

Step 2:

Bring the below path by running the below command,

cd \Program Files\Microsoft Dynamics AX\60\ManagementUtilities

Step 3: Run the below command,

CombineXPOs.exe -XpoDir C:\Users\erp3\Desktop\SourceXPOs\ -CombinedXpoFile C:\Users\erp3\Desktop\New\ AXDestinationfiles.xpo

Great! Now you have combined all the XPOs into single XPO..

Happy coding!

Sunday 7 August 2016

Recursive function in X++


One of my project, I have to show the records based on the employee position wise in form. The requirement is 1) CEO can view all the records and root node employees (Emp6, Emp7, Emp8 & Emp4) can only see his\her records if they created.

For Ex1: Emp5 can view only his record and Emp7 and Emp8 records.

Ex2: Emp4 can view only his\her record.

Ex3: Emp 1 can view his records and Emp3, Emp4 & Emp6

So in any transaction if you want to show the records based on the position hierarchy need to write a recursive method to get the reporting position employee id.

Step 1:

Table must have userid field who is creating the record

Step 2: Create a below form method and pass the current empl id

static void Ave_ReporttoPosition(EmplId ownerid)

{

    HRPPartyPositionTableRelationship     _partyEmplId1,_partyEmplId2,_partyEmplId3;

  

    EmplId                                ownerid;

    str 255                               reporttoposition ;

    int                                   i, j, k, m, n;

    container                             con,con1,con2,conbackup,conRPId,conRPIdTmp;

    PositionId                            _PositionId;

    boolean                               test;

 

       void  GITL_AbsRPosition(PositionId  _posid)

        {

            while select _partyEmplId2 where _partyEmplId2.ReportsToPosition == _posid

            {

               if(_partyEmplId2.ReportsToPosition)

               {

                  con = conins(con,j,_partyEmplId2.PositionId);

                  j++;

                  conRPId = conins(conRPId,k,_partyEmplId2.Reference);

                  k++;

               }

            }

         }

    ;

    ownerid     = '10009';  //_emplid ;

    i = 1;

    j = 1;

    k = 2;

    con        = connull();

    conRPId    = connull();

    conRPIdTmp = connull();

    select _partyEmplId1 where _partyEmplId1.Reference == ownerid;

 

     conRPId  = conins(conRPId,1,ownerid);

     if(_partyEmplId1.ReportsToPosition)

     {

        while select _partyEmplId3 where _partyEmplId3.ReportsToPosition == _partyEmplId1.PositionId

        {

            if(_partyEmplId3.ReportsToPosition)

               {

                  con = conins(con,j,_partyEmplId2.PositionId);

                  j++;

 

                  conRPId = conins(conRPId,k,_partyEmplId2.Reference);

                  k++;

               }

        }

         for(i = 1; i >= conlen(con); i++)

         {

             _PositionId  = conpeek(con,i);

             this.GITL_AbsRPosition(_PositionId);

         }

             conRPIdTmp = conRPId;

     }

     Else

     {

        conRPIdTmp  = conRPId;

     }

  return conRPIdTmp;

} 

(This is Ax 2009 – code but you can get the recursive logic from this sample code)

Step 4) The above method will get the report to position emplid of the current employee id . But the values are in container. So get those values into string variable and then pass all the emplid into executequery  method (As you know that write a query filer and pass this value)

Guess what, every user can able to see their own record as well as their reporting records.

May be I may not explianied here properly. If you have any queries or doubt, please comment or send me an email. I will try to help you!

Thursday 4 August 2016

Number of days between two date in X++ (Axapta)

Number of days between two date:

I have written a job which will give you an output of number of months and remaining days between two date.

static void Ab_DateDifference(Args _args)
{
    int     Sttotdays, diffdays, EndTotdays, Enddiffdays;
    int     i, day, d1, mth, yr;
    date  middate;
    int     stleapyr, frmleapyr,toleapyr, leapyrs;

    date StartDate = str2Date("2/13/2014", 213);

    date EndDate = str2Date("3/29/2019", 213);

    int noOfMonths,noOfMonthsBetweenYears, noofdays;
    ;

    if (dayOfMth(EndDate) >= dayOfMth(StartDate))
    {
       noofdays = dayOfMth(EndDate) - dayOfMth(StartDate);
       if (mthOfYr(StartDate) > 2)
        {

            frmleapyr = year(nextYr(StartDate));
            toleapyr  = year(EndDate);
        }
        else
        {
            frmleapyr = year(StartDate);
            toleapyr  = year(EndDate);
        }

        for ( i = frmleapyr ; i <= toleapyr ; i++)
        {
            if((i mod 4) == 0)
            {
                leapyrs = leapyrs + 1;
            }
        }
        if (noofdays == 31)
        {
           noofdays = 0;
           noOfMonthsBetweenYears = noOfMonthsBetweenYears + 1;
        }

       noofdays = noofdays + leapyrs;
       noOfMonthsBetweenYears = intvNo(EndDate, StartDate, intvScale::YearMonth);
    }
    else
    {
        day = dayOfMth(EndDate);
        mth = mthOfYr(EndDate);
        yr  = year(EndDate);

        d1 = dayOfMth(StartDate);


        middate = mkDate(d1, mth-1, yr);

        noOfMonthsBetweenYears = intvNo(middate, StartDate, intvScale::YearMonth);


        switch (mthOfYr(middate))
        {
            case 1:  Sttotdays = 31;
                     diffdays = Sttotdays + 1  - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;

            case 2:  stleapyr = year(middate);
                     if ((stleapyr mod 4) == 0 && !((stleapyr mod 100) == 0))
                     {
                         Sttotdays = 29;
                         diffdays = Sttotdays + 1 - dayOfMth(middate);
                         noofdays = diffdays + dayOfMth(EndDate);
                     }
                     else if ((stleapyr mod 400) == 0)
                     {
                         Sttotdays = 29;
                         diffdays = Sttotdays + 1 - dayOfMth(middate);
                         noofdays = diffdays + dayOfMth(EndDate);
                     }
                     else
                     {
                         Sttotdays = 28;
                         diffdays = Sttotdays + 1 - dayOfMth(middate);
                         noofdays = diffdays + dayOfMth(EndDate);
                     }
                break;
            case 3:  Sttotdays = 31;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 4:  Sttotdays = 30;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 5:  Sttotdays = 31;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 6:  Sttotdays = 30;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 7:  Sttotdays = 31;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 8:  Sttotdays = 31;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 9:  Sttotdays = 30;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 10: Sttotdays = 31;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 11: Sttotdays = 30;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;
            case 12: Sttotdays = 31;
                     diffdays = Sttotdays + 1 - dayOfMth(middate);
                     noofdays = diffdays + dayOfMth(EndDate);
                break;

        }

        if (noofdays == 31)
        {
           noofdays = 0;
           noOfMonthsBetweenYears = noOfMonthsBetweenYears + 1;
        }
    }

    info(strFmt("noOfMonthsBetweenYears  : %1  noofdays: %2", int2str(noOfMonthsBetweenYears), noofdays));
}