Permanently Delete Datasets and Alignments

The DoCleanup() Apex method permanently removes, or cleans up, datasets and related alignments that a user already deleted using Territory Planning Data Sets or Territory Planning Alignments | Delete in the UI. Use this method to reduce object size and improve Territory Planning load time and performance.

The DoCleanup() Apex method is the equivalent of clicking Permanently Delete in Territory Planning Data Management in Setup. You can permanently delete datasets only with a Deleted status as shown on the Territory Planning Data Sets page. When you permanently delete a dataset, all alignments created from that dataset’s source are also permanently removed.

Dataset and alignment cleanups can run at scheduled times or after a user deletes a dataset in the UI.

Signature

AsyncApexJob maps.TPSDK.DoCleanup()
Where,
  • AsyncApexJob represents an individual Apex sharing recalculation job, a batch Apex job, a method with the future annotation, or a job that implements Queueable. Use this object to query Apex batch jobs in your organization.
  • maps is the namespace that's available after you install Salesforce Maps.
  • TPSDK is the class that contains the Salesforce Maps Territory Planning global methods exposed to developers.
  • DoCleanup() is the method.

Sample Code

This code runs dataset and alignment cleanups at scheduled times. The DoCleanup() method returns an AsyncApexJob containing the number of items processed, the date and time of job completion, and other details.

If you invoke methods within a flow, process builder, or trigger, do one of the following to avoid uncommitted work errors:

  • Call the methods through a future method
  • Call the methods as queueable

Warning

Example

// Implement as Schedulable to run the Apex class at regular intervals.
public with sharing class TerritoryPlanningCleanupSchedule implements Schedulable  {

// Invoke the scheduled Apex class with the execute() method.
    public void execute(SchedulableContext SC) {
        AsyncApexJob cleanupJob = maps.TPSDK.DoCleanup();
        System.debug('Cleanup job is ' +cleanupJob.Status+ '.  '  +cleanupJob.JobItemsProcessed+ ' of ' +cleanupJob.TotalJobItems+ ' batches processed.');
    }
    
}