0

I have the following piece of code:

map<id,List<Spot__c>> spotMap = new map<id,List<Spot__c>>();
List<application__c> apPList = [SELECT ID, Account__r.id,(SELECT id,status__r.name FROM Spot__r ) FROM application__c WHERE Account__r.id in :scope];
       
       Set<id> apPIdSet = new Set<id>();
       for(application__c app: apPList){
           spotMap.put(app.id, app.Spot__r );   
       }

How do I iterate over the map and get the status__r.name value

I need to count all the same statuses of all the related applications and update these on the Account. The account then shows the total count for each status over all related applications. Since I cannot do this with for loop in a for loop I though I had to this with a map. I now get how to iterate over the spotMap but still having some issue to relate that to the related account.

1
  • 1
    As a side note, thanks in advance (TIA) is generally considered unacceptable behavior. You can see the network's behavior expectations, specifically: Do not use signature, taglines, greetings, thanks, or other chitchat. Your gratitude is assumed. 😁
    – sfdcfox
    Commented Jul 21, 2023 at 13:09

1 Answer 1

3
String[] names = new String[0];
for(Spot__c spotRecord: spotMap.values()) {
  names.add(spotRecord.Status__r.Name);
}

Or you can substitute with a Set, if you just want the unique values.

However, it should be noted that this code will crash if you query records with more or less than exactly 1 child record. Spot__r here is a list of records, and only works the way you expect because of the mechanic that allows you to assign a query-type List to a single value:

Account record = [SELECT Id FROM Account LIMIT 1];

Make sure your code is accounting for this fact.

Alternatively, you may want to actually use a Map<Id, List<Spot__c>>, which will allow you to get all the values.

Finally, if you just care about the status, you might want a query like:

SELECT Application__c Id, Status__r.Name status, COUNT(Id) recordCount
FROM Spot__c
WHERE Application__c IN :scope
GROUP BY Application__c Id, Status__r.Name
5
  • Please bare with me since I tried to use Map<Id, List<Spot__c>> which I updated in the ticket im not sure how to get the value what should the names array become then?
    – Thomas
    Commented Jul 21, 2023 at 13:50
  • @Thomas for(Spot__c[] spotRecords: spotMap.values()) { for(Spot__c spotRecord: spotRecords) { names.add(spotRecord.Status__r.Name); } }
    – sfdcfox
    Commented Jul 21, 2023 at 13:59
  • @Thomas It'd be helpful if you explained, though, what you're trying to do, specifically. What are you going to do with these values?
    – sfdcfox
    Commented Jul 21, 2023 at 14:00
  • I need to count all the same statusses of all the related applications and update these on the Account. The account then shows the total count for each status over all related applications. Since I cannot do this with for loop in a for loop I though I had to this with a map. I now get how to iterate over the spotMap but still having some issue to relate that to the related account. I updated the ticket.
    – Thomas
    Commented Jul 21, 2023 at 14:21
  • @Thomas Sounds like you want an aggregate result query (see the SOQL at the end of my answer). That would be the most efficient way to get the data you want.
    – sfdcfox
    Commented Jul 21, 2023 at 15:04

You must log in to answer this question.

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