-1

I have below code where I need Map<ID,Set<String>>, but I am unable to assign that value and it shows error. Can someone please help me?

Apex Code // User - Terr Name

Map<Id,String> Uterr = new Map<Id,String>();
List<Territory> terr = [select Name, Id from Territory];
For(Userterritory ut : [select userId,Territoryid from Userterritory where UserId in: userIds])
{
  for(Territory t: terr)
  { if(t.Id==ut.TerritoryId)
    Uterr.put(ut.UserId,t.Name);
  }

}
    Map<Id,Set<String>> accTerrMap = new Map<Id,Set<String>>();

    for(Account_Team_Member_vod__c con : trigger.new) {

    accTerrMap.put(AccplanAcc.get(con.Account_Plan_vod__c),Uterr.get(con.Team_Member_vod__c));
    } 
3
  • 1
    What is the error? May 15, 2017 at 12:53
  • IN this statement the 2nd parameter value must be a List not String: accTerrMap.put(AccplanAcc.get(con.Account_Plan_vod__c),Uterr.get(con.Team_Member_vod__c)); May 15, 2017 at 12:54
  • 1
    Don't use a loop in a loop: for(UserTerritory ut: [SELECT UserId, Territory.Name FROM UserTerritory WHERE UserId IN :userIds]) { uterr.put(ut.UserId, ut.Territory.Name); } or Map<Id, Territory> terrs = new Map<Id, Territory>([SELECT Name FROM Territory]); for(UserTerritory ut: [SELECT UserId, TerritoryId FROM UserTerritory WHERE UserId IN :userIds]) { if(terrs.containsKey(ut.TerrtitoryId) { uterr.put(ut.UserId, ut.Territory.Name); } }
    – sfdcfox
    May 15, 2017 at 12:56

1 Answer 1

3

Problem is that for Map<Id, Set<String>> for every new Id, value (which is Set should be initialised separately, before records are added into it.

For example:

Map<Id,String> Uterr = new Map<Id,String>();
List<Territory> terr = [SELECT Name, Id FROM Territory];
for(Userterritory ut : [SELECT UserId, Territoryid 
                        FROM Userterritory
                        WHERE UserId in :userIds]) {
    for(Territory t: terr) { 
        if(t.Id==ut.TerritoryId) {
            Uterr.put(ut.UserId,t.Name);
        }
    }
}

Map<Id,Set<String>> accTerrMap = new Map<Id,Set<String>>();

for(Account_Team_Member_vod__c con : Trigger.new) {
    Id key = AccplanAcc.get(con.Account_Plan_vod__c);
    if (!accTerrMap.contains(key)) { 
        // In case if there are no records for given key in Map 
        //    -- add empty set
        accTerrMap.put(key, new Set<String>());
    }
    //Add Team_Member_vod__c into corresponding set (which exists for sure)
    accTerrMap.get(key).add(Uterr.get(con.Team_Member_vod__c));
} 

P.S. Also, as mentioned by @sfdcfox in comments, it possible to avoid double for loop in first part of the code by querying all the territory data in one request

SELECT Id, Territory.Name FROM UserTerritory WHERE UserId IN :userIds

As a result, code above will look like next:

Map<Id,String> Uterr = new Map<Id,String>();
for(Userterritory ut : [SELECT UserId, Territory.Name 
                            FROM UserTerritory
                            WHERE UserId in :userIds]) {
    Uterr.put(ut.UserId,ut.Territory.Name);
}
3
  • But territory.name is not working here May 15, 2017 at 13:25
  • 1
    @SFDC_Learner - Define "Not Working"
    – Eric
    May 15, 2017 at 14:07
  • It simply says Territory.name not valid May 15, 2017 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .