Sunday, May 23, 2021

get last approver in purchase order in d365fo

 public void getLastApprover(PurchPurchaseOrderHeader    tempTable = tempTable)

    {

        AFZWorkflowStatusTable      afzWorkflowStatusTable;

        WorkflowTrackingStatusTable workflowTrackingStatus;

        WorkflowTrackingTable workflowTrackingTable;

        WorkflowTrackingCommentTable workflowTrackingCommentTable;

        UserInfo userInfo;

        RecId _recId;

        Name _name;

        HcmWorkerRecId  workerRecId;

        HcmWorker       hcmWorker;

        _recId = PurchTable::find(tempTable.PurchId).RecId;


        delete_from afzWorkflowStatusTable;


        while select workflowTrackingStatus order by createdDateTime asc

            where workflowTrackingStatus.ContextTableId             == tableNum(PurchTable)

                && workflowTrackingStatus.ContextRecId              == _recId

                && workflowTrackingStatus.TrackingStatus            == WorkflowTrackingStatus::Completed

        join workflowTrackingTable order by createdDateTime asc

            where workflowTrackingTable.WorkflowTrackingStatusTable == workflowTrackingStatus.RecId

                && workflowtrackingtable.TrackingContext            == workflowtrackingcontext::WorkItem

                && workflowTrackingTable.TrackingType               == WorkflowTrackingType::Approval

        join userInfo

            where userInfo.id                                       == workflowTrackingTable.User

        {

            select firstonly workflowTrackingCommentTable

                where workflowTrackingCommentTable.WorkflowTrackingTable    == workflowTrackingTable.RecId;


            afzWorkflowStatusTable.clear();

            afzWorkflowStatusTable.ContextCompanyId             = workflowTrackingStatus.ContextCompanyId;

            afzWorkflowStatusTable.ContextTableId               = workflowTrackingStatus.ContextTableId;

            afzWorkflowStatusTable.ContextRecId                 = workflowTrackingStatus.ContextRecId;

            afzWorkflowStatusTable.WorkflowTrackingStatusTable  = workflowTrackingStatus.RecId;

            afzWorkflowStatusTable.TrackingStatus               = workflowTrackingStatus.TrackingStatus;

            afzWorkflowStatusTable.WorkflowTrackingTable        = workflowTrackingTable.RecId;

            afzWorkflowStatusTable.TrackingType                 = workflowTrackingTable.TrackingType;

            afzWorkflowStatusTable.TrackingContext              = workflowtrackingtable.TrackingContext;

            afzWorkflowStatusTable.UserId                       = workflowTrackingTable.User;

            afzWorkflowStatusTable.UserName                     = userInfo.name;

            afzWorkflowStatusTable.Comment                      = workflowTrackingCommentTable.Comment;

            afzWorkflowStatusTable.TrackingMessage              = workflowTrackingCommentTable.TrackingMessage;

            afzWorkflowStatusTable.WorkflowTrackingCommentTable = workflowTrackingCommentTable.RecId;

            afzWorkflowStatusTable.TrackingId                   = workflowTrackingCommentTable.TrackingId;

            afzWorkflowStatusTable.ApprovalDateTime             = workflowTrackingTable.CreatedDateTime;

            afzWorkflowStatusTable.Key                          = afzWorkflowStatusTable.ContextCompanyId + '-' + int2Str(afzWorkflowStatusTable.ContextTableId) + '-' +

                                                                  int642Str(afzWorkflowStatusTable.ContextRecId) + '-' + int642Str(afzWorkflowStatusTable.WorkflowTrackingTable) + '-' +

                                                                  int642Str(afzWorkflowStatusTable.WorkflowTrackingCommentTable);

            afzWorkflowStatusTable.insert();

        }


        select firstonly afzWorkflowStatusTable

            Order by afzWorkflowStatusTable.ApprovalDateTime desc

            where afzWorkflowStatusTable.ContextRecId   == _recId;


        _name = afzWorkflowStatusTable.RecId ? afzWorkflowStatusTable.UserName : '';


        if(tempTable.AFZApproverName == '')

        {

            tempTable.AFZApproverName =  _name;

            //workerRecId   = HcmWorker::userId2Worker(userInfo.id);

        

            HcmPositionWorkerAssignment workerAssignment;

            HcmPosition hcmPosition;

            HcmPositionDetail   hcmPositionDetail;

            HcmPositionRecId primaryPositionRecId;


            workerRecId   = HcmWorker::userId2Worker(afzWorkflowStatusTable.UserId);


            select recid, person from hcmWorker

            join worker, position from workerAssignment

            where workerAssignment.Worker == workerRecId;


            primaryPositionRecId = HcmWorkerHelper::getPrimaryPosition(workerAssignment.Worker);


            select recid from hcmPosition

            where hcmPosition.RecId == primaryPositionRecId

            join position, description from hcmPositionDetail

            where hcmPositionDetail.Position == hcmPosition.RecId;

            //select recid, person from hcmWorker

            //join worker, position from workerAssignment

            //where workerAssignment.Worker == workerRecId

            //join recid from hcmPosition

            //where hcmPosition.RecId == workerAssignment.Position

            //join position, description from hcmPositionDetail

            //where hcmPositionDetail.Position == hcmPosition.RecId;

            {

                tempTable.AFZDesignation = hcmPositionDetail.Description;

            }

        }


    }


SQL Workflow Approval code

select * from workflowTrackingStatusTable a -- order by createdDateTime Asc
inner join WorkflowTrackingTable b
on a.RecId = b.WorkflowTrackingStatusTable
and a.TrackingStatus = 1
and a.ContextRecId = 5637145328 --(PurchTable/ PurchReqTable RecID)
and a.ContextTableId = 20050 --(PurchTable/ PurchReqTable TableId)
and b.TrackingType = 4 -- 4 for approval, 9 for creation
and b.trackingContext = 5
order by a.createdDateTime, b.createdDateTime Asc

No comments:

Post a Comment