Thursday, June 2, 2016

AX 2012: Create Custom Workflow


I have Student Fee Collection Table and School Administration wants Approval Process,
If fee is pending then they are not allow for next semester etc.

Base Enums
(1) Create “CustFreeInvoiceWFApprovalStatus”  base enums for the workflow approval status



Create “FStatus”  base enums for the workflow approval status


(2) Create New Table “studenttable”

Field
Datatype
Id
Integer
Name
String
Fee
Real, Extended Datatype= AmountMST
FStatus
Enum, Enum Type= FStatus
WorkflowStatus
Enum,Enum Type= CustFreeInvoiceWFApprovalStatus

Override Method on "studenttable" Table
Override canSubmitToWorkflow method on studenttable

public boolean canSubmitToWorkflow(str _workflowType = '')
{
 
    boolean ret;
 
   if(this.FStatus==FStatus::Inreview)
    {
        ret=true;
    }

    return ret;}

(3) Create Query “studentQuery”


(4) Create simple list form “StudentForm


(6) Create New Menu Itemà Displayà Studentmenu



(7) Workflow Category:

AOTàworkflowàWorkflow Categoiesàcreate new “StudentWFCategory


(8)Workflow Type: “studentwft”

AOT-->Workflow-->Workflow type-->Right Click-->Add-Ins-->WorkFlow type Wizard-->Next

This will create a private project ,
After the workflow type is created, add code for the workflow events.

 Add below code to the studentwftSubmitManager class on workflow type project

(9)Create one method “Submit”

void submit(Args args)
{
// Variable declaration.
recId recId = args.record().RecId;
WorkflowCorrelationId workflowCorrelationId;
// Hardcoded type name
WorkflowTypeName workflowTypeName =workflowtypestr("studentwft");
// Initial note is the information that users enter when they
// submit the document for workflow.
WorkflowComment note ="";
WorkflowSubmitDialog workflowSubmitDialog;
    studenttable stable;
//CustInvoiceLine custInvoiceLine;
// Opens the submit to workflow dialog.
workflowSubmitDialog =WorkflowSubmitDialog::construct(args.caller().getActiveWorkflowConfiguration());
workflowSubmitDialog.run();
if (workflowSubmitDialog.parmIsClosedOK())
{
recId = args.record().RecId;
stable = args.record();
// Get comments from the submit to workflow dialog.
note = workflowSubmitDialog.parmWorkflowComment();
try
{
ttsbegin;
workflowCorrelationId =Workflow::activateFromWorkflowType(workflowTypeName,recId,note,NoYes::No);
stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Submitted;
// Send an Infolog message.
info("Submitted to workflow.");
ttscommit;
}
catch(exception::Error)
{
info("Error on workflow activation.");
}
}
args.caller().updateWorkFlowControls();

(10) Goto “main” method of class

public static void main(Args args)
{
   studentwftSubmitManager  SubmitManager =new studentwftSubmitManager();
    SubmitManager.submit(args);
}


(11) Update code on studentwftEventHandler class for different events

Add below code on completed method

public void completed(WorkflowEventArgs _workflowEventArgs)
{
   studenttable stable;
    RecId SRecId;
   SRecId =_workflowEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Completed;
    stable.FStatus=FStatus::Approve;
    stable.update();
    ttsCommit;
}

Add below code on started method

public void started(WorkflowEventArgs _workflowEventArgs)
{
    studenttable stable;
    RecId SRecId;
   SRecId =_workflowEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Pending;
    stable.FStatus=FStatus::Pending;
    stable.update();
    ttsCommit;

}

Add below code on canceled method

public void canceled(WorkflowEventArgs _workflowEventArgs)
{
     studenttable stable;
    RecId SRecId;
   SRecId =_workflowEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Cancellation;
    stable.FStatus=FStatus::Cancel;
    stable.update();
    ttsCommit;
}

(12) Update Lables on Menuitems

studentwftSubmitMenuItemàproperties à set label as Submit-WFT
studentwftCancelMenuItemàpropertiesà set label as Cancel-WFT

***************************************************************************
(13) Enable workflow on form

Goto Form “StudentForm” -à Design->properties
Workflowenabled=Yes ,
workflowdatasource= studenttable
workflowtype= studentwft


(14) Workflow Approval:

Open the AOT.-->Expand the Workflow node.-->
Right-click on Approvals and select Add-ins > Approval wizard.
Click Next.

This wizard will create the private project as below:

(15) Now, Update code on studentApprovalEventHandler for different events

public void completed(WorkflowElementEventArgs _workflowElementEventArgs)
{
    studenttable stable;
    RecId SRecId;
    SRecId=_workflowElementEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Completed;
    stable.FStatus=FStatus::Approve;
    stable.update();
    ttsCommit;
}

public void canceled(WorkflowElementEventArgs _workflowElementEventArgs)
{

    studenttable stable;
    RecId SRecId;
    SRecId=_workflowElementEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Cancellation;
    //stable.FStatus=FStatus::Cancel
    stable.update();
    ttsCommit;}

 public void changeRequested(WorkflowElementEventArgs _workflowElementEventArgs)
{
    studenttable stable;
    RecId SRecId;
    SRecId=_workflowElementEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::ChangeRequested;
    stable.update();
    ttsCommit;
}

public void denied(WorkflowElementEventArgs _workflowElementEventArgs)
{
    studenttable stable;
    RecId SRecId;
    SRecId=_workflowElementEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Rejected;
    stable.FStatus=FStatus::Cancel;
    stable.update();
    ttsCommit;
}

public void returned(WorkflowElementEventArgs _workflowElementEventArgs)
{
   studenttable stable;
    RecId SRecId;
    SRecId=_workflowElementEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::Rejected;
    stable.update();
    ttsCommit;
}

public void started(WorkflowElementEventArgs _workflowElementEventArgs)
{
    //WorkflowElementStartedEventHandler
    studenttable stable;
    RecId SRecId;
    SRecId=_workflowElementEventArgs.parmWorkflowContext().parmRecId();
    ttsBegin;
    select forUpdate stable where stable.RecId == SRecId;
    stable.WorkflowStatus =CustFreeInvoiceWFApprovalStatus::PendingApproval;
    stable.FStatus=FStatus::Pending;
    stable.update();
    ttsCommit;
}

(16) Update Lables on Menuitems

studentApprovalApprove set label as Approve
studentApprovalRejectset label as Reject
studentApprovalRequestChange set label as Request change
studentApprovalDeny  set label as ApprovalDeny

(17) Drag workflow approval to workflow type

Drag workflow approval “studentApproval” to workflow type under studentwft> Supported elements



(18)Create Menu for Menu itemàDisplayàStudentworkflow


(19)Drag and Drop Menu items  ” Studentworkflow”  in “Home” Menu


(20)Drag and Drop Menu items  ” Studentmenu”  in “Home” Menu


(21)Start Compile , AOT-->Right click--> incremental CIL compile 

(22)Open Work Flow: menu and student Form