Table of Contents

  1. UPS Mass Ship to Contacts
  2. One Click Ship to a Case Object 

Before writing APEX code, please install the ShipAutomate package here

1. UPS Mass Ship to Contacts

VisualForce Page

<apex:page standardController="Contact" extensions="NewUPSShipmentsContactCtrl"
recordSetVar="Contact" action="{! processBulkShipment }">
    <apex:form >
    <apex:outputPanel id="blockId">
    <apex:pageMessages escape="false"/>
       <apex:image url="/img/loading32.gif" rendered="{! startPoller }"/>
       <apex:outputLink value="/003" rendered="{! NOT(startPoller) }">Back to Contacts</apex:outputLink>
       <apex:actionPoller action="{! checkBatchStatus }" reRender="blockId" interval="5"
        enabled="{!startPoller}"/>
    </apex:outputPanel>
    </apex:form>    
</apex:page>

Apex Controller

public with sharing class NewUPSShipmentsContactCtrl {
    private ApexPages.StandardSetController standardController;
    private Id bulkShipmentId;
    private String batchId;
    private static final String SERVICE_TYPE = 'UPS';
    private final BulkShipmentSettings__c settings;  
    private Set<Id> contactsIds;

public String statusMessage {get; set;}
    public static final String workingObject = 'Contact';
    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 NewUPSShipmentsContactCtrl(ApexPages.StandardSetController controller) {
    standardController = controller;
    contactsIds = new Set<Id>();
    for (sObject c : standardController.getSelected()) {
    contactsIds.add(c.Id);
    }

  settings = BulkShipmentSettings__c.getInstance(SERVICE_TYPE);
    hasValidationErrors = false;
    hasProcessingErrors = false;
    validationComplete = false;
    }

    public void processBulkShipment() {
        createBulkShippingObjects();
       
        this.batchId = zkups.BulkShipmentInterface.validateBulkShipment(this.bulkShipmentId, true);
        startPoller = true;
        this.statusMessage = 'Shipments 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, 'Shipments validation has been completed.'));
        }

        if (Test.isRunningTest()) {
        zkups__BulkShipmentStatus__c status = new zkups__BulkShipmentStatus__c(
        zkups__BatchId__c = this.batchId,
        zkups__Status__c = VALIDATION_COMPLETE,
        zkups__BulkShipment__c = this.bulkShipmentId);
        insert status;
        }

List<zkups__BulkShipmentStatus__c> statuses = [
SELECT zkups__Status__c, zkups__StatusMessage__c
FROM zkups__BulkShipmentStatus__c
WHERE zkups__BatchId__c = :this.batchId
ORDER BY CreatedDate DESC
LIMIT 1];

        if ( ! statuses.isEmpty() ) {
       this.previousStatus = statuses.get(0).zkups__Status__c;

       if (previousStatus == ERROR_STATUS) {
        this.statusMessage = statuses.get(0).zkups__StatusMessage__c;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, this.statusMessage));
startPoller = false;
        return null;
       } else if (previousStatus == VALIDATION_COMPLETE ) {
           validationComplete = true;

           List<zkups__QueuedShipmentStatus__c> errorsList = [
            SELECT zkups__StatusMessage__c
            FROM zkups__QueuedShipmentStatus__c
            WHERE zkups__BatchId__c = :this.batchId AND
             zkups__Status__c = 'ERROR'
            LIMIT 100];

           if ( ! errorsList.isEmpty() ) {
            hasValidationErrors = true;
            startPoller = false;
            String errorMessage = '';
            for (zkups__QueuedShipmentStatus__c err : errorsList) {
            errorMessage = errorMessage + err.zkups__StatusMessage__c + '<br/>';
            }
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Validation Error: ' + errorMessage));
            return null;
           }

           if ( ! hasValidationErrors) {
            this.batchId = zkups.BulkShipmentInterface.processBulkShipment(this.bulkShipmentId);
       if (Test.isRunningTest()) {
        zkups__BulkShipmentStatus__c status = new zkups__BulkShipmentStatus__c(
        zkups__BatchId__c = this.batchId,
        zkups__Status__c = PROCESSING_COMPLETE,
        zkups__BulkShipment__c = this.bulkShipmentId);
        insert status;
       }
           }
       } else if (previousStatus == PROCESSING_COMPLETE) {
        if (Test.isRunningTest()) {
        zkups__QueuedShipment__c queuedShipment = [SELECT Id FROM zkups__QueuedShipment__c LIMIT 1];
        zkups__QueuedShipmentStatus__c status = new zkups__QueuedShipmentStatus__c(
        zkups__BatchId__c = this.batchId,
        zkups__Status__c = 'SUCCESS',
        zkups__QueuedShipment__c =  queuedShipment.Id
        );
        insert status;
        }
List<zkups__QueuedShipmentStatus__c> messagesList = [
            SELECT zkups__Status__c, zkups__StatusMessage__c
            FROM zkups__QueuedShipmentStatus__c
            WHERE zkups__BatchId__c = :this.batchId
            LIMIT 100];

           if ( ! messagesList.isEmpty() ) {
            if (messagesList.get(0).zkups__Status__c == 'ERROR') {
            startPoller = false;
            String errorMessage = messagesList.get(0).zkups__StatusMessage__c;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Creation Error: ' + errorMessage));
            return null;
            } else if (messagesList.get(0).zkups__Status__c == 'SUCCESS') {
            return new PageReference('/003');
            }
           }        
       }
        }
        return null;
    }

    public void createBulkShippingObjects() {
        List<zkups__UPSShipmatePreference__c> shipmateList = [
                SELECT zkups__CompanyName__c, zkups__ShippingIsResidential__c, zkups__ShippingCity__c,
                  zkups__ShippingCountry__c, zkups__SenderEMailDefault__c, zkups__SenderPhoneDefault__c,
                  zkups__ShippingState__c, zkups__ShippingStreet__c, zkups__ShippingPostalCode__c,
                  zkups__LabelImageTypeDefault__c
                FROM zkups__UPSShipmatePreference__c
                WHERE Name = :settings.ShipmatePreferenceName__c
                LIMIT 1];

        if ( ! shipmateList.isEmpty() ) {
            // 1. zkups__BulkShipment__c object
            zkups__BulkShipment__c bulkShipment = new zkups__BulkShipment__c (
                zkups__ShipmatePreference__c = shipmateList.get(0).Id
            );
            insert bulkShipment;

            // save id
            this.bulkShipmentId = bulkShipment.Id;

            List<Contact> currentContacts = [
            SELECT MailingCity, Account.Name, MailingCountry,
              Email, FirstName, LastName,
              Phone, MailingState, MailingStreet,
              MailingPostalCode
            FROM Contact
            WHERE Id IN :contactsIds
            LIMIT 20000];

            // 2. zkups__QueuedShipment__c object
            List<zkups__QueuedShipment__c> queuedShipmentsToInsert = new List<zkups__QueuedShipment__c>();
            for (Contact con : currentContacts) {
           zkups__QueuedShipment__c queuedShipment = new zkups__QueuedShipment__c (
               zkups__BulkShipment__c = bulkShipment.Id,
               zkups__LabelImageType__c = shipmateList.get(0).zkups__LabelImageTypeDefault__c,
               zkups__ServiceType__c = 'Domestic: UPS Ground',
               zkups__PackagingType__c = settings.DefaultPackagingType__c,
               zkups__WeightDimensionUnits__c = settings.DimensionUnits__c,
               
               // sender info
               zkups__SenderName__c = shipmateList.get(0).zkups__CompanyName__c,
               zkups__SenderIsResidential__c = shipmateList.get(0).zkups__ShippingIsResidential__c,
               zkups__SenderCity__c = shipmateList.get(0).zkups__ShippingCity__c,
               zkups__SenderCompany__c = shipmateList.get(0).zkups__CompanyName__c,
               zkups__SenderCountry__c = shipmateList.get(0).zkups__ShippingCountry__c,
               zkups__SenderEmail__c = shipmateList.get(0).zkups__SenderEMailDefault__c,
               zkups__SenderPhone__c = shipmateList.get(0).zkups__SenderPhoneDefault__c,
               zkups__SenderState__c = shipmateList.get(0).zkups__ShippingState__c,
               zkups__SenderStreet__c = shipmateList.get(0).zkups__ShippingStreet__c,
               zkups__SenderPostalCode__c = shipmateList.get(0).zkups__ShippingPostalCode__c,
               
               // recipient info
               zkups__RecipientIsResidential__c = false,
               zkups__RecipientCity__c = con.MailingCity,
               zkups__RecipientCompany__c = con.Account.Name,
               zkups__RecipientCountry__c = con.MailingCountry,
               zkups__RecipientEmail__c = con.Email,
               zkups__RecipientName__c = con.FirstName + ' ' + con.LastName,
               zkups__RecipientPhone__c = con.Phone,
               zkups__RecipientState__c = con.MailingState,
               zkups__RecipientStreet__c = con.MailingStreet,
               zkups__RecipientPostalCode__c = con.MailingPostalCode,
               zkups__PaymentType__c = settings.PaymentType__c,
               zkups__Contact__c = con.Id
           );

queuedShipmentsToInsert.add(queuedShipment);
            }
            insert queuedShipmentsToInsert;

            // 3. zkups__QueuedPackage__c object
            List<zkups__QueuedPackage__c> queuedPackagesToInsert = new List<zkups__QueuedPackage__c>();

            for (zkups__QueuedShipment__c qs : queuedShipmentsToInsert) {
           zkups__QueuedPackage__c queuedPackage = new zkups__QueuedPackage__c (
               zkups__DeclaredValue__c = settings.DefaultDeclaredValue__c,
               zkups__Weight__c = settings.DefaultWeight__c,
               zkups__QueuedShipment__c = qs.Id          
           );
           queuedPackagesToInsert.add(queuedPackage);
            }

            insert queuedPackagesToInsert;                
        } else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ' Invalid Shipmate Preferences. '));
        }    
    }

    private Map<String, String> serviceTypeMapping = new Map<String, String>{
        'Next Day Air Early AM' =>'Domestic: UPS Next Day Air Early A.M.',
        'Next Day Air' => 'Domestic: UPS Next Day Air',
        'Next Day Air Saver' => 'Domestic: UPS Next Day Air Saver',
        'Second Day Air AM' => 'Domestic: UPS 2nd Day Air A.M.',
        'Second Day Air' => 'Domestic: UPS 2nd Day Air',
        'Three Day Select' => 'Domestic: UPS 3 Day Select',
        'Ground' => 'Domestic: UPS Ground'
    };
}

Apex Controller Test

@isTest
private class NewUPSShipmentsContactCtrlTest {
    @isTest
    public static void testShipment_Success() {
    Account testAccount = new Account();
testAccount.Name = 'testAccount';
insert testAccount;

List<Contact> contactRecordList = new List<Contact>();
Contact contactRecord = new Contact();
contactRecord.AccountId = testAccount.Id;
contactRecord.MailingCity = 'Test City';
contactRecord.MailingCountry = 'US';
contactRecord.Email = 'test@zenkraft.com';
contactRecord.FirstName = 'John';
contactRecord.LastName = 'Doe';
contactRecord.Phone = '12312312300';
contactRecord.MailingState = 'CA';
contactRecord.MailingStreet = 'Test Street 15';
contactRecord.MailingPostalCode = '90001';
insert contactRecord;

zkups__UPSShipmatePreference__c shipmatePreference = new zkups__UPSShipmatePreference__c();
insert shipmatePreference;

String preferenceName = [
SELECT Name
FROM zkups__UPSShipmatePreference__c
WHERE id = :shipmatePreference.Id
].Name;

BulkShipmentSettings__c bulkShipmentSettings = new BulkShipmentSettings__c();
bulkShipmentSettings.Name = 'UPS';
bulkShipmentSettings.ShipmatePreferenceName__c = preferenceName;
insert bulkShipmentSettings;

List<Contact> selectedContactsList = new List<Contact>{contactRecord};

    Test.startTest();

    ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(selectedContactsList);
    stdSetController.setSelected(selectedContactsList);

       NewUPSShipmentsContactCtrl ctrl = new NewUPSShipmentsContactCtrl(stdSetController);
       ctrl.createBulkShippingObjects();

       ctrl.processBulkShipment();
       ctrl.checkBatchStatus();
       ctrl.checkBatchStatus();
       zkups__QueuedShipmentStatus__c successStatuses = [
SELECT zkups__Status__c
FROM zkups__QueuedShipmentStatus__c
WHERE zkups__Status__c = 'SUCCESS'];
       System.assertEquals('SUCCESS',successStatuses.zkups__Status__c);

        Test.stopTest();
    }

}

2. One Click Ship to a Case Object

VisualForce Page

<apex:page standardController="Case" extensions="NewUPSShipmentCaseController" action="{! processBulkShipment }">
    <apex:form >
    <apex:outputPanel id="blockId">
    <apex:pageMessages escape="false"/>
       <apex:image url="/img/loading32.gif" rendered="{! startPoller }"/>
       <apex:outputLink value="/{! caseId }" rendered="{! NOT(startPoller) }">Back to Case</apex:outputLink>
       <apex:actionPoller action="{! checkBatchStatus }" reRender="blockId" interval="5"
        enabled="{!startPoller}"/>
    </apex:outputPanel>
    </apex:form>    
</apex:page>

Apex Controller

public with sharing class NewUPSShipmentCaseController {
    private ApexPages.StandardController standardController;
    private Id bulkShipmentId;
    private String batchId;
    private static final String SERVICE_TYPE = 'UPS';
    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 NewUPSShipmentCaseController(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 = zkups.BulkShipmentInterface.validateBulkShipment(this.bulkShipmentId, true);
        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.'));
        }

        if (Test.isRunningTest()) {
        zkups__BulkShipmentStatus__c status = new zkups__BulkShipmentStatus__c(
        zkups__BatchId__c = this.batchId,
        zkups__Status__c = VALIDATION_COMPLETE,
        zkups__BulkShipment__c = this.bulkShipmentId);
        insert status;
        }

List<zkups__BulkShipmentStatus__c> statuses = [
SELECT zkups__Status__c, zkups__StatusMessage__c
FROM zkups__BulkShipmentStatus__c
WHERE zkups__BatchId__c = :this.batchId];

        if ( ! statuses.isEmpty() ) {
       this.previousStatus = statuses.get(0).zkups__Status__c;

       if (previousStatus == ERROR_STATUS) {
        this.statusMessage = statuses.get(0).zkups__StatusMessage__c;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, this.statusMessage));
startPoller = false;
        return null;
       } else if(previousStatus == VALIDATION_COMPLETE ) {
           validationComplete = true;

           List<zkups__QueuedShipmentStatus__c> errorsList = [
            SELECT zkups__StatusMessage__c
            FROM zkups__QueuedShipmentStatus__c
            WHERE zkups__BatchId__c = :this.batchId AND
             zkups__Status__c = 'ERROR'
            LIMIT 100];

           if ( ! errorsList.isEmpty() ) {
            hasValidationErrors = true;
            startPoller = false;
            String errorMessage = '';
            for (zkups__QueuedShipmentStatus__c err : errorsList) {
            errorMessage = errorMessage + err.zkups__StatusMessage__c + '<br/>';
            }
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Validation Error: ' + errorMessage));
            return null;
           }

           if ( ! hasValidationErrors ) {
            this.batchId = zkups.BulkShipmentInterface.processBulkShipment(this.bulkShipmentId);
            if (Test.isRunningTest()) {
        zkups__BulkShipmentStatus__c status = new zkups__BulkShipmentStatus__c(
        zkups__BatchId__c = this.batchId,
        zkups__Status__c = PROCESSING_COMPLETE,
        zkups__BulkShipment__c = this.bulkShipmentId);
        insert status;
       }
           }
       } else if(previousStatus == PROCESSING_COMPLETE) {
        if (Test.isRunningTest()) {
        zkups__QueuedShipment__c queuedShipment = [SELECT Id FROM zkups__QueuedShipment__c LIMIT 1];
        zkups__QueuedShipmentStatus__c status = new zkups__QueuedShipmentStatus__c(
        zkups__BatchId__c = this.batchId,
        zkups__Status__c = 'SUCCESS',
        zkups__QueuedShipment__c =  queuedShipment.Id
        );
        insert status;
        }
List<zkups__QueuedShipmentStatus__c> errorsList = [
            SELECT zkups__StatusMessage__c
            FROM zkups__QueuedShipmentStatus__c
            WHERE zkups__BatchId__c = :this.batchId AND
             zkups__Status__c = 'ERROR'
            LIMIT 100];

           if ( ! errorsList.isEmpty() ) {
            hasValidationErrors = true;
            startPoller = false;
            String errorMessage = '';
            for (zkups__QueuedShipmentStatus__c err : errorsList) {
            errorMessage = errorMessage + err.zkups__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<zkups__UPSShipmatePreference__c> caseShipmateList = [
                SELECT zkups__CompanyName__c, zkups__ShippingIsResidential__c, zkups__ShippingCity__c,
                  zkups__ShippingCountry__c, zkups__SenderEMailDefault__c, zkups__SenderPhoneDefault__c,
                  zkups__ShippingState__c, zkups__ShippingStreet__c, zkups__ShippingPostalCode__c,
                  zkups__LabelImageTypeDefault__c
                FROM zkups__UPSShipmatePreference__c
                WHERE Name = :settings.ShipmatePreferenceName__c
                LIMIT 1];

        if ( ! caseShipmateList.isEmpty() ) {
            // 1. zkups__BulkShipment__c object
            zkups__BulkShipment__c bulkShipment = new zkups__BulkShipment__c (
                zkups__ShipmatePreference__c = caseShipmateList.get(0).Id
            );
            insert bulkShipment;

            // save id
            this.bulkShipmentId = bulkShipment.Id;

            List<zkups__CustomAddressSource__c> customAddressSourceList = [
                    SELECT Id
                    FROM zkups__CustomAddressSource__c
                    WHERE Name = :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. zkups__QueuedShipment__c object
                zkups__QueuedShipment__c queuedShipment = new zkups__QueuedShipment__c (
                    zkups__BulkShipment__c = bulkShipment.Id,
                    zkups__LabelImageType__c = caseShipmateList.get(0).zkups__LabelImageTypeDefault__c,
                    zkups__ServiceType__c = 'Domestic: UPS Ground',
                    zkups__PackagingType__c = settings.DefaultPackagingType__c,
                    zkups__WeightDimensionUnits__c = settings.DimensionUnits__c,
                   
                    // sender info
                    zkups__SenderName__c = caseShipmateList.get(0).zkups__CompanyName__c,
                    zkups__SenderIsResidential__c = caseShipmateList.get(0).zkups__ShippingIsResidential__c,
                    zkups__SenderCity__c = caseShipmateList.get(0).zkups__ShippingCity__c,
                    zkups__SenderCompany__c = caseShipmateList.get(0).zkups__CompanyName__c,
                    zkups__SenderCountry__c = caseShipmateList.get(0).zkups__ShippingCountry__c,
                    zkups__SenderEmail__c = caseShipmateList.get(0).zkups__SenderEMailDefault__c,
                    zkups__SenderPhone__c = caseShipmateList.get(0).zkups__SenderPhoneDefault__c,
                    zkups__SenderState__c = caseShipmateList.get(0).zkups__ShippingState__c,
                    zkups__SenderStreet__c = caseShipmateList.get(0).zkups__ShippingStreet__c,
                    zkups__SenderPostalCode__c = caseShipmateList.get(0).zkups__ShippingPostalCode__c,
                   
                    // recipient info
                    // you can set it from custom address source or from case directly
                    zkups__RecipientIsResidential__c = currentCase.Is_Residential__c,
                    zkups__RecipientCity__c = currentCase.Recipient_City__c,
                    zkups__RecipientCompany__c = currentCase.Recipient_Company_Name__c,
                    zkups__RecipientCountry__c = currentCase.Recipient_Country__c,
                    zkups__RecipientEmail__c = currentCase.Recipient_Email__c,
                    zkups__RecipientName__c = currentCase.Recipient_Name__c,
                    zkups__RecipientPhone__c = currentCase.Recipient_Phone__c,
                    zkups__RecipientState__c = currentCase.Recipient_State__c,
                    zkups__RecipientStreet__c = currentCase.Recipient_Street__c,
                    zkups__RecipientPostalCode__c = currentCase.Recipient_Postal_Code__c,
                    zkups__PaymentType__c = settings.PaymentType__c,
                    zkups__CustomAddressSource__c = customAddressSourceList.get(0).id,
                    zkups__CustomAddressSourceRecordId__c = caseId);

                insert queuedShipment;

                // 3. zkups__QueuedPackage__c object
                zkups__QueuedPackage__c queuedPackage = new zkups__QueuedPackage__c (
                    zkups__DeclaredValue__c = settings.DefaultDeclaredValue__c,
                    zkups__Weight__c = settings.DefaultWeight__c,
                    zkups__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>{
        'Next Day Air Early AM' =>'Domestic: UPS Next Day Air Early A.M.',
        'Next Day Air' => 'Domestic: UPS Next Day Air',
        'Next Day Air Saver' => 'Domestic: UPS Next Day Air Saver',
        'Second Day Air AM' => 'Domestic: UPS 2nd Day Air A.M.',
        'Second Day Air' => 'Domestic: UPS 2nd Day Air',
        'Three Day Select' => 'Domestic: UPS 3 Day Select',
        'Ground' => 'Domestic: UPS Ground'
    };
}

Apex Controller Test

@isTest
private class NewUPSShipmentCaseControllerTest {
   @isTest
    public static void testShipment_Success() {
    Account testAccount = new Account();
testAccount.Name = 'testAccount';
insert testAccount;

Case caseRecord = new Case();
caseRecord.AccountId = testAccount.Id;
caseRecord.Is_Residential__c = false;
caseRecord.Recipient_City__c = 'Test City';
caseRecord.Recipient_Country__c = 'US';
caseRecord.Recipient_Name__c = 'test@zenkraft.com';
caseRecord.Recipient_Name__c = 'John';
caseRecord.Recipient_Company_Name__c = 'Zenkraft';
caseRecord.Recipient_Phone__c = '12312312300';
caseRecord.Recipient_State__c = 'CA';
caseRecord.Recipient_Street__c = 'Test Street 15';
caseRecord.Recipient_Postal_Code__c = '90001';
insert caseRecord;

zkups__UPSShipmatePreference__c shipmatePreference = new zkups__UPSShipmatePreference__c();
insert shipmatePreference;

String preferenceName = [
SELECT Name
FROM zkups__UPSShipmatePreference__c
WHERE id = :shipmatePreference.Id
].Name;

BulkShipmentSettings__c bulkShipmentSettings = new BulkShipmentSettings__c();
bulkShipmentSettings.Name = 'UPS';
bulkShipmentSettings.ShipmatePreferenceName__c = preferenceName;
insert bulkShipmentSettings;

zkups__CustomAddressSource__c customAddressSource = new zkups__CustomAddressSource__c();
customAddressSource.Name = 'Case';
insert customAddressSource;

    Test.startTest();

    ApexPages.StandardController stdController = new ApexPages.StandardController(caseRecord);
       NewUPSShipmentCaseController ctrl = new NewUPSShipmentCaseController(stdController);
       ctrl.createBulkShippingObjects();

       ctrl.processBulkShipment();
       ctrl.checkBatchStatus();
       ctrl.checkBatchStatus();
       
       zkups__QueuedShipmentStatus__c successStatuses = [
SELECT zkups__Status__c
FROM zkups__QueuedShipmentStatus__c
WHERE zkups__Status__c = 'SUCCESS'];
       System.assertEquals('SUCCESS',successStatuses.zkups__Status__c);

        Test.stopTest();
    }

}
Did this answer your question?