Reader small image

You're reading from  Extending Microsoft Dynamics 365 Finance and Supply Chain Management Cookbook - Second Edition

Product typeBook
Published inMar 2020
PublisherPackt
ISBN-139781838643812
Edition2nd Edition
Right arrow
Author (1)
Simon Buxton
Simon Buxton
author image
Simon Buxton

Simon Buxton has worked with Dynamics 365 Finance and Supply Chain Management since its earliest incarnations, starting with the product in early 1999 when Dynamics 365 Finance and Supply Chain Management was known as Damgaard Axapta 1.5. Simon has been the technical lead on many highly challenging technical projects in countries all around the world. These projects included complex integrations with on-premises and external systems, ISV solutions, and many technically challenging customer solutions. Now working with Binary, he was part of a team that implemented the first Dynamics 365 Finance and Supply Chain Management implementation as part of the Community Technical Preview (CTP) program, which led to the close working relationship with Microsoft that made this book possible
Read more about Simon Buxton

Right arrow

State Machines

State machines are a new concept in SCM and are a very welcome feature. Previously, the control of status fields was handcrafted in code, which could often be hard to read as there was no obvious pattern to follow. Having said that, we will always look at a standard example that's similar to our case and use that idea. This is good practice, as it is good to seek examples in standard code. Using standard code as a pattern often helps other developers understand the code we have written, and, if we can't find a pattern to follow, it might suggest that we rework our technical solution so that we can.

State machines allow us to define how the status transitions from an initial state to its final state through metadata. These rules are then enforced by code that the state machine will generate.

There is a restriction, though: there must be one initial state...

Creating a state machine

This first recipe is all about creating a state machine for vehicle inspection. In Chapter 14, Workflow Development, we created a workflow task and an inspection status field. In this recipe, we will use a state machine to handle the inspection status change logic.

Getting ready

We need to have a table with a status field with an initial and final status, such as the InspStatus field we added to the ConWHSVehicleTable table in Chapter 14, Workflow Development. In this recipe, we will use the main vehicle management project.

How to do it...

To...

Creating a state machine handler class

The state machine provides control over the transition rules, but, sometimes, we want to ensure that other validation rules are obeyed in order to validate whether the transition can be done.

This is done by subscribing to the Transition delegate of the ConVMSVehicleTableInspStateMachine class that was generated by the state machine.

The code in this recipe will be tied programmatically to the state machine. Should you wish to attach the statement to the workflow directly (which is a great idea), the status will be set by the state machine. Therefore, the event handlers must not set the status. Furthermore, should the validation written in this recipe fail, we must ensure that the workflow's internal status matches the state machine's status. This can be done by canceling the workflow by throwing an error.

...

Using menu items to control a state machine

In this section, we will add the state machine to the form so that we can use it. Using menu items for this is a nice concise way to control the state machine and follows the UI patterns that can be found in other areas, such as the projects module.

Getting ready

The prerequisite for this recipe is that we have a table with a state machine that has been generated.

How to do it...

To create the state machine menu items, follow these steps:

  1. Add a new action menu item to the project named ConVMSVehInspStatusWaiting. Complete the...

Hooking up the state machine to a workflow

In this recipe, we will hook up our state machine to the ConVMSVehWFInsp workflow task.

Getting ready

We need to have a workflow task and have completed the recipes in this chapter. The changes that will be made here are best made with the workflow project that we created in the previous chapter.

How to do it...

To hook up the state machine to a workflow task, follow these steps:

  1. Before we start, the workflow needs to be able to move the state directly to Complete. This will cause a fault as our state machine does not have this...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Extending Microsoft Dynamics 365 Finance and Supply Chain Management Cookbook - Second Edition
Published in: Mar 2020Publisher: PacktISBN-13: 9781838643812
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Simon Buxton

Simon Buxton has worked with Dynamics 365 Finance and Supply Chain Management since its earliest incarnations, starting with the product in early 1999 when Dynamics 365 Finance and Supply Chain Management was known as Damgaard Axapta 1.5. Simon has been the technical lead on many highly challenging technical projects in countries all around the world. These projects included complex integrations with on-premises and external systems, ISV solutions, and many technically challenging customer solutions. Now working with Binary, he was part of a team that implemented the first Dynamics 365 Finance and Supply Chain Management implementation as part of the Community Technical Preview (CTP) program, which led to the close working relationship with Microsoft that made this book possible
Read more about Simon Buxton