I created a Territory Hierarchy like:


      France       |      Poland

Paris  Lyon  Nice  |  Warsaw  Krakow

When I delete Poland I want to automatically delete all child territory records. How can I resolve this problem?


First, as you've likely discovered, you can't delete territories that have children. But there's a workaround for that. It involves exporting your data using DataLoader into a CSV file, editing the file, then importing it again to Delete them by working from the Bottom Up. You may need to repeat the process until you have no errors.

From How to Mass Delete Territories:

The Data Loader can help with mass territory deletion, but because Salesforce territories cannot be deleted if they have child territories, your deletion must be organized to delete bottom-level territories first, working up through the territory tree. But how can this be done?

Fist use Data Loader to export the Territory data from the Territory Object and prepare an Excel CSV file listing all the territories you want to remove from the TM Hierarchy.

Choose Show all objects to select Object=Territory. Then include fields: Id, Name, ParentTerritoryId.

IMPORTANT: Either use SOQL query in your Data Loader export or edit the CSV file later to EXCLUDE territories you don't want to delete. Next, there are two options for removing the exported territories choose ONE:

METHOD 1. Organize your CSV data file whereby the territories are listed sequentially from the lowest node (hierarchy level) to highest node (hierarchy level). Using the data export from the Territory object and some manipulation in Excel can help with this sequencing. Then in Data Loader use the Settings menu to change your Data Loader Batch size set to 1 (this is so that each row in the file gets processed one at a time). Then use Data Loader's Delete function with Object Territory (Show all objects) with your sorted CSV file to delete the territories. The Id field identifies the correct territories to delete.

METHOD 2. Or you can skip the step of sorting your CSV file and territories listed in that file can still be deleted simply by running Data Loader Delete multiple times until all child and parent territories are deleted. Depending on the order of territories in the CSV file, if the lower nodes are sequenced before the parent nodes, they will be deleted. The Data Loader error file for each pass will contain the remaining Hierarchy records that were not successfully processed. Continue to process the territories listed in each new error file, iteratively, and it will whittle away at the hierarchy tree until all the Territories are deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.