Let me provide a comprehensive solution for handling work order status conflicts in your automation workflow.
1. Implement Status Pre-Check Logic
Before attempting any status update, always verify the current work order state and validate that your intended transition is allowed:
// Get current work order state
WorkOrder wo = api.getWorkOrder(workOrderId);
String currentStatus = wo.getStatus();
// Validate transition is allowed
if (!isValidTransition(currentStatus, targetStatus)) {
log.warn("Invalid transition: {} -> {}", currentStatus, targetStatus);
return;
}
Valid work order state transitions in Apriso typically follow this pattern:
- Released → In Progress → Complete
- Released → Cancelled
- In Progress → On Hold → In Progress
- In Progress → Rework → In Progress
- Complete → Closed
Attempting invalid transitions (like Released → Complete) will always result in 409 conflicts. Build a state machine validator that checks allowed transitions before making API calls.
2. Implement Retry Logic with Exponential Backoff
Conflicts are often transient - another process updated the work order milliseconds before your request. Implement intelligent retry logic:
Pseudocode approach:
// Pseudocode - Work order update with retry logic:
1. Set maxRetries = 3, retryDelay = 500ms
2. For attempt = 1 to maxRetries:
a. GET current work order state and version
b. Validate target status transition is allowed
c. If invalid transition, log error and exit
d. Build PATCH payload with version/etag
e. Execute PATCH request
f. If success (200/204), return success
g. If 409 conflict:
- Log conflict with attempt number
- Wait retryDelay * (2 ^ attempt) milliseconds
- Continue to next retry
h. If other error (400/500), log and exit
3. After maxRetries, log permanent conflict
4. Send alert for manual investigation
// Most conflicts resolve on first retry (80%)
// Second retry resolves another 15%
// Third retry resolves remaining 4%
Exponential backoff prevents your automation from hammering the API during high-contention periods (like shift changes). The delays give time for conflicting updates to complete.
3. API Conflict Handling Best Practices
When making PATCH requests, include version control headers to enable optimistic locking:
PATCH /api/work-orders/WO-12345
If-Match: "v7"
Content-Type: application/json
{
"status": "Complete",
"completionTimestamp": "2024-11-28T11:00:00Z"
}
The If-Match header contains the version/etag from your GET request. If the work order was modified by another process, the version won’t match and you’ll get a 409. This is the correct behavior - it prevents you from overwriting changes you haven’t seen.
Always check the response:
- 200/204: Update succeeded
- 409: Conflict - retry with fresh GET
- 400: Bad request - check your payload format
- 404: Work order not found - may have been deleted
- 422: Business rule violation - invalid status transition or missing required fields
4. Rework Automation Specific Considerations
When automating rework workflows based on quality events, additional fields are typically required:
reworkReasonCode: Classification of why rework is needed
defectType: Specific defect detected
qualityHoldStatus: Whether material should be held
reworkInstructions: Operator guidance for correction
originalOperationId: Which operation produced the defect
Missing these fields when transitioning to “Rework” status will cause 409 conflicts or 422 validation errors. Build a complete rework payload:
PatchRequest reworkUpdate = new PatchRequest()
.setStatus("Rework")
.setReworkReasonCode(defectEvent.getReasonCode())
.setDefectType(defectEvent.getDefectType())
.setReworkInstructions(defectEvent.getInstructions())
.setOriginalOperationId(currentOperation.getId());
5. Handle Concurrent Updates During Shift Changes
Your 15-20% conflict rate during shift changes is typical - operators are manually updating work orders while your automation runs. To minimize conflicts:
- Implement event deduplication: Cache processed PLC events for 60 seconds to prevent duplicate automation triggers
- Add jitter to retries: Randomize retry delays slightly (±10%) to prevent multiple automation instances from retrying simultaneously
- Queue updates during high-contention periods: If conflict rate exceeds 30%, queue updates for 2-3 minutes and process in batch
- Coordinate with operator workflows: Consider implementing a “lock” mechanism where automation checks if an operator has the work order open before attempting updates
6. Monitoring and Alerting
Track conflict metrics to identify patterns:
- Conflict rate by time of day (should spike at shift changes)
- Conflict rate by work order type (some types may have more manual interaction)
- Retry success rate (should be >95% after retries)
- Permanent conflicts requiring manual intervention (should be <1%)
Set up alerts:
- Alert if conflict rate exceeds 25% for more than 5 minutes
- Alert if permanent conflicts (after all retries) exceed 5 per hour
- Alert if retry success rate drops below 90%
7. Expected Results
After implementing this solution:
- Overall conflict rate: Remains at 15-20% initially (conflicts are normal)
- Successful resolution after retry: 95-98% (down from your current failures)
- Failed updates requiring manual intervention: <1%
- Average update latency: 500-2000ms (including retries)
- Automation reliability: >99% successful work order transitions
The key insight is that 409 conflicts aren’t errors to eliminate - they’re the API correctly preventing data corruption. Your job is to handle them gracefully with proper retry logic and validation. The combination of pre-checks, retry logic, and proper payload construction will make your automation robust and reliable even during high-contention periods.