background

Add External UPS tracking numbers to an Order

Published on 14 March 2017 in salesforce tracking 

In today's blog post we’ll show you how to add externally created UPS tracking number to an object. Most common use cases are to get updated tracking, that you are then able to query using the standard Salesforce reporting. We will be using the Order object in our demo here.

 

Trigger on the Order Object:

Line 3: For when a brand new order is added with a tracking number

Line 6: For when the tracking number field is updated

 

The trigger Handler class:

This is where the work gets done.

Line 4: You will need to contact us to get the ModKey value that allows the creation of a Shipment Object (without actually going through our package for the shipment creation process).

Line 7-11: You need to create a custom setting to store the ID of the preference, so that when you move this code from your sandbox org to your production org, its easy to update the custom setting to your saved UPS preference.

Shipping Settings

Line 27-39: For any new Orders, create the new shipment with the zkups__MasterTrackingId__c and make sure its linked to your UPS Preference via the zkups__UPSShipmatePreference__c.

Line 40-63: If you are just updating the tracking number field (FB_Tracking_Numbers__c) on your Order, this lines take care of the shipment creation.

Line 67-69: If a shipment has been queued for insertion, then we insert the Shipments.

public class AddShipmentManuallyHandler {
public static Boolean enablesTrigger = true;
private static final String ZKMODKEY = 'PLEASE_CONTACT_US_FOR_THIS_VALUE';
public static void createUPSShipments (List<Order> newRecords, List<Order> oldRecords) {
ShipmentSettings__c shipmentSettings = ShipmentSettings__c.getInstance('UPS');
if (shipmentSettings == null || String.isBlank(shipmentSettings.Shipment_Preference_Id__c)){
return; // cannot insert any ups shipment without an ups shipment preference
}
List<zkups__UPSShipmatePreference__c> preferences = [
SELECT Id, Name
FROM zkups__UPSShipmatePreference__c
WHERE Id =: shipmentSettings.Shipment_Preference_Id__c
LIMIT 1];
if (preferences == null || preferences.isEmpty()) {
return; // cannot insert any ups shipment without an ups shipment preference
}
zkups__UPSShipmatePreference__c preference = preferences[0];
List<zkups__UPSShipment__c> shipmentsForInsert = new List<zkups__UPSShipment__c>();
// collect ids
if (oldRecords == null) { // insert, undelete
for (Order newRecord: newRecords) {
// logic for insert and undelete
if (String.isNotBlank(newRecord.FB_Tracking_Numbers__c)) {
zkups__UPSShipment__c newShipment = new zkups__UPSShipment__c();
newShipment.zkups__MasterTrackingId__c = newRecord.FB_Tracking_Numbers__c;
newShipment.zkups__UPSShipmatePreference__c = preference.Id;
newShipment.zkups__ModKey__c = ZKMODKEY;
newShipment.Order__c = newRecord.Id;
shipmentsForInsert.add(newShipment);
}
}
} else {
if (newRecords != null) { // update
for (Integer i = 0; i < newRecords.size(); i++) {
Order oldRecord = oldRecords.get(i);
Order newRecord = newRecords.get(i);
// logic for update
if (String.isNotBlank(newRecord.FB_Tracking_Numbers__c)
&& newRecord.FB_Tracking_Numbers__c != oldRecord.FB_Tracking_Numbers__c) {
zkups__UPSShipment__c newShipment = new zkups__UPSShipment__c();
newShipment.zkups__MasterTrackingId__c = newRecord.FB_Tracking_Numbers__c;
newShipment.zkups__UPSShipmatePreference__c = preference.Id;
newShipment.zkups__ModKey__c = ZKMODKEY;
newShipment.Order__c = newRecord.Id;
shipmentsForInsert.add(newShipment);
}
}
} else { // delete
// for (Order detail: oldRecords) {
// logic for delete
// }
}
}
// main method logic
if (shipmentsForInsert != null && !shipmentsForInsert.isEmpty()) {
insert shipmentsForInsert;
}
}
}

The @Test class, is needed for the code coverage to be able to deploy into production.

@isTest
private class AddShipmentManuallyHandlerTest {
static testMethod void test_createUPSShipments() {
zkups__UPSShipmatePreference__c preference = new zkups__UPSShipmatePreference__c();
insert preference;
ShipmentSettings__c shipmentSettings = new ShipmentSettings__c();
shipmentSettings.Name = 'UPS';
shipmentSettings.Shipment_Preference_Id__c = preference.Id;
insert shipmentSettings;
String trackNumber = 'trackNumber';
Account acc = new Account();
acc.Name = 'Account';
insert acc;
Order ord = new Order();
ord.FB_Tracking_Numbers__c = trackNumber;
ord.AccountId = acc.Id;
ord.Status = 'Draft';
ord.EffectiveDate = Date.today();
Test.startTest();
insert ord;
checkShipmentExists(trackNumber);
ord.FB_Tracking_Numbers__c = trackNumber + '1';
update ord;
checkShipmentExists(trackNumber + '1');
Test.stopTest();
}
private static void checkShipmentExists(String trackNumber){
List<zkups__UPSShipment__c> shipments = [
SELECT Id, zkups__MasterTrackingId__c
FROM zkups__UPSShipment__c
WHERE zkups__MasterTrackingId__c = :trackNumber
LIMIT 1
];
System.assertEquals(1, shipments.size());
}
}

Any questions, please do not hesitate to contact us!