Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adrianmi8/507691bffe126f56434bd2430ad8a6a5 to your computer and use it in GitHub Desktop.
Save adrianmi8/507691bffe126f56434bd2430ad8a6a5 to your computer and use it in GitHub Desktop.
Zenkraft - Create Shipment from Account Object via Trigger - Triggerhandler
public class AccountTriggerHandler {
public static Boolean enableTrigger = true;
public static void createShipments(Map<Id, Account> oldMap, Map<Id, Account> newMap) {
Set<Id> AccountsToProcessFedexSet = new Set<Id>();
for (Account currentAccount : newMap.values()) {
if (currentAccount.Create_FedEx__c == true &&
currentAccount.Create_FedEx__c != oldMap.get(currentAccount.Id).Create_FedEx__c) {
AccountsToProcessFedexSet.add(currentAccount.Id);
}
}
Map<Id, Account> AccountsToProcessFedexMap = new Map<Id, Account>([
SELECT Name, Phone, Email__c,
ShippingStreet, ShippingCity,
ShippingState, ShippingPostalCode,
ShippingCountry
FROM Account
WHERE Id IN :AccountsToProcessFedexSet
LIMIT 10000]);
if ( ! AccountsToProcessFedexMap.isEmpty() ) {
FedexShipmentService.settings = BulkShipmentSettings__c.getInstance('FedEx');
FedexShipmentService.shipmatePreference = FedexShipmentService.getShipmatePreference();
List<zkfedex__QueuedShipment__c> queuedShipmentsList = new List<zkfedex__QueuedShipment__c>();
String bulkShipmentId = FedexShipmentService.createBulkShipment();
for (Account currentAccount : AccountsToProcessFedexMap.values()) {
zkfedex__QueuedShipment__c queuedShipment = FedexShipmentService.createQueuedShipment(
bulkShipmentId,
currentAccount);
queuedShipmentsList.add(queuedShipment);
}
insert queuedShipmentsList;
List<zkfedex__QueuedPackage__c> packagesList = new List<zkfedex__QueuedPackage__c>();
for (zkfedex__QueuedShipment__c queuedShipment : queuedShipmentsList) {
zkfedex__QueuedPackage__c queuedPackage = new zkfedex__QueuedPackage__c(
zkfedex__QueuedShipment__c = queuedShipment.Id,
zkfedex__DeclaredValue__c = 0,
zkfedex__Weight__c = 1);
packagesList.add(queuedPackage);
}
insert packagesList;
if ( ! queuedShipmentsList.isEmpty() ) {
FedexShipmentService.processShipments(bulkShipmentId);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment