function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KR_ForceKR_Force 

How to loop through the map values... Compile Error: Loop must iterate over a collection type: SOBJECT:Asset

I'm actually trying to get the assets from the map based on account ID and loop through the values to get the Product_Family__c.

Map<ID,Asset>actAstMap=new Map<ID,Asset>();
for (Asset a1: actAstMap.get(ActID)){
if(a1.Product_Family__c!=null ||a1.Product_Family__c<>''){
        pFamily.add(a1.Product_Family__c);
}
}

Any help will be much apprecitated.
Best Answer chosen by KR_Force
Prabu MahalingamPrabu Mahalingam
Hi KR_Force,

Glad that it helped.

Now the new issue is becuase of your improper usage of  lActAgentcontMap

you have declared it as Map<ID,ID>lActAgentcontMap=new Map<ID,ID>();

which means that both the key and value are of type ID but in you 69 th line you have passed as follows 

lActAgentcontMap.get(lactID)

where lactID is a sObject of  type Linked_Account__c

so you have to replace that 69 th line as follows

for(Linked_Account__c lactID:actLActMap.get(c.AccountID)){aContID.add(lActAgentcontMap.get(lactID.Id));}   // I have replaced lactId to lact.Id


Cheers..!!


All Answers

kiranmutturukiranmutturu
Map<ID,Asset>actAstMap=new Map<ID,Asset>();
for (Asset a1: actAstMap.values()){
if(a1.Product_Family__c!=null ||a1.Product_Family__c<>''){
        pFamily.add(a1.Product_Family__c);
}
}
Asset a1: actAstMap.values() before you called this line you should fill the values in to the map actAstMap
Prabu MahalingamPrabu Mahalingam
Hi,
First thing you need to understand is that the for loop can iterate only using collections like  LIST, SET... In the above code you have used actAstMap.get(ActID)

which will return single Asset (which is not a collection).

To fix this you need to change the Map as follows

Map<ID,List<Asset>>actAstMap=new Map<ID,List<Asset>>();  // Replace the first line with this line

so now it retuns List of assets for the Account Id.


Hope This Helps...!!
Cheers
Vi$hVi$h
Hi ,

You can loop through a map using keyset function:
eg
Map<ID,Asset>actAstMap=new Map<ID,Asset>();
//Assume some values are put in the map
for (ID mapID:actAstMap.keyset() ){
 Asset a = actAstMap.get(mapID);
//use object a for your purpose
}
}

keySet():
Returns a set that contains all of the keys in the map.

Thanks,
Vishal
KR_ForceKR_Force
@Prabu..thanks for the hint...now i got struct at line 69...Error: Compile Error: Incompatible key type SOBJECT:Linked_Account__c for MAP<Id,Id> at line 69 column 79
Here is my code
  1. public static void ContactProductSubscription(List<Contact>newcontacts,set<ID>acIds){
  2.     //set<id>acIds=new set<Id>();
  3.     set<id>acIds1=new set<Id>();
  4.     list<Asset>ast=new List<Asset>();
  5.     List<Linked_Account__c>lActs=new List<Linked_Account__c>();
  6.     List<Linked_Account__c>linkedAccounts=new List<Linked_Account__c>();
  7.     //Map<Id,Id>actLActMap=new Map<Id,Id>();
  8.     Map<Id,List<Linked_Account__c>>actLActMap=new Map<Id,List<Linked_Account__c>>();
  9.     Map<Id,Id>agentctLActMap=new Map<Id,Id>();
  10.     Map<Id,ID>alActs=new Map<Id,ID>();
  11.     Map<Id,Id>conActMap=new Map<Id,Id>();
  12.     Map<ID,String>ActPfamilyMap=new Map<ID,String>();
  13.     Map<ID,ID>lActAgentcontMap=new Map<ID,ID>();
  14.     Map<ID,ID>lActAcontMap=new Map<ID,ID>();
  15.     Map<ID,List<Asset>>actAstMap=new Map<ID,List<Asset>>();
  16.     set<id>aContacts=new set<id>();//Agent Contacts
  17.     set<string>pFamily=new set<string>();
  18.     String psubscription;
  19.      for(contact c :newcontacts){conActMap.put(c.id,c.accountID);}
  20.         if(acids.size()>0){
  21.             lActs=[select Active__c, Agent__c, Name, Agent_Contact__c,Agent_Contact__r.account.ID,Agent_Contact_AccountId__c, Assets__c,
  22.             Cases__c, Contacts__c, Expiration__c, Linked_Account__c, Id, User_Account__c, User_Contact__c FROM Linked_Account__c
  23.             where Agent__c=true and  Contacts__c=true and (Expiration__c<>null OR Expiration__c >: date.today()) and Linked_Account__c in:acids And Linked_Account__c != Null and Agent_Contact__c != Null ];
  24.         }
  25.         if(lActs.size()>0){
  26.         List<Linked_Account__c> tLacts = new List<Linked_Account__c>();
  27.         List<contact> tAgconts = new List<contact>();
  28.         for(id aid1: acIds){
  29.         for(Linked_Account__c l:lActs){
  30.             aContacts.add(l.Agent_Contact__c);
  31.             if(l.Linked_Account__c==aid1){
  32.             //actLActMap.put(l.Linked_Account__c,l.id);
  33.             agentctLActMap.put(l.Agent_Contact__c,l.id);
  34.             tLacts.add(l);
  35.             //tAgconts.add();
  36.             }
  37.             actLActMap.put(l.Linked_Account__c,tLacts);
  38.             } 
  39.             }
  40.         }
  41.         if(aContacts.size()>0){
  42.             linkedAccounts=[select Active__c, Agent__c, Name, Agent_Contact__c,Agent_Contact__r.account.ID,Agent_Contact_AccountId__c, Assets__c,
  43.             Cases__c, Contacts__c, Expiration__c, Linked_Account__c, Id, User_Account__c, User_Contact__c FROM Linked_Account__c
  44.             where Agent__c=false and  isDeleted=false and Agent_Contact__c in:aContacts And Linked_Account__c != Null And Agent_Contact__c != Null];
  45.         }
  46.         if(linkedAccounts.size()>0){
  47.         For(Linked_Account__c l1:linkedAccounts){
  48.             acIds1.add(l1.Linked_Account__c);
  49.             lActAgentcontMap.put(l1.Linked_Account__c,l1.Agent_Contact__c);
  50.             lActAcontMap.put(l1.id,l1.Linked_Account__c);
  51.           }
  52.         }
  53.         acIds1.addAll(acIds);
  54.         if(acIds1.size()>0){ast=[select AccountId, Id, Product_Family__c FROM Asset where AccountId in:acIds1];}
  55.         System.debug('Assets**:'+ast);
  56.         List<Asset> tList = new List<Asset>();
  57.         for(id aid: acIds1){
  58.          for(Asset a: ast){
  59.           ActPfamilyMap.put(a.AccountId,a.Product_Family__c);
  60.           if(a.AccountId == aid){
  61.           tlist.add(a);
  62.           }
  63.           actAstMap.put(a.AccountId,tlist);
  64.           }
  65.          }
  66.         for(contact c :newcontacts){
  67.         List<ID> aContID;
  68.         List<ID> actID;
  69.         for(Linked_Account__c lactID:actLActMap.get(c.AccountID)){aContID.add(lActAgentcontMap.get(lactID));}
  70.         for(Id lactID1:agentctLActMap.get(aContID)){actID.add(lActAcontMap.get(lactID1));
  71.         ActID.add(c.AccountID);}
  72.         aContID.clear();
  73.         actID.clear();
  74.         if(ActID.size()>0){
  75.         for(Asset a1:actAstMap.get(actID)){
  76.         if(a1.Product_Family__c!=null ||a1.Product_Family__c<>''){
  77.         pFamily.add(a1.Product_Family__c);
  78.            }
  79.         }
  80.          System.debug('Product Family**:'+pFamily);
  81.          for(string s:pFamily){
  82.          psubscription+=s+'; ';
  83.          c.productSubscriptions__c=psubscription;
  84.           }
  85.         }
  86.         pFamily.clear();
  87.      } 
  88.   }
Prabu MahalingamPrabu Mahalingam
Hi KR_Force,

Glad that it helped.

Now the new issue is becuase of your improper usage of  lActAgentcontMap

you have declared it as Map<ID,ID>lActAgentcontMap=new Map<ID,ID>();

which means that both the key and value are of type ID but in you 69 th line you have passed as follows 

lActAgentcontMap.get(lactID)

where lactID is a sObject of  type Linked_Account__c

so you have to replace that 69 th line as follows

for(Linked_Account__c lactID:actLActMap.get(c.AccountID)){aContID.add(lActAgentcontMap.get(lactID.Id));}   // I have replaced lactId to lact.Id


Cheers..!!


This was selected as the best answer