function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tstrongtstrong 

Reporting on Opportunities in My Team's Territories

I need to build a report with a filter that shows not only the opportunities in my territory, but those in all of my team's territories.  The problem is that there is no standard way of doing this in a report.  There is an idea on the IdeaExchange for this (http://ideas.salesforce.com/article/show/10088301/My_territory_teams_opportunities_view_filter_needed_for_opportunity_reports).

 

Has anyone built something custom to address this need yet?

 

I am designing a trigger on the Territory object that will collect all of the parent Territories and the assigned users to populate a "TerritoryMgr" field with the manager of that Territory and all of the parent Territory managers (assigned users).  I'm struggling with the code though.  Below is what I have so far.  Not sure how good it is since it's a compilation of code I found online.

 

 

trigger TerritoryManagerUpdate on Territory (before insert, before update) { // Create a list of maps - each entry represents a 'generation' of parents in the parent/child hierarchy // (Territory, parent, grandparent, great-grandparent, etc.) List<Map<Id, Territory>> listTerritoryMaps = new List<Map<Id, Territory>>(); // Start with the Territories from the trigger list - put them in a map, indexed by Id. Map<ID, Territory> mapTerritoriesToQuery = new Map<ID, Territory>((List<Territory>)Trigger.new); // Create a list of parent ids - used for single query to receive parent generation Territories. List<ID> listParentTerritoryIds = createListOfParentTerritoryIdsFromTerritoryMap(mapTerritoriesToQuery); while (listParentTerritoryIds.size() > 0) { // Use a single database query to get the parent Territory records, put results in a map, and add the map to the list. // FYI - need to set appropriate Select Fields. Map<ID, Territory> mapParentResults = new Map<ID, Territory>([SELECT ID, ParentTerritoryId FROM Territory WHERE Id IN :listParentTerritoryIds]); listTerritoryMaps.add(mapParentResults); // Create a list of Parent ids using the results from the previous query. listParentTerritoryIds = createListOfParentTerritoryIdsFromTerritoryMap(mapParentResults); } // Now iterate the trigger.new list and perform parent look-ups via the list of generational Territory maps. for (Integer i = 0; i<Trigger.new.size(); i++) { Territory terrUltimateParent = (Territory)Trigger.new[i]; ID idParent = terrUltimateParent.ParentTerritoryId; // Iterate the list of maps until you find a null parent id. for (Integer j=0; (idParent != null && j<listTerritoryMaps.size()-1); j++) { terrUltimateParent = listTerritoryMaps[j].get(idParent); idParent = terrUltimateParent.ParentTerritoryId; } // Right here, terrUltimateParent should hold the ultimate parent for Trigger.new[i] - //do whatever needs to be done. } private List<ID> createListOfParentTerritoryIdsFromTerritoryMap(Map<ID, Territory> mapTerritories) { List<ID> listParentTerritoryIds = new List<ID>(); // Iterate map to create list of parent ids. for (Territory terr : mapTerritories.Values()) { listParentTerritoryIds.add(terr.ParentTerritoryId); } return listParentTerritoryIds; } }

 

 

 

 

 

Any help you can provide would be super!

 

Thanks!