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!

No comments:

Post a Comment