Concepts or coding lessons of Salesforce that you can implement easily

Territory Management In Salesforce Can Increase Your Profit !!!

Territory management is important and critical to businesses of any size i.e. Large businesses, Medium and Small.
Territories align to sales team, defined on the basis of geography, sales potential, history, product-based, or a combination of factors.
With the help of Territory management, companies can make the most of sales team resources at minimum cost.

Territory management is an account sharing system that grants access to accounts based on the characteristics of the accounts. It enables your company to structure your Salesforce data and users the same way you structure your sales territories.

Mostly your salesforce organization has a private sharing model, and you want to grant access to accounts based on criteria then Territory management is a solution.

Key points of Territory Management:

1. An Opportunity can assigned to only one Territory.
2. One territory can have unlimited number of Users and Users can assign to any number of territories.
3. One territory can have unlimited number of Accounts and Accounts can assign to any number of territories.

Territory management in Opportunity object:


Requirement: Access TerritoryId of Opportunity in Apex class.
Solution: Easily you can access TerritoryId of Opportunity using Apex class. Once you enables Territory Management into salesforce organization then Territory lookup is created in Opportunity Object which is Standard Field.

Apex Code:

trigger OpportunityTerritoryManagement on Opportunity (before insert, before update) {
    if(Trigger.isBefore){
// Create Set which store opportunity`s territoryId
        Set<Id> setOpptyTerritoryIds = new Set<Id>();
        for(Opportunity objOppty : Trigger.New){
            if(Trigger.isInsert || Trigger.isUpdate){
              if(objOppty.TerritoryId != null){
                setOpptyTerritoryIds.add(objOppty.TerritoryId);
      }
            }
        }
        // Populate Territory details of assigned Territory of Opportunity.
        if(setOpptyTerritoryIds.size() > 0){
            Map<Id, Territory> mapOpptyTerritory = new Map<Id, Territory>([SELECT Id, Name, ParentTerritoryId, ForecastUserId, Description FROM Territory WHERE Id IN :setOpptyTerritoryIds]);
        }
    }
}


Territory management in Account object:


Requirement: Access TerritoryId of Account in Apex class.
Solution: This is not easy as Opportunity. You need to write extra code to access TerritoryId of Account using Apex class. If Territory Management is enable then Territory lookup create on Opportunity object not on Account object. You need extra queries on different object to access TerritoryId of Account.

Steps to Access TerritoryId of Account in Apex class:
1. Query in Account Share object.
2. Query in Group object from where access RelatedId.
3. Query in Territory object to Populate Territory details of assigned Territory of Account.

Apex Code:

trigger AccountTerritoryManagement on Account (before update) {
  if(Trigger.isBefore){
    // Create Set which store Account Territory Id
    Set<Id> setAccTerritoryIds = new Set<Id>();
    Map<Id, Id> mapAccountShare = new Map<Id, Id>();
    Map<Id, Id> mapGroup = new Map<Id, Id>();
    Map<Id, Territory> mapUserTerritory = new Map<Id, Territory>();
    
    //Query in Account Share object
    List<AccountShare> listOfAccountShare = [SELECT Id, UserOrGroupId, AccountId FROM AccountShare WHERE RowCause = 'Territory' AND AccountId IN :Trigger.newMap.keyset()];
    
    for(AccountShare objAcctShare : listOfAccountShare){
      mapAccountShare.put(objAcctShare.AccountId, objAcctShare.UserOrGroupId);         
    }   
    
    //Query in Group object from where access RelatedId
    List<Group> listOfGroup = [SELECT Id, RelatedId FROM Group WHERE Type = 'Territory' AND Id IN :mapAccountShare.Values()];
    
    //Map of Group object
    for(Group objGroupRecord : listOfGroup){
      mapGroup.put(objGroupRecord.Id, objGroupRecord.RelatedId);         
    }
    
    // Populate Territory details of assigned Territory of Account.
    if(!mapGroup.isEmpty()){
      //Query in Territory object
      Map<Id, Territory> mapTerritories = new Map<Id, Territory>([SELECT Id, Name, ParentTerritoryId, ForecastUserId, Description FROM Territory WHERE Id IN:mapGroup.Values()]);
    }
  }
}

If you want more details on Territory Management the check Salesforce Documentation.

Enjoy! If you have any questions, comments etc. please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

Next Post: Import CSV file In Salesforce using Apex in 3 Easiest Steps
loading...

No comments:

Post a Comment