Today’s blog post explains how to create a FedEx shipment directly from a trigger on a standard Salesforce Account object. You will need our FedEx app and our Bulk Shipping package (sandbox install link, production install link, please be aware there are extra licensing costs for using the bulk package.) Contact us if you need any further information.
Firstly, create a checkbox on your account object. Let’s call it Create_Fedex__c. We will run the shipment creation process when this gets set to TRUE.
To store any validation errors (such as incorrect addresses), you need to create a custom object called Zenkraft_Validation_Error__c. Fill in the fields as you see here:
Next, create the trigger on the Account object. You can see the code below:
The trigger calls the AccountTriggerHandler that determines whether the checkbox has been set to TRUE, and if so, creates the shipment.
Lines 13-20: Get the address information from the account object that is needed to create the shipment.
Lines 25-33: Create the queued shipment.
Lines 35-43: Create the package, including the weight and declared value.
Lines 45-47: If the above have been generated successfully we use the processShipments method to start the creation process
FedexShipmentService is the class that allows the easy creation of the QueuedShipments, it gets the account details from the FedEx account that you have set up, and creates the BulkShipment where we store all the shipment information before we actually send it to be processed.
Lines 6-41: We set the zkfedex__ServiceType__c , zkfedex__PackagingType__c for the shipment along with the sender and recipient addresses.
To handle any errors, create another trigger. This time put it on the Queued Shipment Status object.
And the respective trigger handler code is below:
Happy coding.