Created
April 14, 2016 11:41
-
-
Save adrianmi8/7422de864b4c67d005181ea311401abf to your computer and use it in GitHub Desktop.
USPS - Mass Ship to Contacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class NewUSPSShipmentCaseController { | |
private ApexPages.StandardController standardController; | |
private Id bulkShipmentId; | |
private String batchId; | |
private static final String SERVICE_TYPE = 'USPS'; | |
private final BulkShipmentSettings__c settings; | |
public Id caseId {get; set;} | |
public Case currentCase {get; set;} | |
public String statusMessage {get;set;} | |
public static final String workingObject = 'Case'; | |
public Boolean startPoller { get; set; } | |
private Boolean validationComplete {get; set;} | |
private Boolean hasValidationErrors {get; set;} | |
private Boolean hasProcessingErrors {get; set;} | |
private String previousStatus {get; set;} | |
private static final String VALIDATION_COMPLETE = 'VALIDATION_COMPLETED'; | |
private static final String PROCESSING_COMPLETE = 'PROCESSING_COMPLETED'; | |
private static final String ERROR_STATUS = 'ERROR'; | |
private static final String SUCCESS_STATUS = 'SUCCESS'; | |
public NewUSPSShipmentCaseController(ApexPages.StandardController standardController) { | |
this.standardController = standardController; | |
this.caseId = standardController.getId(); | |
settings = BulkShipmentSettings__c.getInstance(SERVICE_TYPE); | |
hasValidationErrors = false; | |
hasProcessingErrors = false; | |
validationComplete = false; | |
} | |
public void processBulkShipment() { | |
createBulkShippingObjects(); | |
this.batchId = zkusps.BulkShipmentInterface.validateBulkShipment(this.bulkShipmentId); | |
startPoller = true; | |
this.statusMessage = 'Shipment creation process has been initiated.'; | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, this.statusMessage)); | |
} | |
public PageReference checkBatchStatus() { | |
hasValidationErrors = false; | |
if (validationComplete) { | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Shipment validation has been completed.')); | |
} | |
List<zkusps__BulkShipmentStatus__c> statuses = [ | |
SELECT zkusps__Status__c, zkusps__StatusMessage__c | |
FROM zkusps__BulkShipmentStatus__c | |
WHERE zkusps__BatchId__c = :this.batchId]; | |
if ( ! statuses.isEmpty() ) { | |
this.previousStatus = statuses.get(0).zkusps__Status__c; | |
if (previousStatus == ERROR_STATUS) { | |
this.statusMessage = statuses.get(0).zkusps__StatusMessage__c; | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, this.statusMessage)); | |
startPoller = false; | |
return null; | |
} else if(previousStatus == VALIDATION_COMPLETE ) { | |
validationComplete = true; | |
List<zkusps__QueuedShipmentStatus__c> errorsList = [ | |
SELECT zkusps__StatusMessage__c | |
FROM zkusps__QueuedShipmentStatus__c | |
WHERE zkusps__BatchId__c = :this.batchId AND | |
zkusps__Status__c = 'ERROR' | |
LIMIT 100]; | |
if ( ! errorsList.isEmpty() ) { | |
hasValidationErrors = true; | |
startPoller = false; | |
String errorMessage = ''; | |
for (zkusps__QueuedShipmentStatus__c err : errorsList) { | |
errorMessage = errorMessage + err.zkusps__StatusMessage__c + '<br/>'; | |
} | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Validation Error: ' + errorMessage)); | |
return null; | |
} | |
if ( ! hasValidationErrors ) { | |
this.batchId = zkusps.BulkShipmentInterface.processBulkShipment(this.bulkShipmentId); | |
} | |
} else if(previousStatus == PROCESSING_COMPLETE) { | |
List<zkusps__QueuedShipmentStatus__c> errorsList = [ | |
SELECT zkusps__StatusMessage__c | |
FROM zkusps__QueuedShipmentStatus__c | |
WHERE zkusps__BatchId__c = :this.batchId AND | |
zkusps__Status__c = 'ERROR' | |
LIMIT 100]; | |
if ( ! errorsList.isEmpty() ) { | |
hasValidationErrors = true; | |
startPoller = false; | |
String errorMessage = ''; | |
for (zkusps__QueuedShipmentStatus__c err : errorsList) { | |
errorMessage = errorMessage + err.zkusps__StatusMessage__c + '<br/>'; | |
} | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Creation Error: ' + errorMessage)); | |
return null; | |
} | |
if ( ! hasValidationErrors) { | |
return new PageReference('/' + this.caseId); | |
} | |
} | |
return null; | |
} else { | |
return null; | |
} | |
} | |
public void createBulkShippingObjects() { | |
List<zkusps__ShippingPreference__c> caseShipmateList = [ | |
SELECT zkusps__CompanyName__c, zkusps__ShippingCity__c, zkusps__preference__c, | |
zkusps__shippingSenderEmailDefault__c, zkusps__shippingSenderPhoneDefault__c, | |
zkusps__ShippingState__c, zkusps__ShippingStreet__c, zkusps__ShippingPostalCode__c, | |
zkusps__LabelImageTypeDefault__c | |
FROM zkusps__ShippingPreference__c | |
WHERE Name = :settings.ShipmatePreferenceName__c | |
LIMIT 1]; | |
if ( ! caseShipmateList.isEmpty() ) { | |
// 1. zkusps__BulkShipment__c object | |
zkusps__BulkShipment__c bulkShipment = new zkusps__BulkShipment__c ( | |
zkusps__BillingPreference__c = caseShipmateList.get(0).zkusps__preference__c | |
); | |
insert bulkShipment; | |
// save id | |
this.bulkShipmentId = bulkShipment.Id; | |
List<zkusps__CustomAddressSource__c> customAddressSourceList = [ | |
SELECT Id | |
FROM zkusps__CustomAddressSource__c | |
WHERE zkusps__CustomAddressSourceObject__c = :workingObject | |
LIMIT 1]; | |
currentCase = [ | |
SELECT Is_Residential__c, Recipient_City__c, Recipient_Company_Name__c, | |
Recipient_Country__c, Recipient_Email__c, Recipient_Name__c, | |
Recipient_Phone__c, Recipient_State__c, Recipient_Street__c, | |
Recipient_Postal_Code__c | |
FROM Case | |
WHERE Id = :caseId]; | |
if ( ! customAddressSourceList.isEmpty() ) { | |
// 2. zkusps__QueuedShipment__c object | |
zkusps__QueuedShipment__c queuedShipment = new zkusps__QueuedShipment__c ( | |
zkusps__BulkShipment__c = bulkShipment.Id, | |
zkusps__LabelImageType__c = caseShipmateList.get(0).zkusps__LabelImageTypeDefault__c, | |
zkusps__ServiceType__c = 'Domestic: Express', | |
zkusps__PackagingType__c = settings.DefaultPackagingType__c, | |
// sender info | |
zkusps__SenderName__c = caseShipmateList.get(0).zkusps__CompanyName__c, | |
zkusps__SenderCity__c = caseShipmateList.get(0).zkusps__ShippingCity__c, | |
zkusps__SenderCompany__c = caseShipmateList.get(0).zkusps__CompanyName__c, | |
zkusps__SenderEmail__c = caseShipmateList.get(0).zkusps__shippingSenderEmailDefault__c, | |
zkusps__SenderPhone__c = caseShipmateList.get(0).zkusps__shippingSenderPhoneDefault__c, | |
zkusps__SenderState__c = caseShipmateList.get(0).zkusps__ShippingState__c, | |
zkusps__SenderStreet__c = caseShipmateList.get(0).zkusps__ShippingStreet__c, | |
zkusps__SenderPostalCode__c = caseShipmateList.get(0).zkusps__ShippingPostalCode__c, | |
// recipient info | |
// you can set it from custom address source or from case directly | |
zkusps__RecipientCity__c = currentCase.Recipient_City__c, | |
zkusps__RecipientCompany__c = currentCase.Recipient_Company_Name__c, | |
zkusps__RecipientCountry__c = 'US', | |
zkusps__RecipientEmail__c = currentCase.Recipient_Email__c, | |
zkusps__RecipientName__c = currentCase.Recipient_Name__c, | |
zkusps__RecipientPhone__c = currentCase.Recipient_Phone__c, | |
zkusps__RecipientState__c = currentCase.Recipient_State__c, | |
zkusps__RecipientStreet__c = currentCase.Recipient_Street__c, | |
zkusps__RecipientPostalCode__c = currentCase.Recipient_Postal_Code__c, | |
zkusps__CustomAddressSource__c = customAddressSourceList.get(0).id, | |
zkusps__CustomAddressSourceRecordId__c = caseId); | |
insert queuedShipment; | |
// 3. zkusps__QueuedPackage__c object | |
zkusps__QueuedPackage__c queuedPackage = new zkusps__QueuedPackage__c ( | |
zkusps__DeclaredValue__c = settings.DefaultDeclaredValue__c, | |
zkusps__WeightPounds__c = settings.DefaultWeight__c, | |
zkusps__QueuedShipment__c = queuedShipment.Id | |
); | |
insert queuedPackage; | |
} else { | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ' Invalid Custom Address Source. ')); | |
} | |
} else { | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ' Invalid Preference. ')); | |
} | |
} | |
private Map<String, String> serviceTypeMapping = new Map<String, String>{ | |
'Express' => 'Domestic: Express', | |
'Priority' => 'Domestic: Priority' | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment