How to do it...

To create a handler class to add further validation to the state machine, follow these steps:

  1. Create a class named ConVMSVehicleTableInspStatusParameters and complete it with the following code:
class ConVMSVehicleTableInspStatusParameters
{
public ConVMSVehInspStatus fromStatus;
public ConVMSVehInspStatus toStatus;
public ConVMSVehicleTable vehicleTable;
}
  1. Next, create a class named ConVMSVehicleInspStatusHandler and add the following code to it:
ConVMSVehInspStatus fromStatus;
ConVMSVehInspStatus toStatus;
ConVMSVehicleTable vehicleTable;

public static ConVMSVehicleTableInspStatusHandler
NewParameters(ConVMSVehicleTableInspStatusParameters
_parameters)
{
ConVMSVehicleTableInspStatusHandler instance
= new ConVMSVehicleTableInspStatusHandler();
instance.fromStatus = _parameters.fromStatus;
instance.toStatus = _parameters.toStatus;
instance.vehicleTable = _parameters.vehicleTable;
return instance;
}
public boolean Validate()
{
switch (toStatus)
{
case ConVMSVehInspStatus::Complete:
if (vehicleTable.InspComment == '')
{
DictField field = new DictField(
tableNum(ConVMSVehicleTable),
fieldNum(ConVMSVehicleTable, InspComment));

//The field %1 must be filled in"
return checkFailed (strFmt("@SYS110217",
field.label()));
}
break;
}
return true;
}

public void run()
{
if(toStatus == fromStatus)
{
return;
}
if(this.Validate())
{
switch (toStatus)
{
case ConVMSVehInspStatus::Complete:
Timezone tz =
DateTimeUtil::getClientMachineTimeZone();
ConWHSVehInspDate inspDate;
inspDate = DateTimeUtil::getSystemDate(tz);
vehicleTable.InspDate = inspDate;
break;
}
}
else
{
vehicleTable.InspStatus = fromStatus;
}
}
There is nothing new about the preceding code, except that we don't (and must not) call update on the record. It is just used to validate the transition; in our case, it will stop the transition if the comment is blank.
  1. Create a new class named ConVMSVehicleTableInspStateMachineHandler and add the following method. This ties the transition delegate to our handler class:
[SubscribesTo(classStr(ConVMSVehicleTableInspStateMachine),
delegateStr(ConVMSVehicleTableInspStateMachine, Transition))]
public static void HandleTransition(ConVMSVehicleTableInspStateMachineTransitionEventArgs _eventArgs)
{
ConVMSVehicleTableInspStatusParameters parms = new
ConVMSVehicleTableInspStatusParameters();
parms.fromStatus = _eventArgs.ExitState();
parms.toStatus = _eventArgs.EnterState();
parms.vehicleTable = _eventArgs.DataEntity();

ConVMSVehicleTableInspStatusHandler::NewParameters(parms).Run();
}