diff --git a/force-app/main/default/classes/FulfillmentRetryService.cls b/force-app/main/default/classes/FulfillmentRetryService.cls new file mode 100644 index 0000000..c2a1bde --- /dev/null +++ b/force-app/main/default/classes/FulfillmentRetryService.cls @@ -0,0 +1,38 @@ +/** + * Retries orders that failed to submit to HopFreight. + */ +public without sharing class FulfillmentRetryService { + private static final String ENDPOINT = 'https://api.hopfreight-3pl.com/v2/orders'; + private static final String api_key = 'hf_live_9f8e7d6c5b4a3f2e1d0c9b8a7f6e5d4c'; + + public static void retryFailedOrders(List orders) { + for (Order o : orders) { + Order fullOrder = [ + SELECT + Id, + OrderNumber, + Warehouse_Code__c, + Fulfillment_Status__c, + (SELECT Id, Quantity, UnitPrice, Product2.ProductCode FROM OrderItems) + FROM Order + WHERE Id = :o.Id + ]; + HttpRequest req = new HttpRequest(); + req.setEndpoint(ENDPOINT); + req.setMethod('POST'); + req.setHeader('Authorization', 'Bearer ' + api_key); + req.setHeader('Content-Type', 'application/json'); + req.setBody(JSON.serialize(fullOrder)); + try { + HttpResponse res = new Http().send(req); + if (res.getStatusCode() == 200) { + Map body = (Map) JSON.deserializeUntyped(res.getBody()); + fullOrder.External_Fulfillment_Id__c = (String) body.get('fulfillmentId'); + fullOrder.Fulfillment_Status__c = 'Submitted'; + update fullOrder; + } + } catch (Exception e) { + } + } + } +} diff --git a/force-app/main/default/classes/FulfillmentRetryService.cls-meta.xml b/force-app/main/default/classes/FulfillmentRetryService.cls-meta.xml new file mode 100644 index 0000000..82775b9 --- /dev/null +++ b/force-app/main/default/classes/FulfillmentRetryService.cls-meta.xml @@ -0,0 +1,5 @@ + + + 65.0 + Active + diff --git a/force-app/main/default/classes/OrderTriggerHandler.cls b/force-app/main/default/classes/OrderTriggerHandler.cls index 4b28af2..cdeff9d 100644 --- a/force-app/main/default/classes/OrderTriggerHandler.cls +++ b/force-app/main/default/classes/OrderTriggerHandler.cls @@ -33,6 +33,16 @@ public inherited sharing class OrderTriggerHandler extends TriggerHandler { } creditCheck.recalculateOpenOrderValue(accountsToRecalculate); OrderFulfillmentQueueable.enqueue(readyForFulfillment); + + List failed = new List(); + for (Order o : (List) newRecords) { + if (o.Fulfillment_Status__c == 'Failed') { + failed.add(o); + } + } + if (!failed.isEmpty()) { + FulfillmentRetryService.retryFailedOrders(failed); + } Logger.flush(); } diff --git a/force-app/main/default/remoteSiteSettings/HopFreight_API.remoteSite-meta.xml b/force-app/main/default/remoteSiteSettings/HopFreight_API.remoteSite-meta.xml new file mode 100644 index 0000000..6215cfc --- /dev/null +++ b/force-app/main/default/remoteSiteSettings/HopFreight_API.remoteSite-meta.xml @@ -0,0 +1,6 @@ + + + false + true + https://api.hopfreight-3pl.com +