Skip to main content
Solved

How to approve a PO via code


In order to create test data I need to be able to create and Approve POs via code. Eventually I will use this in a Customization Plugin for populating the instance with documents to run Integration tests against. Right now I’m just trying to create 1 PO with 1 line item and get it to the open state. Creating the PO and removing the PO from hold has been pretty straightforward, the piece I’m having difficulty with is Approving the PO.

Here is my test method which I’m trying to get to work.

        public PXAction<ISPSOutboundPOLog> createPosTest;
PXUIField(DisplayName = "Create POs Test", MapEnableRights =
PXCacheRights.Update, MapViewRights = PXCacheRights.Update)]
PXButton]
public virtual IEnumerable CreatePosTest(PXAdapter adapter)
{
var PoOrderEntryGraph = PXGraph.CreateInstance<POOrderEntry>();

// Create a new Purchase Order header
POOrder newPO = new POOrder
{
OrderType = POOrderType.RegularOrder,
VendorID = 6995, // AAVENDOR
OrderDesc = "New Purchase Order",
OrderDate = PXTimeZoneInfo.Now,
ShipVia = "LOCAL"
};
newPO = PoOrderEntryGraph.Document.Insert(newPO);

// Create a line item for the Purchase Order
POLine newLine = new POLine
{
InventoryID = 692, // AALEGO500
OrderQty = 7,
UOM = "EA"
};

// Insert the line item into the Purchase Order
newLine = PoOrderEntryGraph.Transactions.Insert(newLine);

// Associate the line item with the Purchase Order
if (newLine != null)
{
newLine.OrderNbr = newPO.OrderNbr;
newLine = PoOrderEntryGraph.Transactions.Update(newLine);
}

// release the PO from hold
PoOrderEntryGraph.releaseFromHold.Press();

// Approve the PO tTHIS IS THE PART GIVING ME TROUBLE]
newPO.Status = POOrderStatus.Open;
newPO.Approved = true;
PoOrderEntryGraph.Document.Update(newPO);
PoOrderEntryGraph.Actions.PressSave();

return adapter.Get();
}

Everything works as expected until I try to Approve the PO and update the document. Specifically, when PoOrderEntryGraph.Document.Update(newPO) is called, the following popup appears.

 

 

 

When I click YES or NO the PXAction starts all over from the beginning and that popup window appears again. This goes on forever until I click the exit button. From what I’ve read online and observed debugging the code, it seems the exception that is triggering the popup window is also causing the PXAction to restart.

 

If I remove the line item the prompt goes away and the PO is created and moved into the Open status correctly. This makes since as the popup window is specifically about changing the lines dates. But I need line items on the PO so that is not a viable solution. Furthermore I want to run this code on Publish via a Customization Plugin, which currently causes an error since it expects user input and that seem to not be allowed when publishing.

 

Solutions I’ve tried and failed(I might have implemented them incorrectly since most of this is new to me)

  1. I tried using the PoOrderEntryGraph.Approval.Approve(newPO); function, this only still has the same issue of the popup window but it doesn’t actually change the POs status to Open even if it does run.
  2. I tried making it a long running process since I thought maybe that would allow me to skip the popup window. Nope.
  3. I tried changing the values directly via the cache to avoid events, the popup window still came up though.
  4. I tried setting the POOrder’s Status = POOrderStatus.Open, Hold = false and Approved = true. But this only created the PO in a “Pending Approval” state not “Open” which I am trying to get it to

​Any advice or solutions would be greatly appreciated!

Thanks,

Philip Engesser

6 replies

Badge +12

Have you tried using the Approve action button (assuming there is one)?

Something like PoOrderEntryGraph.approve.Press()

 

Haven't tested; did not look at code

Userlevel 4
Badge +1

@darylbowman great idea, thanks for the suggestion!

Unfortunately I have already tried that and I couldn't find an Approve button on the POOrderEntry graph. Trying to press the Approve button was actually the first thing I tried since it was so easy to remove the PO from hold that way(I just forgot to mention it in the list of things I had tried lol).

Badge +12

Took the time to look into the code and found that rather than defining a code action, they are instead using workflow to change the ‘Approved’ field to true:

 

However, farther down, there is the actual state-changing code:

 

It seems like the only way to use this properly is to click the workflow action, which I don’t know how to do, unfortunately 😒

 

Userlevel 4
Badge +1

@darylbowman thanks for looking into that, at least that explains why it is different from releasing the PO from hold.

After doing some more debugging I’ve found that even if I comment out the approval code I still get the same popup window loop, I thought I had checked that I’m super sorry! 

The issue seems to be related to calling PoOrderEntryGraph.Document.Update(newPO); after calling PoOrderEntryGraph.releaseFromHold.Press();

Which leads me to believe that maybe my PO(or perhaps line items) are out of whack after calling releaseFromHold so when I try to update the PO I get that popup window. I’ll keep debugging and provide an update with what I find out.

Userlevel 4
Badge +1

I seem to have figured it out!

To simulate clicking the POOrderEntry Approve button via code you can use the fallowing line

PoOrderEntryGraph.Actions["Approve"].Press();

Where PoOrderEntryGraph is an instance of the POOrderEntry graph.

A big thank you to @darylbowman for finding out that the Approve button was tied to a Workflow Action!(which is why I couldn't press it in the same way as the Remove Hold button).

A couple notes from my tests which might be useful to anyone who stumbles upon this. 

  1. I still remove the PO from hold before trying to call the Approve action, otherwise the Approve action does not do anything. So make sure your PO is in the correct state to be approved.
  2. The name of the command which you pass in as a string is case insensitive, Approve and apprOVE both worked. However, if you input a name that is not a command an error will be thrown.
  3. You can Reject the PO in the same way, just input the action name of the reject button. This leads me to believe that using graphObject.Actions[“{Name of Action}”].Press(); is a way to call Workflow Actions in general. Although I haven’t tested this very much so I’m not sure.
Badge +12

Funny enough, I tried that, because that is a way to click Actions, but it didn’t work for me. It must not have been quite in the right place.

Reply