In my previous posts, we learned how to create new tables and pages, and also how to customize standard base objects.
In today's post, we will learn how to enable workflow.
If we require approval workflow for purchase, sales order etc… we enable standard workflow of Dynamics BC 365.
In our case, we need a customized workflow for our Claim.
Pre-Requisites
- Dynamics Business Central 365
- Claim Management Extension
You must achieve the two earlier Workshops to develop the Claim Management Extension.
Developing Extension in AL Using Visual Studio Code – Part 1
Developing Extension in AL Using Visual Studio Code – Part 2
Steps:
- Manage Approval status for each claim document
- Add "Approval Status" field to Claim Card and buttons to handle approval request entries.
- Create events for workflow
- Add events to library
- Check workflow (Activated or No)
- Handling workflow response
- Setup claim workflow
- Access record from the approval request page
- Publish & Enjoy
IDRange for our extension will be from 60011 to 60015
Manage Approval Status for each claim document
We need to extend Claim table by adding a new field "Approval Status" with option string as (Open,"Pending Approval",Released)
Extend Claim table and add new field "Approval Status".
Create new file Tab60000-Ext60011ClaimExt.al and add code like below:
As you see, we got an error: The target Table Claim for the extension object is not found.
This is because we are trying to extend an extension.
So before, we need to specify the dependency to the parent extension in the app.json file of our child extension. (Claim Workflow)
Note: The parent extension (ClaimManagementExt) should be published.
The dependencies property accepts an array of value, so we can specify multiple dependency extension.
Add "Approval Status" field to Claim Card and buttons to handle approval request entries.
- Add "Approval Status" field and Approve button
Create new file Pag60000-Ext60011.ClaimExt.al
- Add "Send Approval Request" & "Cancel Approval Request" buttons and trigger it.
To implement this, we first need to create new Codeunit 60014 "Approval Mgmt. Ext" and add two functions like below
Once done, go back to Claim page extension and add buttons like below:
We will add code on the OnAfterGetRecord trigger of the page to handle visibility of each buttons:
Global variables:
Create events for workflow
Now we need to create events for the workflow hence create a new Codeunit 60012 "Workflow Event Handling Ext" and add functions:
RunWorkflowOnSendClaimForApprovalCode : This is a global function with return value as Code and length 128
RunWorkflowOnSendClaimForApproval : This is a global function which is a Subscriber to the publisher Integration event OnSendClaimForApproval created earlier in "Approval Mgt Ext" Codeunit
RunWorkflowOnCancelClaimApprovalCode : This is a global function with return value as Code and length 128
RunWorkflowOnCancelClaimApproval : This is a global function which is a Subscriber to publisher Integration event OnCancelClaimForApproval created earlier in "Approval Mgt Ext" Codeunit
Add events to library
To populate the Workflow Event Table with our new events, we need to add two function on Codeunit "Workflow Event Handling Ext"
OnAddWorkflowEventsToLibrary : This is a local function which is a Subscriber to publisher Integration event OnAddWorkflowEventsToLibrary located on standard codeunit "Workflow Event Handling"
OnAddWorkflowEventPredecessorsToLibrary : This is a local function which is a Subcriber to publisher Integration event OnAddWorkflowEventPredecessorToibrary located on standard codeunit "Workflow Event Handing"
Global Variable:
Once done and before proceeding with Workflow response creation, we will add some functions to our "Approval Mgt Ext" Codeunit to determinate whether the workflow is activated or no and to populate the "Approval Entry" table with Claim Document No.
Check workflow (Activated or No)
Open "Approval Mgt Ext" codeunit and create three more functions:
CheckClaimApprovalsWorkflowEnable & IsClaimDocApprovalsWorkflowEnable: These are global functions with return value as boolean
OnPopulateApprovalEntryArgument: This is a local function which is a Subscriber to publisher Integration event OnPopulateApprovalEntryArgument located on standard codeunit "Approval Mgmt."
Global variables:
Handling workflow response
When Sending, Canceling or Approving a Request Approval Entries, the "Approval Status" of our claim document must be update.
To manage this, we create a new codeunit "Workflow Response Handling Ext" and add functions:
OnOpenDocument: This is a local function which is a Subcriber to publisher integration event OnOpenDocument located in standard codeunit "Workflow Response Handling".
OnReleaseDocument: This is a local function which is a Subscriber to publisher integration event OnReleaseDocument located in standard Codeunit "Workflow Response Handling".
OnSetStatusToPendingApproval: This is a local function which is a Subcriber to publisher integration event OnSetStatusToPendingApproval located in standard Codeunit "Approval Mgmt."
Now we need to add relation between our events and response.
To meet the needs, we will subscribe to publisher integration event located in standard Codeunit "Workflow Response Handling".
Add a subscribe event function like below:
Setup claim workflow
We can setup our new workflow manually or by code.
In this topic, we will see how to implement a new Workflow Template by code.
Worklfow template are created through codeunit 2 (Company-Initialize) calling the function InitWorkflow in Codeunit 1502 (Workflow Setup).
So, we will subscribe to some functions event integration base on this codeunit (Workflow Setup) to handle this.
Create a new Codeunit 60011 "Workflow Setup Ext" and add functions:
●OnAddWorkflowCategoriesToLibrary:
This is a local function which is a Subcriber to publisher integration event OnAddWorkflowCategoriesToLibrary located in standard Codeunit "Workflow Setup" .
●OnAfterInsertApprovalTableRelations:
This is a local function which is a Subcriber to publisher integration event OnAfterInsertApprovalTableRelations located in standard Codeunit "Workflow Setup" .
●OnInsertWorkflowTemplates:
This is a local function which is a Subcriber to publisher integration event OnInsertWorkflowTemplates located in standard Codeunit "Workflow Setup" .
InsertClaimApprovalWorkflowTemplate: This is a local function which will populate the Workflow table and mark It as template.
InsertClaimApprovalWorkflowDetails: This is a local function which will initialize Event with and Response for our Workflow
To initialize steps for the workflow, we will use standard function "InsertDocApprovalWorkflowSteps" located in "Workflow Setup" Codeunit
BuildClaimTypeConditions: This is a local function which will build condition for event.
Global Variables:
·WorkflowSetup : Codeunit 1502
·ClaimWorkflowCategoryTxt : TextConst ‘CDW’
·ClaimWorkflowCategoryDescTxt : TextConst ‘Claim Document’
·ClaimApprovalWorkflowCodeTxt : TextConst ‘CAPW’
·ClaimApprovalWorkfowDescTxt : TextCont ‘Claim Approval Workflow’
·ClaimTypeCondTxt : TextConst ‘<?xml version = “1.0” encoding=”utf-8” standalone=”yes”?><ReportParameters><DataItems><DataItem name=”Claim”>%1</DataItem></DataItems></ReportParameters>
Access record from the approval request page
To access the record from the approval request page, we need to subscribe to event integration function located in standard codeunit "Page Management".
Create new codeunit 600015 "Page Management Ext" and add code like below:
Publish & Enjoy
Here we are …. Now you need to publish the extension and setup approval user.
Our workflow allow user to Send, Cancel or Approve approval request, but if we want to reject or delegate it?
I Invite you to add thosefunctionalities