background

Create a scheduled job code to delete any FedEx checkpoints for shipments that have been delivered more than 2 week ago

Published on 04 October 2017 in  

Todays blog post will show you the code needed to set up a scheduled job that will remove all FedEx checkpoint objects for shipments that have been delivered more than 2 weeks ago.

This is useful if you are coming up against salesforce storage limits and need to clear out some space.

We will need to create two classes and then their respective test classes.

RemoveDeliveredCheckpointsBatch

This class creates the list of all the checkpoints that need to be deleted.

global class RemoveDeliveredCheckpointsBatch implements Database.Batchable<sObject> {
public Date checkDate;
public String query;
global RemoveDeliveredCheckpointsBatch() {
this.checkDate = Date.today().addDays(-14);
this.query = 'SELECT Id FROM zkfedex__Checkpoint__c WHERE zkfedex__Shipment__r.zkfedex__ActualDeliveryDate__c < :checkDate AND zkfedex__Shipment__r.zkfedex__Delivered__c = TRUE';
}
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, list<Sobject> scope) {
List<zkfedex__Checkpoint__c> listToDelete = (List<zkfedex__Checkpoint__c>)scope;
delete listToDelete;
}
global void finish(Database.BatchableContext BC) {
}
}

Line 7 creates the SELECT query, and Line 15-16 is where the DELETE statement is executed.

RemoveDeliveredCheckpointsScheduler

This class is needed to execute the RemoveDeliveredCheckpointsBatch above, and will be the class that we use the Schedule Apex on your production instance, once these classes have been deployed.

global class RemoveDeliveredCheckpointsScheduler implements Schedulable {
global void execute(SchedulableContext sc) {
RemoveDeliveredCheckpointsBatch s = new RemoveDeliveredCheckpointsBatch();
Database.executeBatch(s);
}
}

RemoveDeliveredCheckpointsBatchTest

The test class for RemoveDeliveredCheckpointsBatch

@isTest
private class RemoveDeliveredCheckpointsBatchTest {
static testMethod void testBatch() {
zkfedex__Shipment__c shipment = new zkfedex__Shipment__c();
shipment.zkfedex__RecipientName__c = 'Test Name';
shipment.zkfedex__ActualDeliveryDate__c = date.newinstance(2017, 2, 17);
shipment.zkfedex__Delivered__c = TRUE;
shipment.zkfedex__RecipientStreet__c = 'Street 13';
shipment.zkfedex__RecipientCity__c = 'San Diego';
shipment.zkfedex__RecipientState__c = 'CA';
shipment.zkfedex__RecipientPostalCode__c = '90001';
shipment.zkfedex__RecipientCountry__c = 'United Sates';
shipment.zkfedex__ServiceType__c = 'FedEx Ground';
shipment.zkfedex__RecipientPhone__c = '123123123122';
insert shipment;
zkfedex__Checkpoint__c checkpoint = new zkfedex__Checkpoint__c();
checkpoint.zkfedex__Shipment__c = shipment.id;
insert checkpoint;
Test.startTest();
Database.executeBatch(new RemoveDeliveredCheckpointsBatch());
Test.stopTest();
List<zkfedex__Checkpoint__c> testCheckpoints = [
SELECT id
FROM zkfedex__Checkpoint__c];
System.assert(testCheckpoints.isEmpty());
}
}

RemoveDeliveredCheckpointsSchedulerTest

The test class for RemoveDeliveredCheckpointsScheduler

@isTest
private class RemoveDeliveredCheckpointsSchedulerTest {
static testMethod void testMethod1() {
Test.StartTest();
RemoveDeliveredCheckpointsScheduler sh = new RemoveDeliveredCheckpointsScheduler();
String sch = '0 0 12 * * ?'; system.schedule('Test Job', sch, sh);
Test.stopTest();
}
}

Next you need to create the changeset and deploy these classes into your production environment.

Once in your production environment you need to go to Setup > Develop > Apex Classes and click on the schedule Apex button:

Fill in the details and remember to input RemoveDeliveredCheckpointsScheduler into the Apex class field.

If you want the job to run everyday at 5am then set up the page like so:

Click Save, and you’re finished.

Please let us know if you have any questions and we will be happy to help.