Tuesday, January 30, 2018

Recover workflow to initial draft state for the document data in ax 2012

static void RecoverWorkflow_VendInvoice(Args _args)
{
    VendInvoiceInfoTable            vendInfoTable;
    VendInvoiceInfoLine             vendInfoLine;

    WorkflowTrackingStatusTable     workflowTrackingStatusTable, workflowTrackingStatusTable_forDelete;
    WorkflowTrackingTable           workflowTrackingTable;
    WorkflowTrackingWorkItem        workflowTrackingWorkItem;
    WorkflowWorkItemTable           workflowWorkItemTable_forDelete;

    WorkflowCorrelationId           rootCorrelationId;
    SysWorkflowTable                sysWorkflowTable_forDelete;

    Dialog                          dialog;
    DialogField                     vendInvoiceField;
    DialogField                     vendInvoiceWFStatus;
    Str 30                          vendInvoiceId;
    Counter                         totalLines;

    dialog              = new Dialog("Delete Workflow history and reset vend invoice status to Draft");
    vendInvoiceField     = dialog.addField(extendedTypeStr(Name));
    vendInvoiceWFStatus = dialog.addField(enumStr(WorkflowTrackingStatus));
    dialog.run();

    if (dialog.closedOk())
    {
        vendInvoiceId = vendInvoiceField.value();

        if (!vendInvoiceId)
            return;

        if (Box::yesNo(strFmt("Do you want to reset vend invoice number %1 in %2 LE?", vendInvoiceId, curext()), DialogButton::Yes == DialogButton::Yes))
        {
            ttsbegin;

            select forUpdate vendInfoTable
                where vendInfoTable.num == vendInvoiceId && vendInfoTable.num != "";

            select firstOnly workflowTrackingStatusTable
                where workflowTrackingStatusTable.ContextCompanyId  == vendInfoTable.dataAreaId &&
                      workflowTrackingStatusTable.ContextTableId    == vendInfoTable.TableId &&
                      workflowTrackingStatusTable.ContextRecId      == vendInfoTable.RecId &&
                      workflowTrackingStatusTable.TrackingStatus    == vendInvoiceWFStatus.value();

            if (vendInfoTable.RequestStatus == VendInvoiceRequestStatus::InReview
                    && workflowTrackingStatusTable)
            {
                rootCorrelationId = workflowTrackingStatusTable.RootCorrelationId;

                //delete work items
                while select workflowTrackingStatusTable
                    where workflowTrackingStatusTable.RootCorrelationId == rootCorrelationId &&
                          workflowTrackingStatusTable.ContextCompanyId  == vendInfoTable.dataAreaId &&
                          (workflowTrackingStatusTable.ContextTableId == tableNum(VendInvoiceInfoTable)
                            || workflowTrackingStatusTable.ContextTableId == tableNum(VendInvoiceInfoLine))
                    join workflowTrackingTable
                        where workflowTrackingTable.WorkflowTrackingStatusTable == workflowTrackingStatusTable.RecId
                        join workflowTrackingWorkItem
                            where workflowTrackingWorkItem.WorkflowTrackingTable == workflowTrackingTable.RecId
                {
                    while select forUpdate workflowWorkItemTable_forDelete
                        where workflowWorkItemTable_forDelete.RecId == workflowTrackingWorkItem.WorkflowWorkItemTable &&
                              workflowWorkItemTable_forDelete.Id    == workflowTrackingWorkItem.WorkItemId
                    {
                        info(strFmt("WorkflowWorkItemTable record ID %1 is deleted", workflowWorkItemTable_forDelete.RecId));
                        workflowWorkItemTable_forDelete.delete();
                    }
                }

                //delete tracking status
                while select forUpdate workflowTrackingStatusTable_forDelete
                    where workflowTrackingStatusTable_forDelete.RootCorrelationId == rootCorrelationId &&
                          workflowTrackingStatusTable_forDelete.ContextCompanyId  == vendInfoTable.dataAreaId &&
                          (workflowTrackingStatusTable_forDelete.ContextTableId == tableNum(VendInvoiceInfoTable)
                            || workflowTrackingStatusTable_forDelete.ContextTableId == tableNum(VendInvoiceInfoLine))
                {
                    info(strFmt("WorkflowTrackingStatusTable record ID %1 instance number %2 is deleted", workflowTrackingStatusTable_forDelete.RecId, workflowTrackingStatusTable_forDelete.InstanceNumber));
                    workflowTrackingStatusTable_forDelete.delete();
                }

                //delete sysWorkflowTable
                while select forUpdate sysWorkflowTable_forDelete
                    where sysWorkflowTable_forDelete.RootCorrelationId == rootCorrelationId &&
                          sysWorkflowTable_forDelete.ContextCompanyId  == vendInfoTable.dataAreaId    &&
                          (sysWorkflowTable_forDelete.ContextTableId == tableNum(VendInvoiceInfoTable)
                            || sysWorkflowTable_forDelete.ContextTableId == tableNum(VendInvoiceInfoLine))
                {
                    info(strFmt("SysWorkflowTable record ID %1 is deleted", sysWorkflowTable_forDelete.RecId));
                    sysWorkflowTable_forDelete.delete();
                }

                //reset status to Draft
                vendInfoTable.RequestStatus = VendInvoiceRequestStatus::Draft;
                vendInfoTable.doUpdate();

                while select forUpdate vendInfoLine
                    where vendInfoLine.TableRefId == vendInfoTable.TableRefId
                        && vendInfoLine.ParmId == vendInfoTable.ParmId
                {
                    vendInfoLine.RequestStatus = VendInvoiceRequestStatus::Draft;
                    vendInfoLine.doUpdate();
                    totalLines++;
                }

                info(strFmt("Workflow instance is deleted."));
                info(strFmt("Vend invoice number %1 has been reset to Draft.", vendInvoiceId));
                info(strFmt("Total lines that reset to Draft: %1.", totalLines));
            }
            else
            {
                info(strFmt("Nothing to update. The approval status is not %1 status or there is no existing workflow history.",enum2str(vendInvoiceWFStatus.value())));
            }

            ttsCommit;

        }
    }

}

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...