Thursday, August 11, 2016

Submit/Approve/Reject/Recall/Cancel workflow from external webservice ax 2012 - Workflow Esacalating

//Refer below code and rewritten as per you workflow type and document table.

Note:
For
Action (Approve, Reject, Request change)
Cancel
Resubmit

Need to execute seperate set of code below in job.

static void WorkflowAutoxplusplus(Args _args)
{
    recId                                   recId;
    SAN_LeaveTrans                           leaveTrans;
    SAN_LeaveRequestSubmitManager            leaveRequestManager;
    WorkflowComment                         note;
    FormRun                                 formRun;
    WorkflowVersionTable                    versionTable;
    //WorkflowTable                           workflowTable;
    tableId                                 tableId;
    WorkflowTypeName                        workflowTypeName    = workflowtypestr(SAN_LeaveTransWFType);
    Args                                    args                = new Args();
    Args                                    argsCheck           = new Args();
    WorkflowCorrelationId                   workflowCorrelationId;
    FormRun                                 callerForm;
    SAN_LeaveTransWFTypeSubmitManager        resubmit;
    SAN_LeaveTransWFTypeCancelManager        cancel;
    SysWorkflowApprovalService      workflowservice;
    WorkflowWorkItemTable   workitemTable;
    recId                    elementTableRecId;
    WorkflowActionTable      actionTable;
    SysWorkflowTable        workflowTablecheck;
    WorkflowWorkItemTable WorkflowWorkItemTablechk,WorkflowWorkItemTableins;
 
    //Function Do message processing - For escalating workflow work items Auto
    void doMessageProcessing()
    {
        #Event
        Microsoft.Dynamics.AX.Framework.Workflow.Runtime.WorkItemActivityContext activityContext;
        SysWorkflowMessageTable     workflowMessageTable;
        WorkflowWorkItemTable       workflowWorkItemTable;
        SysWorkflowElementTable     workflowElementTable;
        SysWorkflowTable            workflowTable;
        WorkflowNotificationStaging workflowNotificationStaging;
        SysWorkflowNotificationManager  notificationManager;
        ;
        notificationManager = SysWorkflowNotificationManager::construct();
        SysWorkflowMessageQueueManager::runNow();
        select count(RecId) from workflowMessageTable where
            workflowMessageTable.MessageLifeCycleState == WorkflowMessageLifeCycleState::Queued;
     
        while select workflowWorkItemTable where
            (workflowWorkItemTable.Type == WorkflowWorkItemType::WorkItem ||
             workflowWorkItemTable.Type == WorkflowWorkItemType::Escalated) &&
            (workflowWorkItemTable.Status == WorkflowWorkItemStatus::Pending ||
             workflowWorkItemTable.Status == WorkflowWorkItemStatus::Delegated) &&
            workflowWorkItemTable.DueDateTime < DateTimeUtil::getSystemDateTime()
        {
            WorkflowWorkItem::escalateWorkItem(workflowWorkItemTable);
        }
        //take action on elements that exceed max runtime
        while select workflowElementTable where workflowElementTable.DueDateTime < DateTimeUtil::getSystemDateTime()
        {
            workflowTable = SysWorkflowTable::find(workflowElementTable.CorrelationId);
            if (workflowTable.RecId && workflowTable.Version == WorkflowAXVersion::AX6)
            {
                // we need to post a message to WF runtime that causes it to take the action
                SysWorkflowQueue::elementDueDateExceeded(
                    workflowElementTable.CorrelationId,
                    workflowElementTable.ElementId,
                    workflowElementTable.MaxRuntimeId,
                    workflowElementTable.DueDateTime);
            }
            // Note if there is not a workflow instance record there is nothing we can do here the
            // batch will eat an exception workflow associated with the missing record will be in
            // an unrecoverable state - so we just fall through
        }
        select workflowNotificationStaging;
        if (workflowNotificationStaging)
        {
            notificationManager.run();
        }
    }
    ;
   
    info(strFmt("%1",time2str(timeNow(),1,1)));
    leaveTrans  = SAN_LeaveTrans::find("SAN_K-000054",true);
    recId       = leaveTrans.RecId;
    args.record(leaveTrans);
   
    argsCheck   = new Args();
    argsCheck.name(formStr(SAN_LeaveTransListPage));
    formRun     = classFactory.formRunClass(argsCheck);
    formRun.run();
    formRun.close();
    formRun.detach();
    args.caller(formRun);
   
    //Submit to workflow
    if(leaveTrans.canSubmitToWorkflow())
    {
        ttsbegin;
        note = "Workflow submitted for approval";
        workflowCorrelationId = Workflow::activateFromWorkflowType(workflowTypeName, recId, note, NoYes::No,curUserId());
        SAN_LeaveTrans::UpdateWorkflowState(recId,SAN_WorkflowState::PendingApproval);
        ttscommit;
        info("Done");
    }
    else
    {
        warning("Leave trans workflow cannot be submitted");
    }
    SAN_LeaveTransWFTypeSubmitManager::main(args); // Workflow submit manager class
 
 
    //Action auto Workflow
    select firstonly workitemTable
            where workitemTable.RefRecId == recId
                && workitemTable.RefTableId == 30034;
   
    elementTableRecId = WorkflowElementTable::find(workItemTable.ElementId).RecId;
    info(strFmt("%1",elementTableRecId));
   
    actionTable = WorkflowActionTable::findByAction(elementTableRecId,"Approve");
    info(strFmt("%1",actionTable.RecId,actionTable.Name));
    workflowservice = New SysWorkflowApprovalService();
    workflowservice.applyAction("Approve",workitemTable.Id,"testuser","SAN_i.com.kw","Leave Transaction Approved"); //Done for Approve and Reject
 
   //Cancel

    //Cancel workflow
   
    WorkflowCancelManager::cancelWorkflows(WorkflowCancelManager::getWorkflowsToCancel(args.caller()), note);
    cancel= SAN_LeaveTransWFTypeCancelManager::construct();
    cancel.run(args);
 
    SysWorkflow::cancel();
    SysWorkflow::cancelWorkflow();
   
    select firstonly workflowTablecheck
            where workflowTablecheck.InstanceNumber == "001587"
            && workflowTablecheck.ContextRecId == SAN_LeaveTrans::find("SAN_K-000057",true).RecId
            && workflowTablecheck.ContextTableId == tableNum(SAN_LeaveTrans)
            && workflowTablecheck.ContextCompanyId == curext()
            && workflowTablecheck.Category == WorkflowInstanceCategory::Workflow;
    if("Auto Cancel by ADMIN")
    {
        workflow::cancelWorkflow(workflowTablecheck.WorkflowCorrelationId,"Auto Cancel by ADMIN");
        info("Verified");
    }
 
    //Resubmit option by auto workflow
    while select WorkflowWorkItemTablechk
    where
        WorkflowWorkItemTablechk.UserId == 'testuser'
    &&  WorkflowWorkItemTablechk.Type == WorkflowWorkItemType::Recall
    &&  WorkflowWorkItemTablechk.Status == WorkflowWorkItemStatus::Pending
    &&  WorkflowWorkItemTablechk.RefTableId == tableNum(SAN_LeaveTrans)
    && WorkflowWorkItemTablechk.RefRecId == SAN_LeaveTrans::find("SAN_K-000057",true).RecId
    {
        WorkflowWorkItemTableins.clear();
        WorkflowWorkItemTableins.UserId = curUserId();
        WorkflowWorkItemTableins.Type = WorkflowWorkItemType::Recall;
        WorkflowWorkItemTableins.Status = WorkflowWorkItemStatus::Pending;
        WorkflowWorkItemTableins.RefRecId = WorkflowWorkItemTablechk.RefRecId;
        WorkflowWorkItemTableins.RefTableId = WorkflowWorkItemTablechk.RefTableId;
        WorkflowWorkItemTablechk.ActivityId = WorkflowWorkItemTablechk.ActivityId;
        WorkflowWorkItemTablechk.CommentId = WorkflowWorkItemTablechk.CommentId;
        WorkflowWorkItemTablechk.CompanyId = WorkflowWorkItemTablechk.CompanyId;
        WorkflowWorkItemTablechk.ConfigurationId = WorkflowWorkItemTablechk.ConfigurationId;
        WorkflowWorkItemTablechk.DataSourceName = WorkflowWorkItemTablechk.DataSourceName;
        WorkflowWorkItemTablechk.Description = WorkflowWorkItemTablechk.Description;
        WorkflowWorkItemTablechk.DocumentClassId = WorkflowWorkItemTablechk.DocumentClassId;
        WorkflowWorkItemTablechk.DueDateTime = WorkflowWorkItemTablechk.DueDateTime;
        WorkflowWorkItemTablechk.ElementId = WorkflowWorkItemTablechk.ElementId;
        WorkflowWorkItemTablechk.EscalationLevel = WorkflowWorkItemTablechk.EscalationLevel;
        WorkflowWorkItemTablechk.id = WorkflowWorkItemTablechk.id;
        WorkflowWorkItemTablechk.IsClaimed = WorkflowWorkItemTablechk.IsClaimed;
        WorkflowWorkItemTablechk.IsRead = WorkflowWorkItemTablechk.IsRead;
        WorkflowWorkItemTablechk.MenuItemName = WorkflowWorkItemTablechk.MenuItemName;
        WorkflowWorkItemTablechk.NotificationId = WorkflowWorkItemTablechk.NotificationId;
        WorkflowWorkItemTablechk.OriginalCorrelationId = WorkflowWorkItemTablechk.OriginalCorrelationId;
        WorkflowWorkItemTablechk.ParentCorrelationId = WorkflowWorkItemTablechk.ParentCorrelationId;
        WorkflowWorkItemTablechk.Queue = WorkflowWorkItemTablechk.Queue;
        WorkflowWorkItemTablechk.RootCorrelationId = WorkflowWorkItemTablechk.RootCorrelationId;
        WorkflowWorkItemTablechk.Subject = "Recall";
        WorkflowWorkItemTablechk.SubWorkflowId = WorkflowWorkItemTablechk.SubWorkflowId;
     
        WorkflowWorkItemTableins.insert();
        WorkflowWorkItemActionManager::dispatchWorkItemAction(
                                    WorkflowWorkItemTableins,
                                    "Auto Cancel by ADMIN", // comment
                                    'testuser', // << user ID
                                    WorkflowWorkItemActionType::Resubmit,
                                    "SAN_LeaveTransListPage", // << menu item action approval workflow
                                    false); //is not web Menu Item
        info("Verified");
    }
 
 
    doMessageProcessing();
   
    info(strFmt("%1",time2str(timeNow(),1,1)));
}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...