While writing test class, I am unable create the data that connects AccountShare, Group and Territory. Group and Territory are related by the field relatedID on Group which is read only. Has anybody tried or implemented this scenario before? Thanks in advance. Here is my Apex class below:

  Public Static Void updateTerritoryUserIdField(List<Id> accountIds){      
    //Declare map & List Variables
    Map<Id, List<AccountShare>>  mapAcctAndUserOrGroupIds = new Map<Id, List<AccountShare>>();//Map of AccountId and AccountShare Records 
    Map<Id, Id>  mapUserOrGroupIdAndRelatedTerr = new Map<Id, Id>();//Map of groupId and TerritoryID 
    Map<Id, List<UserTerritory2Association>> mapContactAndTerritories = new Map<Id, List<UserTerritory2Association>>();//Map of TerritoryId and UserId
    Map<Id, String>  mapUserIdStringAndAccountId = new Map<Id, String>();//Map of userIDString and AccountId to be updated on fields of contact and Inquiry        
    List<Id> userOrGroupidsList = new List<Id>();        
    List<AccountShare> listOfAccountShares = new  List<AccountShare>();        
    String territoryUserIDsString ='';        
    //Query in AccountShare object 
    listOfAccountShares =  [Select AccountId, UserOrGroupId, RowCause from AccountShare where AccountId IN :accountIds and RowCause='Territory'];
        for(AccountShare acctShare : listOfAccountShares){
            mapAcctAndUserOrGroupIds.put(acctShare.AccountID, new  List <AccountShare> { acctShare });
    //Create list of userOrGroupIds from mapAcctAndUserOrGroupIds        

    for(Group groupRecord : [Select Id, RelatedId from Group where Type='Territory' and Id In: userOrGroupidsList]){
        mapUserOrGroupIdAndRelatedTerr.put(groupRecord.Id, groupRecord.RelatedId);             

    //Query in UserTerritory2Association object            
    List<UserTerritory2Association> listOfUsers = [Select Territory2Id, userId from UserTerritory2Association where Territory2Id In: mapUserOrGroupIdAndRelatedTerr.values()];       
    //Create map for UserTerritory2Association
    for(UserTerritory2Association userTerr : listOfUsers){
            mapContactAndTerritories.put(userTerr.Territory2Id, new List <UserTerritory2Association> { userTerr });
    for(Account acct: [select Id,Name from Account Where Id In: accountIds]){ 
     List<UserTerritory2Association> userDtls = new  List<UserTerritory2Association>();
     List<AccountShare> accountSharesList = new List<AccountShare>();
     List<String> userIds = new List<String>();            
        accountSharesList = mapAcctAndUserOrGroupIds.get(acct.id);            
        for(AccountShare accountshare : accountshareslist) {
            // Get the group from the share
            id groupid = accountshare.userorgroupid;
            system.debug('groupid : ' +groupid);                
            // Get the territory from the group map
            id territoryid  = mapUserOrGroupIdAndRelatedTerr.get(groupid); 
                    userDtls = mapContactAndTerritories.get(territoryid);

            for(UserTerritory2Association userDtl : userDtls){  
                String modifiedUserId = userDtl.userId;                 
                modifiedUserId = (modifiedUserId).substring(3); 

        territoryUserIDsString = convertListToString(userIds);
        mapUserIdStringAndAccountId.put(acct.Id, territoryUserIDsString);                      
     List<Contact> contactRecs = [select Id,Territory_user_Ids__c,AccountId from Contact where AccountId In:accountIds];
     List<Inquiry__c> InquiryRecs = [select Id,Territory_user_Ids__c,Sold_To__c from Inquiry__c where Sold_To__c In:accountIds];  
     List<Opportunity> oppRecs = [select Id,Territory_user_Ids__c,AccountId from Opportunity where AccountId In:accountIds]; 
    for(Contact contact :contactRecs){  
        contact.Territory_user_Ids__c = mapUserIdStringAndAccountId.get(contact.AccountId);        
    Database.SaveResult[] savedContactList = Database.update(contactRecs, false);
    for(Inquiry__c inquiry : InquiryRecs){ 
        inquiry.Territory_user_Ids__c = mapUserIdStringAndAccountId.get(inquiry.Sold_To__c);
    Database.SaveResult[] savedInquiryList = Database.update(InquiryRecs, false);
     for(Opportunity opp : oppRecs){ 
        opp.Territory_user_Ids__c = mapUserIdStringAndAccountId.get(opp.AccountId);
    Database.SaveResult[] savedOppList = Database.update(oppRecs, false);

  • I'd suggest providing the actual code itself if you can with an edit. – Kris Goncalves Nov 22 '19 at 22:26
  • depending on what the underlying code does (and hence what the test needs to do), you may be able to mock SObjects using Json.deserialize and use a Selector layer between the code and the SOQL. Then you mock the selector to return sobjects. – cropredy Nov 23 '19 at 21:44
  • Please edit your post to include the relevant code and a clear description of your issue and any error messages. – David Cheng Nov 24 '19 at 17:17

Your Answer

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

Browse other questions tagged or ask your own question.