2

How do you iterate over a map?

   //Create State Abbreviation to State Count map
        map <string,decimal>    mapStates   = new map<string,decimal>();

        //Get Employee Counts for each state
        Account accStates = [Select AK__c,AL__c,.....
                             FROM Account WHERE ....];

        //Put those counts into map. 
        mapStates.put('AK', accStates.AK__c);
        //49 other states added to map. 

//Loop through map and save only states that have employees
        for (string key : mapStates.keySet()) {
            if( ?? decimal value is blank ){
                 map.removekey???            
            }
        }
3
  • Why dont you filter those values in the SOQL query ? I would add one thing to the solution below. I dont think it is possible to remove an item from a map while you are iterating over it.
    – ransommule
    Feb 14 '17 at 16:10
  • You can remove it, here is the code for my example: for (string key : mapStates.keySet()) { Decimal mapValue = mapStates.get(key); if(mapValue == 0){ mapStates.remove(key); } }
    – Matt M
    Feb 14 '17 at 16:31
  • Have a look into my answer, for your long term solution Feb 14 '17 at 16:43
6

In your pseudocode, you're iterating over the Map's keyset.

To get the corresponding value from the map, you need to call get() just like you normally would.

for(String key :myMap.keySet()){
    Decimal mapValue = myMap.get(key);
}

That said, it seems that you're running into a bit of an X-Y problem here.

Although my solution answers your question as worded, seeing as this is your second question regarding the same general piece of code, your asking this question is a sign that you're trying to solve the underlying problem with an ill-fitting solution.

If you find yourself continuing to struggle with whatever it is you're trying to accomplish in the end, then perhaps it's time to take a step back and re-consider the data model/architecture that you're using.

Having 50 fields to hold counts of employees for each state is a red flag to me. My first instinct would be to create a junction object instead. If you'd like to go further here, you can either ask a new question (with the 'big picture' of what you're trying to accomplish), or I can start up a chat room.

1
  • Thanks @DerekF, I am just running into my normal struggle of that I don't write Apex enough on a regular basis so I forget how to accomplish certain things. I don't call myself a developer due to this fact. I try to use the declarative solutions as much as possible before I resort to Apex.
    – Matt M
    Feb 14 '17 at 16:09
0

If you like to maintain dynamic behavior, this could be an approach.

1) Create a custom metadata type which will contain Country Initial and Country API Name.

Country Meta data

2) Upload all the country initials and API name into that.

3) Now loop through the metadata values and match with the API name from accStates and do your processing logic.

//Create State Abbreviation to State Count map
Map<String, String> countryMapping = Map<String, String>();

//Get Employee Counts for each state
Account accStates = [Select AK__c,AL__c,.....
                     FROM Account WHERE ....];


for(Country_API_Mapping__mdt mdt: [SELECT CountryInitial__c, Country_API__c FROM Country_API_Mapping__mdt])
{
    //49 other states added to map. 
    countryMapping.put(mdt.CountryInitial__c, mdt.Country_API__c);
}

//Loop through map and save only states that have employees
for (String apiName : countryMapping.values()) 
{
    if(accStates.get(apiName) == null)
    {
        //do your processing
    }   
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.