• Tyler Whitesides SD
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Salesforce Consultant

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
I am having some trouble bulkifying a class that inherently hits the database quite a bit.  This works fine for a single record, but bulk operations fail due to the high number of SOQL queries.

The goal of this class is to provide a UserId given an AccountId and a "Role" (which is actually the MasterLabel on the assigned Territory Type). 

I will be using this in a trigger when the Account updates.  Essentially I am trying to create a trigger that will update 3  Lookup(User) fields to populate the "Territory Team" depending on what function a particular person serves for that Account, as determined by Enterprise Territory Management.  Each Account record has several assigned territories, and each one represents a role, plus the person serving that role.

I know that this is available on the "Assigned Territories" related list, but unfortunately that is not reportable and you cannot automate anything off of it (i.e. you cannot change the Owner of an Event on creation depending on Territory role).  I need this sort of automation, hence having the fields populate with userId, pulled from territories.
 
public class UserbyAccountandTerritoryRole {
    public static Id getUserbyAccountandRole(Id acctId,String role){
        Id SCUser;
            try {
        //With AccountId, get ObjectTerritory2Association records to get TerritoryIds
        //With TerritoryIds get Territory2 records to get Territory2TypeIds
        List<Territory2> territories = [SELECT Id,Territory2TypeId FROM Territory2 WHERE Id IN (SELECT Territory2Id FROM ObjectTerritory2Association WHERE ObjectId = :acctId)];
        //With Territory2TypeIds get Territory2Type records MasterLabels to filter down to 1 TerritoryId
          	List<Id> territorytypeIds;
            for(Territory2 t : territories){
                territorytypeIds.add(t.Territory2TypeId);
            }
            List<Territory2Type> types = [SELECT Id,MasterLabel FROM Territory2Type WHERE Id IN :territorytypeIds];

        //Find Territory Type matching the Role
        Id SCterritoryTypeId;
                for(Territory2Type i : types){
                    if(i.MasterLabel == role){
                        SCterritoryTypeId = i.Id;
                    }
            }
        //Match the Territory2Type with the original Territory to find the User
        Id finalTerritory;
            for(Territory2 x : territories){
                if(x.Territory2TypeId==SCterritoryTypeId){
                    finalTerritory = x.Id;
                }
            }    
        //With last TerritoryId get UserTerritory2Association records to get UserId
        SCUser = [SELECT UserId FROM UserTerritory2Association WHERE Territory2Id = :finalTerritory].UserId;
          	   }
    catch (Exception ex) {
        system.debug('Uh oh an error occurred!');
    }
                return SCUser;
    }
}


I would also love to hear if I am just going about this the wrong way.
I am having some trouble bulkifying a class that inherently hits the database quite a bit.  This works fine for a single record, but bulk operations fail due to the high number of SOQL queries.

The goal of this class is to provide a UserId given an AccountId and a "Role" (which is actually the MasterLabel on the assigned Territory Type). 

I will be using this in a trigger when the Account updates.  Essentially I am trying to create a trigger that will update 3  Lookup(User) fields to populate the "Territory Team" depending on what function a particular person serves for that Account, as determined by Enterprise Territory Management.  Each Account record has several assigned territories, and each one represents a role, plus the person serving that role.

I know that this is available on the "Assigned Territories" related list, but unfortunately that is not reportable and you cannot automate anything off of it (i.e. you cannot change the Owner of an Event on creation depending on Territory role).  I need this sort of automation, hence having the fields populate with userId, pulled from territories.
 
public class UserbyAccountandTerritoryRole {
    public static Id getUserbyAccountandRole(Id acctId,String role){
        Id SCUser;
            try {
        //With AccountId, get ObjectTerritory2Association records to get TerritoryIds
        //With TerritoryIds get Territory2 records to get Territory2TypeIds
        List<Territory2> territories = [SELECT Id,Territory2TypeId FROM Territory2 WHERE Id IN (SELECT Territory2Id FROM ObjectTerritory2Association WHERE ObjectId = :acctId)];
        //With Territory2TypeIds get Territory2Type records MasterLabels to filter down to 1 TerritoryId
          	List<Id> territorytypeIds;
            for(Territory2 t : territories){
                territorytypeIds.add(t.Territory2TypeId);
            }
            List<Territory2Type> types = [SELECT Id,MasterLabel FROM Territory2Type WHERE Id IN :territorytypeIds];

        //Find Territory Type matching the Role
        Id SCterritoryTypeId;
                for(Territory2Type i : types){
                    if(i.MasterLabel == role){
                        SCterritoryTypeId = i.Id;
                    }
            }
        //Match the Territory2Type with the original Territory to find the User
        Id finalTerritory;
            for(Territory2 x : territories){
                if(x.Territory2TypeId==SCterritoryTypeId){
                    finalTerritory = x.Id;
                }
            }    
        //With last TerritoryId get UserTerritory2Association records to get UserId
        SCUser = [SELECT UserId FROM UserTerritory2Association WHERE Territory2Id = :finalTerritory].UserId;
          	   }
    catch (Exception ex) {
        system.debug('Uh oh an error occurred!');
    }
                return SCUser;
    }
}


I would also love to hear if I am just going about this the wrong way.
Hi All

The 'Define Forecast Rollups and Default Date Ranges' unit is giving me the following error when I have done exactly what is asked in the challenge, hence an apparent error in the Trailhead checking process:
Challenge Not yet complete... here's what's wrong:
Couldn’t find default forecast display period 'Monthly', starting on '1 month ago', display '4 months'. Please double check the instructions.

Is there some apparently irrelevant setting I need that is not specified? It's not as if this is a complex unit!

Thanks
Keith
 

Hi all, 

 

I am doing some work on flow and having some issues with mapping a multi select checklist into an object. 

I can create the field on the flow easily enough, but when i try and map it to a field in a custom object, I get no data returned. 

 

Anyone have any ideas I can try?!

 

Thanks

 

Matt