We had a fairly complex workflow defined via the GUI in Customization Projects, but the last upgrade gave us a lot of errors trying to upgrade the workflows for custom screens. The recommendation was to move from GUI based workflow to Code based workflow, so I’ve been learning about writing custom workflows via code in Visual Studio and even wrote a series of blog posts on my personal blog to share my “one step at a time” learning approach for the basics of coded workflows.
Although almost all of our workflows have been converted to code, one challenge still stands in the way. Auto-Run Actions. We have 2 actions (1 per screen on 2 different screens) that need to run automatically under a specific condition. We had them working in the GUI version, but I cannot find how to code this in the workflow. The only examples in the code repository are for InitializeState, but “copy cat” programming isn’t giving me any results on this one.
GUI version
The action called is defined as PXAction in the graph. The action is setup in the workflow as follows:
On the approved state, the action is defined as auto-run, which causes it to run once the state changes to Approved.
Coded Workflow Version
I found an IsAutoAction, but to use that the action must be converted from PXAction to PXAutoAction. (This breaks business events that fire the PXAction, so I have tried changing back to PXAction and creating a new PXAutoAction to call via workflow code which simply presses the original PXAction.)
See below for the original CheckReadyToProcess action defined as a normal action and as a (commented out) Auto Action. You also can see the CheckRTP Auto Action created to “auto-run” and call the original action used by the business event.
public PXAutoAction<SSRQRequisition> CheckRTP;
[PXUIField(DisplayName = "Check Ready to Process", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update)]
[PXButton]
protected virtual void checkRTP()
{
CheckReadyToProcess.Press();
}
#region Action - CheckReadyToProcess
//public PXAutoAction<SSRQRequisition> CheckReadyToProcess;
public PXAction<SSRQRequisition> CheckReadyToProcess;
[PXUIField(DisplayName = "Check Ready to Process", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update)]
[PXButton]
protected virtual void checkReadyToProcess()
{
//Insert Code Here
}
In the following code, you can see where I commented out my attempt to use the original CheckReadyToProcess action as IsAutoAction and used a new CheckRTP action defined as the PXAutoAction required to use c.IsAutoAction().
states
.Add<State.approved>(flowState => flowState
.WithActions(actions =>
{
actions.Add(g => g.putOnHold);
actions.Add(g => g.CheckReadyToProcess, a => a.IsDuplicatedInToolbar());
actions.Add(g => g.CheckRTP, c => c.IsAutoAction());
//actions.Add(g => g.CheckReadyToProcess, c => c.IsAutoAction());
})
.WithFieldStates(fields =>
{
fields.AddAllFields<SSRQRequisition>(c => c.IsDisabled());
fields.AddField<requisitionCD>();
fields.AddField<hold>(c => c.IsHidden());
}));
The purpose of this code is that once the SSRQRequisition has been approved, the system should check automatically if the approved requisition is ready to process. A business event monitors to see if anything previously requiring further action has been resolved and now can be processed.
In this example, the business event overcomes the failure to fire an auto-run action, so the user just has to wait a moment and refresh the screen. A nuisance, but functional at a basic level. My other application of auto-run actions is less forgiving. When a user applies a value to a field and changes the state, the value must be pushed into related records… but only once the record has been approved with the entered value where the next business step is far less convenient to wait on a business event to fire.
The Question - Finally
How do I define an auto-run action in workflow via code so that if fires equal to the way the one shown in the GUI version above?