background

Request UPS rate for shipment from the Account Object

Published on 01 November 2017 in

Today we will show you how you can get UPS rates for a shipment and add the users choice back onto the account object.

The first thing we need to do is set the “to” and “from” address of the shipment. In the code below we get the preference details to ship from and we get the ship to address details from the account object itself. We add all this information to a queuedShipmentobject.

    zkups__UPSShipmatePreference__c shipmatePref = [
            SELECT Id, Name,
                zkups__UserId__c, zkups__UserIdVisible__c, zkups__UPSAccountNumber__c,
                zkups__ThermalPrinterName__c, zkups__Street__c, zkups__State__c, zkups__SenderPhoneDefault__c,
                zkups__SenderNameDefault__c, zkups__SenderEMailDefault__c, zkups__SendEMailNotificationToShipper__c,
                zkups__SendEMailNotificationToRecipient__c, zkups__PrimaryContactTitle__c, zkups__PrimaryContactPhone__c,
                zkups__PrimaryContactName__c, zkups__PrimaryContactEmail__c, zkups__PostalCode__c, zkups__Password__c,
                zkups__LabelImageTypeDefault__c, zkups__EMailNotifyOnExceptionDefault__c, zkups__EMailNotifyOnDeliveryDefault__c,
                zkups__EMailMessageDefault__c, zkups__DetailedViewShipmatePreferenceList__c, zkups__DetailedViewReUsePackageList__c,
                zkups__Country__c, zkups__CompanyURL__c, zkups__CompanyName__c, zkups__City__c, zkups__AccessLicenseNumber__c,
                zkups__ShippingStreet__c, zkups__ShippingCity__c, zkups__ShippingState__c, zkups__ShippingPostalCode__c,
                zkups__ShippingCountry__c, zkups__ShippingIsResidential__c
            FROM zkups__UPSShipmatePreference__c
            LIMIT 1];
        
        //zkups__QueuedShipment__c object
        zkups__QueuedShipment__c queuedShipment = new zkups__QueuedShipment__c (
            zkups__LabelImageType__c = 'GIF',
            zkups__ServiceType__c = 'Domestic: UPS Ground',
            zkups__PackagingType__c = 'Your Packaging',
            zkups__PaymentType__c = 'Sender',
            zkups__WeightDimensionUnits__c = 'LB / IN', 
            zkups__Account__c = accountRecord.Id,
        
            // sender info
            zkups__SenderName__c = shipmatePref.zkups__SenderNameDefault__c,
            zkups__SenderCity__c = shipmatePref.zkups__ShippingCity__c,
            zkups__SenderCompany__c = shipmatePref.zkups__CompanyName__c,
            zkups__SenderEmail__c = shipmatePref.zkups__SenderEMailDefault__c,
            zkups__SenderPhone__c = shipmatePref.zkups__SenderPhoneDefault__c,
            zkups__SenderState__c = shipmatePref.zkups__ShippingState__c,
            zkups__SenderStreet__c = shipmatePref.zkups__ShippingStreet__c,
            zkups__SenderPostalCode__c = shipmatePref.zkups__ShippingPostalCode__c,
            zkups__SenderCountry__c = shipmatePref.zkups__ShippingCountry__c,
                    
            // recipient info 
            zkups__RecipientName__c = accountRecord.Name,
            zkups__RecipientCity__c = accountRecord.ShippingCity,
            zkups__RecipientCompany__c = accountRecord.Name,
            zkups__RecipientCountry__c = accountRecord.ShippingCountry,
            zkups__RecipientPhone__c = accountRecord.Phone,
            zkups__RecipientState__c = accountRecord.ShippingState,
            zkups__RecipientStreet__c = accountRecord.ShippingStreet,
            zkups__RecipientPostalCode__c = accountRecord.ShippingPostalCode
        );

Note lines 19-23 where the Packaging Type, Unit Dimensions, DropOff Type and Service Type are set.

The next step is to create the UPS package type to include the Declared Value and the Weight. This can be done using this code here:

        zkups__QueuedPackage__c queuedPackage = new zkups__QueuedPackage__c (
            zkups__DeclaredValue__c = 1,
            zkups__Weight__c = 1,
            zkups__Height__c = 1,
            zkups__Length__c = 1,
            zkups__Width__c = 1,
            zkups__QueuedPackageSpecialServices__c = null,
            zkups__QueuedShipment__c = queuedShipment.Id         
        );

If you need to add special services then create the QueuedShipmentSpecialServicesobject like so:

        zkups__QueuedShipmentSpecialServices__c specialServices = new zkups__QueuedShipmentSpecialServices__c();
        List<zkups__QueuedPackageSpecialServices__c> packagesSecialServices = new List<zkups__QueuedPackageSpecialServices__c>();

We now have the required information to create a successful rate request. Which is done using this line of code:

this.ratesList = zkups.BulkShipmentInterface.calculateRates(queuedShipment, queuedPackages, specialServices, packagesSecialServices, shipmatePref);            

The ratesList variable contains all the rate information that is returned from UPS. Each record in this list is of type zkups.ShipmentRate, and the information contained is:

	global String shipmentProvider { get; set;}
	global String mailService { get; set; }
	global String currencyIsoCode { get; set; }
	global Decimal cost { get; set; }
	global Decimal negotiatedRates { get; set; }
	global Integer guaranteedDaysToDelivery {get; set;}
	global String scheduledDeliveryTime {get; set;}

If you want the user to select the rate to write back to the Account object field Cost__c you can use the variables in the commandLink.

Cost__c is the field where you want to store UPS quote and selectedRate is the line that was selected by the user on the VisualForce page.

The full code is below:

Controller:

public with sharing class CalculateUPSRates {
	public List<zkups.ShipmentRate> ratesList {get; set;}
	public Account accountRecord {get; set;}

    public void CalculateUPSRates() {

		this.accountRecord = [
				SELECT Name, Phone, ShippingStreet, ShippingCity, 
					   ShippingPostalCode, ShippingCountry, ShippingState
				FROM Account
				WHERE Id = :ApexPages.currentPage().getParameters().get('id')
				LIMIT 1];

        zkups__UPSShipmatePreference__c shipmatePref = [
            SELECT Id, Name,
                zkups__UserId__c, zkups__UserIdVisible__c, zkups__UPSAccountNumber__c,
                zkups__ThermalPrinterName__c, zkups__Street__c, zkups__State__c, zkups__SenderPhoneDefault__c,
                zkups__SenderNameDefault__c, zkups__SenderEMailDefault__c, zkups__SendEMailNotificationToShipper__c,
                zkups__SendEMailNotificationToRecipient__c, zkups__PrimaryContactTitle__c, zkups__PrimaryContactPhone__c,
                zkups__PrimaryContactName__c, zkups__PrimaryContactEmail__c, zkups__PostalCode__c, zkups__Password__c,
                zkups__LabelImageTypeDefault__c, zkups__EMailNotifyOnExceptionDefault__c, zkups__EMailNotifyOnDeliveryDefault__c,
                zkups__EMailMessageDefault__c, zkups__DetailedViewShipmatePreferenceList__c, zkups__DetailedViewReUsePackageList__c,
                zkups__Country__c, zkups__CompanyURL__c, zkups__CompanyName__c, zkups__City__c, zkups__AccessLicenseNumber__c,
                zkups__ShippingStreet__c, zkups__ShippingCity__c, zkups__ShippingState__c, zkups__ShippingPostalCode__c,
                zkups__ShippingCountry__c, zkups__ShippingIsResidential__c
            FROM zkups__UPSShipmatePreference__c
            LIMIT 1];
        
        //zkups__QueuedShipment__c object
        zkups__QueuedShipment__c queuedShipment = new zkups__QueuedShipment__c (
            zkups__LabelImageType__c = 'GIF',
            zkups__ServiceType__c = 'Domestic: UPS Ground',
            zkups__PackagingType__c = 'Your Packaging',
            zkups__PaymentType__c = 'Sender',
            zkups__WeightDimensionUnits__c = 'LB / IN', 
            zkups__Account__c = accountRecord.Id,
        
            // sender info
            zkups__SenderName__c = shipmatePref.zkups__SenderNameDefault__c,
            zkups__SenderCity__c = shipmatePref.zkups__ShippingCity__c,
            zkups__SenderCompany__c = shipmatePref.zkups__CompanyName__c,
            zkups__SenderEmail__c = shipmatePref.zkups__SenderEMailDefault__c,
            zkups__SenderPhone__c = shipmatePref.zkups__SenderPhoneDefault__c,
            zkups__SenderState__c = shipmatePref.zkups__ShippingState__c,
            zkups__SenderStreet__c = shipmatePref.zkups__ShippingStreet__c,
            zkups__SenderPostalCode__c = shipmatePref.zkups__ShippingPostalCode__c,
            zkups__SenderCountry__c = shipmatePref.zkups__ShippingCountry__c,
                    
            // recipient info 
            zkups__RecipientName__c = accountRecord.Name,
            zkups__RecipientCity__c = accountRecord.ShippingCity,
            zkups__RecipientCompany__c = accountRecord.Name,
            zkups__RecipientCountry__c = accountRecord.ShippingCountry,
            zkups__RecipientPhone__c = accountRecord.Phone,
            zkups__RecipientState__c = accountRecord.ShippingState,
            zkups__RecipientStreet__c = accountRecord.ShippingStreet,
            zkups__RecipientPostalCode__c = accountRecord.ShippingPostalCode
        );

        //zkups__QueuedPackage__c object
        zkups__QueuedPackage__c queuedPackage = new zkups__QueuedPackage__c (
            zkups__DeclaredValue__c = 1,
            zkups__Weight__c = 1,
            zkups__Height__c = 1,
            zkups__Length__c = 1,
            zkups__Width__c = 1,
            zkups__QueuedPackageSpecialServices__c = null,
            zkups__QueuedShipment__c = queuedShipment.Id         
        );

        List<zkups__QueuedPackage__c> queuedPackages = new List<zkups__QueuedPackage__c>();
        queuedPackages.add(queuedPackage);
        
        zkups__QueuedShipmentSpecialServices__c specialServices = new zkups__QueuedShipmentSpecialServices__c();
        List<zkups__QueuedPackageSpecialServices__c> packagesSecialServices = new List<zkups__QueuedPackageSpecialServices__c>();

        // validation
        this.ratesList = zkups.BulkShipmentInterface.calculateRates(queuedShipment, queuedPackages, specialServices, packagesSecialServices, shipmatePref);        
    }
	
	//If you want the user to select the Rate Cost and Service Type to write back to the Account object fields Cost__c and Service_Type__c
    public Pagereference addCost() {
        String serviceTypeValue = ApexPages.currentPage().getParameters().get('ServiceType');
        String costValue = ApexPages.currentPage().getParameters().get('Cost');
        
        accountRecord.Service_Type__c = serviceTypeValue;
        accountRecord.Cost__c = Decimal.valueof(costValue);        
        update accountRecord;
        
        return new Pagereference('/' + accountRecord.id);
    }  
} 


Visualforce Page:

<apex:page Controller="CalculateUPSRates" action="{! CalculateUPSRates }">
    <apex:form >
        
        <apex:pageBlock >        
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{! ratesList }" var="rate">      
                    <apex:column value="{! rate.shipmentProvider }" headerValue="Shipment Provider"/>          
                    <apex:column value="{! rate.mailService }" headerValue="Service Type"/>
                    <apex:column value="{! rate.cost }" headerValue="Cost"/>
                    <apex:column >
                        <apex:commandLink value="Add Cost" action="{! addCost }">
                            <apex:param name="ServiceType" value="{! rate.mailService }"/>
                            <apex:param name="Cost" value="{! rate.cost }"/> 
                        </apex:commandLink>  
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

Any questions please contact us. Happy coding.