MSBA-341 · Lesson 7 of 12 · 19 min
Workflow development
▶ Video coming soon A video version of this lesson is on the way.
Some castings fail inspection paperwork for reasons that have nothing to do with quality. A foundry emails a scanned certificate that is unreadable, or a rush order arrives on a Friday evening and the certificate follows on Monday. Marisol did not want receipts blocked forever in those cases, but she also did not want any warehouse clerk to wave a casting through. Her rule was simple: a waiver could be requested by the receiving clerk, had to be approved by a quality engineer, and above a certain line value also by the plant quality manager. That is an approval process with routing and history, and in finance and operations apps the tool for it is workflow.
Workflow from the developer's side
Functional consultants configure workflows in the graphical editor: they draw the steps, set the conditions and choose the approvers. They can only do that for workflow types that a developer has created. A workflow type defines what kind of document can be approved, which tasks and approvals are available to put in the editor, what conditions can refer to, and what code runs when the document is submitted, approved, rejected or cancelled. Standard workflow types exist for purchase requisitions, vendor invoices, journals and many other documents. No standard type covers a request to waive an inspection certificate, so Desmond and Prudence needed a new one, CRInspWaiver.
The elements of a workflow type
A workflow type is assembled from several elements, and Visual Studio provides a wizard for each. The order matters because later elements refer to earlier ones.
| Element | Purpose | Cobalt Ridge example |
|---|---|---|
| Document table with status field | Stores the document being approved and its workflow state | CRInspWaiverRequest with field WorkflowStatus based on enum CRInspWaiverStatus |
| Query | Defines the data the workflow can see, including fields used in conditions | CRInspWaiverQuery: the request joined to the purchase line |
| Workflow category | Places the type in a module so it appears on the right workflow list page | CRInspWaiverCategory in the Procurement and sourcing module |
| Workflow type | Ties the query, category, document class and menu items together | CRInspWaiver |
| Document class | Exposes calculated values to conditions and placeholders | CRInspWaiverDocument with a parmLineAmount method |
| Approval or task | Defines the step that people act on and its outcomes | CRInspWaiverApproval |
| Event handlers | Run code when the workflow starts, completes, is cancelled, or an approval outcome occurs | CRInspWaiverEventHandler and CRInspWaiverApprovalEventHandler |
| Menu items and submit manager | Let users submit, resubmit and cancel from the form | CRInspWaiverSubmitManager |
The document table needs a status field because the workflow framework does not store the business state of the document itself. Event handlers update that field as the workflow moves, and the form uses it to decide whether the document can be edited. CRInspWaiverStatus had the values Draft, Submitted, Approved, Rejected, ChangeRequested and Cancelled, and the receipt rule from lesson 3 treated the purchase line's inspection status as Waived only when its waiver request reached Approved.
Enabling workflow on a form
Users interact with workflow through the workflow bar at the top of a form, which shows Submit, the current status, and actions such as Approve or Reject for the assigned user. A form becomes workflow-enabled by setting design properties: WorkflowEnabled to Yes, WorkflowDatasource to the document table's data source, and WorkflowType to the type name. The document table must also have a canSubmitToWorkflow method that returns true only when the record is in a state that can be submitted. Without that method, the Submit button would appear even for records that are already approved.
public boolean canSubmitToWorkflow(str _workflowType = '')
{
boolean ret = super(_workflowType);
ret = ret
&& (this.WorkflowStatus == CRInspWaiverStatus::Draft
|| this.WorkflowStatus == CRInspWaiverStatus::ChangeRequested);
return ret;
}Desmond pointed out that this is a new table, so the method is an ordinary override. If a project needs workflow on a standard table that does not already support it, the same method would be added through Chain of Command or by adding a method in a table extension, but that situation is rare: standard documents that need approval almost always come with a standard workflow type.
Submitting
When a user selects Submit, the framework calls the class behind the submit menu item. That class, often called a submit manager, shows the dialog where the user types a comment, then calls Workflow::activateFromWorkflowType with the workflow type name, the record ID, the table ID and the comment. If activation succeeds, the submit manager sets the document's status to Submitted. The resubmit and cancel actions follow the same pattern with their own menu items.
public static void main(Args _args)
{
CRInspWaiverRequest request = _args.record();
WorkflowSubmitDialog dialog = WorkflowSubmitDialog::construct(_args.caller().getActiveWorkflowConfiguration());
dialog.run();
if (dialog.parmIsClosedOK())
{
ttsbegin;
Workflow::activateFromWorkflowType(workFlowTypeStr(CRInspWaiver), request.RecId, tableNum(CRInspWaiverRequest), dialog.parmWorkflowComment());
request.selectForUpdate(true);
request.WorkflowStatus = CRInspWaiverStatus::Submitted;
request.update();
ttscommit;
}
_args.caller().updateWorkflowControls();
}Event handlers
Workflow work happens on the server in batch, not in the user's session. When the workflow starts, completes or is cancelled, the framework calls the type's event handler class, which implements interfaces such as WorkflowStartedEventHandler, WorkflowCompletedEventHandler and WorkflowCanceledEventHandler. When an approver acts, the approval's event handler is called through interfaces for started, completed, returned, change requested and denied events. Each handler receives an event arguments object from which it can read the record ID, load the document and update its status.
This is where business consequences belong. When the waiver approval completed, CRInspWaiverEventHandler set the request to Approved and set the purchase line's CRInspectionStatus to Waived, writing who approved it and when into the request record for the audit trail. When the approver rejected, the handler set the request to Rejected and left the purchase line untouched, so the receipt stayed blocked.
Because handlers run under the workflow execution account, in a batch context, they must not assume a user interface. Prudence's first version displayed an info message in the completed handler; it ran without error but nobody ever saw the message. Desmond told her that anything a user needs to see should be stored on the record or sent as a notification, and that handlers should be short and safe to rerun, because the workflow framework may retry a step after a failure.
The document class and conditions
Conditions in the editor can refer to fields of the query, but some useful values are calculated. The plant quality manager's step should apply only when the line value is above a threshold, and the value is not stored on the request; it comes from the purchase line. A workflow document class extends WorkflowDocument, names the query, and adds parm methods that take the company, table ID and record ID and return calculated values. Any parm method with that signature appears in the condition editor and as a placeholder in instructions and notifications.
class CRInspWaiverDocument extends WorkflowDocument
{
public queryName getQueryName()
{
return querystr(CRInspWaiverQuery);
}
public AmountCur parmLineAmount(CompanyId _companyId, TableId _tableId, RecId _recId)
{
CRInspWaiverRequest request;
changecompany(_companyId)
{
request = CRInspWaiverRequest::findRecId(_recId);
}
return request.purchLine().LineAmount;
}
}With that in place, Annelise built the configuration in Cobalt Ridge's Canadian company: a first approval step assigned to the quality engineer role, and a second step with the condition Line amount greater than 25,000, assigned to the plant quality manager. The amounts were in the company's accounting currency, which differed between CRCA and CRUS, so each company had its own configuration with its own threshold.
Extending a standard workflow
Developers sometimes need to extend a standard workflow type rather than build one. Common needs include exposing a new field to conditions, which is done by adding it to the workflow's query through a query extension or by adding a parm method to the document class through Chain of Command, and running extra logic when a standard approval completes, which is done by wrapping or subscribing to the standard event handler class. The submit manager and handlers of standard types are ordinary classes, so the rules from lessons 3 and 4 apply. Cobalt Ridge's purchase requisition workflow, for example, could show CRInspectionRequired in its conditions if a query extension added the field from the requisition line's related purchase line.
Testing a workflow
Workflow processing depends on the workflow message processing batch job. In a developer environment, Prudence confirmed that the job was running before testing, otherwise submitted documents sit with no progress and look broken. She then submitted a waiver, opened the workflow history from the workflow bar to see the steps the engine had created, approved it as a test user with the quality engineer role, and checked that the purchase line status changed. She repeated the test with a high-value line to see the second step appear, and with a rejection to see the receipt stay blocked. Each outcome was recorded in the test script for user acceptance testing.
Approvals, tasks and outcomes
Workflow types can include two kinds of step elements. An approval asks a person to decide, with the standard outcomes Approve, Reject, Request change and Delegate. A task asks a person to do something and mark it complete, with outcomes such as Complete, Return and Request change. Developers create these elements with their own wizards, and each has its own event handler class and its own menu items, which are what the workflow bar calls when a user chooses an outcome. Cobalt Ridge's waiver used approvals, but Desmond described a task the team considered: asking the receiving clerk to attach a scanned certificate before the waiver went to quality. The idea was dropped because clerks already attached the documents to the request record.
Line-level workflows
Some documents need approval per line rather than for the whole document, such as purchase requisitions where each line can go to a different budget owner. Workflow supports this with line-item workflows, where a header workflow starts a sub-workflow for each line. A line-item workflow type is built on the line table with its own query, and the header type includes a line-item workflow element. The waiver request had no lines, so Cobalt Ridge did not need this, but the exam expects developers to know the option exists and when it applies.
Security for workflow elements
Every menu item a workflow uses must be in a privilege, or users will not see the buttons. That includes the submit, resubmit and cancel menu items of the type, and the approve, reject, request change and delegate menu items of the approval. Prudence added them to the request and approve privileges described in lesson 10. The workflow configuration also has its own security consideration: approvers are assigned by role, user, hierarchy or queue, and the assignment must match the roles that hold the approve privilege, or a user may be assigned work they cannot open.
Workflow versions and changes
When a functional consultant changes a workflow configuration and activates the new version, documents already in progress continue on the version they started with, and new submissions use the new version. Developers should keep this in mind when changing a workflow type's elements: a document submitted before a deployment may still be on an old configuration that refers to elements in their old form. Desmond's rule was never to rename or remove a workflow element in a deployment while documents were in progress, and to add new elements alongside old ones when a change was needed.
Desmond summed up the session with a comparison Prudence found helpful. The developer builds the road: the document, the query, the possible steps and what happens at each outcome. The functional consultant draws the route on that road: who approves, in what order and under which conditions. A good workflow type gives consultants enough choices that they can change the route without calling a developer, which was exactly what happened when Marisol asked for a lower second-approval threshold during sandbox testing.
Worked problem
Placing the waiver decision
Prudence is unsure where to write the code that sets the purchase line to Waived: in the submit manager, in the approval's completed handler, or in the workflow type's completed handler.
Show solution
- The submit manager runs when the user submits, before anyone approves, so it is too early.
- The approval's completed handler runs when one approval step finishes. If there are two steps, it runs twice, once for the quality engineer and once for the plant manager.
- The workflow type's completed handler runs once, when the whole workflow has finished with approval.
- The workflow type's completed handler is therefore the right place to mark the line as Waived.
Answer: Put document-level consequences in the workflow type's completed handler, and step-level bookkeeping, if any, in the approval handlers.
Common mistakes
MistakeExpecting the workflow framework to store the document status.
InsteadAdd a status field to the document table and update it in event handlers.
MistakeShowing info messages from workflow event handlers.
InsteadStore results on the record or send notifications; handlers run in batch.
MistakeTesting workflow without checking the message processing batch job.
InsteadConfirm workflow batch jobs are running before testing.
MistakeWriting document-level consequences in an approval step's handler.
InsteadUse the workflow type's completed handler for consequences that should happen once.
Summary and key terms
- Developers create workflow types; functional consultants configure workflows from them.
- A workflow type ties together a document table with a status field, a query, a category, a document class, approvals or tasks, event handlers and menu items.
- Forms are workflow-enabled through design properties and a canSubmitToWorkflow method on the document table.
- Event handlers run in batch and update the document state; they should be short and safe to rerun.
- Document classes expose calculated values to conditions; standard workflows are extended through query extensions and Chain of Command.
- Workflow type
- The developer-created definition that lets a kind of document be routed for approval.
- Workflow category
- Places a workflow type in a module.
- Workflow document class
- A class extending WorkflowDocument that names the query and exposes calculated values.
- Submit manager
- The class behind the Submit menu item that activates the workflow.
- Workflow event handler
- A class that runs code when a workflow or approval reaches a given event.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Why does CRInspWaiverRequest need a WorkflowStatus field?
Show answer
The framework does not store business state; handlers update the field and forms use it to control editing and submission.
How does a calculated line amount reach the condition editor?
Show answer
Through a parm method on the workflow document class with the company, table ID and record ID parameters.
What makes a waiver look stuck after submission in a developer environment?
Show answer
The workflow message processing batch job is not running.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.