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 queuedShipment object.
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:
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 UPS. Each record in this list is of type zkups.ShipmentRate, and the information contained is:
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:
Any questions please contact us. Happy coding.