Automating Purchase Order Cancellations in Oracle Fusion Procurement Using Python
Managing a high volume of purchase orders (POs) can be challenging, especially when tasks like mass Purchase Order Cancellations in Oracle Fusion Procurement are required. In my recent project, I faced the task of cancelling around 450 POs. Doing it manually would have been time-consuming, so I developed a Python script to automate this process efficiently. This solution saved substantial time and effort, and it was robust enough to handle multiple POs with error handling and delay management. Here’s how it works.ย ย
Initial Testing with Postman
Before writing the code, I used Postman to test the API endpoints, the credentials, and the payload required for cancelling a PO. This allowed me to verify that the API behaved as expected with each request and that the payload format was correct.
Exploring a career path in Oracle Fusion Procurement?
Get started with our FREE Oracle Fusion Procurement Training
Python Script for Bulk Purchase Order Cancellations in Oracle Fusion Procurementย
The script uses Python’s requests library to perform HTTP POST and GET requests. First, it sends a POST request to initiate the cancellation of each PO. It then sends a GET request to retrieve and verify the cancellation statuses, ensuring that all purchase order cancellations in Oracle Fusion Procurement were successful. Below is the code snippet:ย ย
Python
Copy code
import time
import requests
from requests.auth import HTTPBasicAuth
# Fusion Cloud credentials
username = "your_usernameโ
password = "your_password"
# List of PO header IDs to cancel
po_header_ids = [
33392, 33393, 33394, 33395, 33396, 33420, 33421, 33422, # ... add more as needed
]
# Base URL for the API endpoint
base_url = "https://your-instance-url/fscmRestApi/resources/11.13.18.05/purchaseOrders/"
# Payload for cancelling the purchase order
cancel_payload = {
"name": "cancel",
"parameters": [
{"cancellationReason": "No longer required"},
{"initiatingParty": "buyer"}
]
}
# Headers with the updated Content-Type
headers = {
"Content-Type": "application/vnd.oracle.adf.action+json"
}
# Step 1: Cancel each PO with a delay between requests
for po_id in po_header_ids:
url = f"{base_url}{po_id}"
response = requests.post(
url,
json=cancel_payload,
auth=HTTPBasicAuth(username, password),
headers=headers
)
if response.status_code == 200:
print(f"Purchase order {po_id} cancelled successfully.")
else:
print(f"Failed to cancel purchase order {po_id}. Status code: {response.status_code}")
print("Response:", response.text)
# Delay to avoid hitting rate limits
time.sleep(10)
# Step 2: Verify cancellation status for each PO
for po_id in po_header_ids:
status_url = f"{base_url}{po_id}"
status_response = requests.get(
status_url,
auth=HTTPBasicAuth(username, password),
headers={"Content-Type": "application/json"}
)
if status_response.status_code == 200:
po_status = status_response.json().get("Status", "Unknown")
print(f"Purchase order {po_id} status: {po_status}")
else:
print(f"Failed to retrieve status for purchase order {po_id}. Status code: {status_response.status_code}")
print("Response:", status_response.text)
How the Script Worksย for Purchase Order Cancellations in Oracle Fusion Procurement
- Cancelling Each Purchase Order (POST Request): For each PO ID in the list, the script sends a POST request to initiate the cancellation. The payload specifies the cancellation reason as “No longer required” and identifies the initiating party as “buyer.”
- Delay Between Requests: A 10-second delay is added between requests to manage server load and avoid hitting rate limits.
- Verifying the Cancellation Status (GET Request): After attempting to cancel all POs, the script sends a GET request for each PO to retrieve its current status. This confirms that each PO has indeed been cancelled.
Key Takeaways
- Automation Efficiency: This approach allowed for quick processing of a large number of POs without manual intervention.
- Error Handling and Verification: The script includes checks to handle errors in case a PO cancellation fails. It also verifies the status afterwards to ensure that each cancellation was successful.
- Scalability: With slight modifications, the script could handle even larger volumes or different types of operations, such as updates or status checks on other procurement objects.
This solution not only streamlined the Purchase Order Cancellations in Oracle Fusion Procurement but also minimised the chances of human error, making it a practical tool for Procurement tasks.ย ย