Solved

Adding Auto-Run Actions to Workflow via Code

  • 5 April 2022
  • 9 replies
  • 544 views

Userlevel 6
Badge +4

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?

icon

Best answer by Brian Stevens 19 October 2023, 00:12

View original

9 replies

Userlevel 6
Badge +4

Update:  I’ve had some guidance to use IsAutoAction, which I’m using in the posted code.  To do this in code, you must define the action as PXAutoAction instead of PXAction.  (Or perhaps there’s more to it which is my underlying issue.)

For now, I’ve implemented a GUI workflow extension of the coded system workflow by simply extending the system workflow and flipping the one action to AutoRun as shown below.  Still would like to know what I’m missing that this effort is required, so any help is appreciated.

 

 

Userlevel 6
Badge +4

Update #2: I got this to work some time back and never remembered to update the post.

In 22r1, you actually add the action in the state where you want it to appear.  Then you must UPDATE the state to Update the action to specify that the action is IsAutoAction().

Ex.  In the first section below, CheckReadyToProcess is added to the approved state.  Later in the workflow, we must update the flow state for approved to tell the workflow that the action is .IsAutoAction which causes the action to be run automatically when the document is approved.

states
.Add<State.approved>(flowState => flowState
.WithActions(actions =>
{
actions.Add(g => g.putOnHold);
actions.Add(g => g.CheckReadyToProcess);
})

...

.WithFlowStates(states =>
{
states
.Update<State.approved>(flowState => flowState
.WithActions(actions =>
{
actions.Update(g => g.CheckReadyToProcess, a => a.IsAutoAction());
}));
})

In 23r1, this functionality is no longer supported in this manner.  Now that I am upgrading to 23r1, I need to find the new solution.  (IsAutoAction was removed per 23r1 Developer Release Notes)

Badge +11

Thanks for being the bleeding edge hero and writing the rest of us guides for how to do everything!

Userlevel 7
Badge +17

Hi @Brian Stevens  Have you found any solution that to add AutoRun Action for the existing actions based on the conditions? 

Userlevel 6
Badge +4

Hi @Brian Stevens  Have you found any solution that to add AutoRun Action for the existing actions based on the conditions? 

The example above works in 2022, but I have yet to figure out how to do this in 2023.  This has moved down my priority list for now, but I still need a solution.

Userlevel 7
Badge +17

Hi @Brian Stevens  Thanks for the update. I will check this in 23 R1 by the weekend and share the update if found any solution to this.

Userlevel 3
Badge +1

Any updates on 2023r1?

Userlevel 6
Badge +4

Any updates on 2023r1?

We ended up totally reworking the solution with business events and some extra fields that the sys admin could use to trigger the business events.  I never got any guidance on a direct replacement of this functionality via workflow.

  

Any updates on 2023r1?

We ended up totally reworking the solution with business events and some extra fields that the sys admin could use to trigger the business events.  I never got any guidance on a direct replacement of this functionality via workflow.

Hi, any chance you would be able to give some more details on how exactly you were able to make this work?

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved