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.
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:
If you need to add special services then create the QueuedShipmentSpecialServices object like so:
We now have the required information to create a successful rate request. Which is done using this line of code:
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:
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.