Today we will show you how you can get FedEx 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 queuedShipment object.
zkfedex__ShipmatePreference__c shipmatePreference = [ | |
SELECT zkfedex__ShippingCity__c, zkfedex__ShippingCountry__c, zkfedex__ShippingState__c, | |
zkfedex__ShippingStreet__c, zkfedex__ShippingPostalCode__c, | |
zkfedex__CompanyName__c, zkfedex__DropoffTypeDefault__c, zkfedex__EMailMessageDefault__c, | |
zkfedex__SenderNameDefault__c, zkfedex__SenderEMailDefault__c, | |
zkfedex__SenderPhoneDefault__c, zkfedex__LabelImageTypeDefault__c, zkfedex__SendEMailNotificationToRecipient__c, | |
zkfedex__SendEMailNotificationToShipper__c, | |
zkfedex__FedExAccountNumber__c, zkfedex__BillingCountry__c, zkfedex__BillingCity__c, zkfedex__BillingState__c, | |
zkfedex__BillingStreet__c, zkfedex__BillingPostalCode__c, zkfedex__FedExMeterNumber__c, | |
zkfedex__SubscriptionDataEncrypted__c, zkfedex__EncryptedFedExMeterNumber__c, | |
zkfedex__GenericFedExEndUserKey__c, zkfedex__GenericFedExEndUserPassword__c, | |
zkfedex__GenericCredentialsEncrypted__c, zkfedex__EncryptedGenericFedExEndUserKey__c, | |
zkfedex__EncryptedGenericFedExEndUserPasswd__c, Name | |
FROM zkfedex__ShipmatePreference__c | |
LIMIT 1]; | |
zkfedex__QueuedShipment__c queuedShipment = new zkfedex__QueuedShipment__c(); | |
queuedShipment.zkfedex__SenderName__c = shipmatePreference.zkfedex__SenderNameDefault__c; | |
queuedShipment.zkfedex__SenderCompany__c = shipmatePreference.zkfedex__CompanyName__c; | |
queuedShipment.zkfedex__SenderEmail__c = shipmatePreference.zkfedex__SenderEMailDefault__c; | |
queuedShipment.zkfedex__ShipDate__c = Date.today(); | |
queuedShipment.zkfedex__SenderStreet__c = shipmatePreference.zkfedex__ShippingStreet__c; | |
queuedShipment.zkfedex__SenderCity__c = shipmatePreference.zkfedex__ShippingCity__c; | |
queuedShipment.zkfedex__SenderPostalCode__c = shipmatePreference.zkfedex__ShippingPostalCode__c; | |
queuedShipment.zkfedex__SenderCountry__c = shipmatePreference.zkfedex__ShippingCountry__c; | |
queuedShipment.zkfedex__SenderState__c = shipmatePreference.zkfedex__ShippingState__c; | |
queuedShipment.zkfedex__PaymentType__c = 'Sender'; | |
queuedShipment. | |
queuedShipment.zkfedex__RecipientName__c = accountRecord.Name; | |
queuedShipment.zkfedex__RecipientCompany__c = accountRecord.Name; | |
queuedShipment.zkfedex__RecipientPhone__c = accountRecord.Phone; | |
queuedShipment.zkfedex__RecipientEmail__c = accountRecord.Email; | |
queuedShipment.zkfedex__RecipientStreet__c = accountRecord.ShippingStreet; | |
queuedShipment.zkfedex__RecipientCity__c = accountRecord.ShippingCity; | |
queuedShipment.zkfedex__RecipientPostalCode__c = accountRecord.ShippingPostalCode; | |
queuedShipment.zkfedex__RecipientCountry__c = accountRecord.ShippingCountry; | |
queuedShipment.zkfedex__RecipientState__c = accountRecord.ShippingState; | |
queuedShipment.zkfedex__PackagingType__c = 'Your Packaging'; | |
queuedShipment.zkfedex__WeightDimensionUnits__c = 'LB / IN'; | |
queuedShipment.zkfedex__DropoffType__c = 'Business Service Center'; | |
queuedShipment.zkfedex__ServiceType__c = 'Domestic: FedEx Ground'; |
Note lines 40-43 where the Packaging Type, Unit Dimensions, DropOff Type and Service Type are set.
The next step is to create the FedEx package type to include the Declared Value and the Weight. This can be done using this code here:
List<zkfedex__QueuedPackage__c> queuedPackagesList = new List<zkfedex__QueuedPackage__c(); | |
zkfedex__QueuedPackage__c newpackage = new zkfedex__QueuedPackage__c(); | |
newpackage.zkfedex__DeclaredValue__c = 0; | |
newpackage.zkfedex__Weight__c = 5; | |
queuedPackagesList.add(newpackage); |
If you need to add special services then create the QueuedShipmentSpecialServices object like so:
zkfedex__QueuedShipmentSpecialServices__c specialServices = new zkfedex__QueuedShipmentSpecialServices__c(); | |
List<zkfedex__QueuedPackageSpecialServices__c> queuedPackageSpecialServices = new List<zkfedex__QueuedPackageSpecialServices__c>{new zkfedex__QueuedPackageSpecialServices__c()}; |
We now have the required information to create a successful rate request. Which is done using this line of code:
List<zkfedex.ShipmentRate> ratesList = zkfedex.BulkShipmentInterface.calculateRates(queuedShipment, queuedPackagesList, specialServices, queuedPackageSpecialServices, spreference); |
The ratesList variable contains all the rate information that is returned from FedEx. Each record in this list is of type zkfedex.ShipmentRate, and the information contained is:
global String shipmentProvider { get; set;} | |
global String mailService { get; set; } | |
global String deliveryDayOfWeek { get; set; } | |
global DateTime deliveryDate { get; set; } | |
global Decimal taxes { get; set; } | |
global Decimal discounts { get; set; } | |
global Decimal cost { get; set; } | |
global String transitTime { get; set; } | |
global Decimal surcharges { get; set; } | |
global Decimal rebates { get; set; } | |
global Decimal baseCharge { get; set; } | |
global Decimal netCharge { get; set; } | |
global String netChargeCurrency { get; set; } |
If you want the user to select the rate to write back to the Account object field Fedex_Quote__c you can use ratesList in apex:pageBlockTable. Make sure to add apex:commandLink in the first apex:column to get cost and set it to the field in record.
accountRecord.Fedex_Quote__c = selectedRate.cost;
Fedex_Quote__c is the field where you want to store FedEx quote and selectedRate is the line that was selected by the user on the VisualForce page.
Any questions please contact us. Happy coding.