How to Create a Shipment off a Trigger on Salesforce
Here we will show you how you can create a shipment directly via a trigger on your Account object. For this example we will use our UPS app with our ShipAutomate product.
First, set up a trigger on your account object. This trigger will run each time the account is updated.
The trigger calls this apex class “AccountTriggerHandler”. The full code can be found here, but we will walk through it further down:
- First thing we need to do is get the UPS Shipment Preference and the associated preference details needed to create a shipment. We just select the top preference as we are only going to be using one preference on our test org..
- To support the Salesforce bulk process, we need to create a bulk shipment map to store Accounts and Zenkraft bulk shipments. For example, if we update a list of Accounts in Salesforce, this code below ensures that all Accounts generate a shipment.
- The next step is to create our queued shipment objects. This is the main section of our code and where we set all the ship to/ship from and service type details. Line 6, determines the “Service Type” and Line 9 is the “Packaging Type”.
As the Account is a standard object, all we have to do to link the shipments generated to the account is to add this line of code: queuedShipment.zkups__Account__c = accountId; (as you can see in line 10 below).
- After created all the queued shipments, we are able to go to the next step, which is creating a new list of packages for each of the queued shipments. We add a package with a value of $100 and a weight of 2 pounds for each queued shipment.
- Final step, we loop through the bulk shipment list and call the ‘processBulkShipment‘ method to create the shipment: