1

Below code is returning null for cmap. I am trying to debug contacts as id, contact name pair inside for loop but getting null.

Map<Id, case> caseMap = new Map<Id,case>([SELECT id, accountId, priority from case LIMIT 1]);
list<Id> accountIds = new list<Id>();
for(Id c : caseMap.keyset()) {
   if(caseMap.get(c).priority != 'low'){
      accountIds.add(caseMap.get(c).id);
      system.debug('h:'+cmap);
   }
}
Map<Id, contact> cmap = new map<Id, contact>([select id, accountid from contact where accountid =: accountIds]); 
  • 2
    I don't see an IN keyword in your second query. – highfive Aug 6 '15 at 9:09
  • highfive, I forgot to write In before but updated my code now but still not working. Can you please look and suggest the necessary change. Thanks – sf_user Aug 6 '15 at 9:13
  • 1
    accountIds list is empty. So cmap will also be empty. Why are you adding case Id to the accountIds list? Your code has bugs.... What is your requirement? – Vignex Aug 6 '15 at 9:13
  • The other problem is here : accountIds.add(caseMap.get(c).id); You want to add the accountId instead of the case id. – martin Aug 6 '15 at 9:14
1

Your second query is to retrieve records from the collected Ids I guess. If so you have to use IN clause in your query.

Map<Id, case> caseMap = new Map<Id,case>([SELECT id, accountId, priority from case LIMIT 10]);
list<Id> accountIds = new list<Id>();
for(Id c : caseMap.keyset()) {
   if(caseMap.get(c).priority != 'low'){
      accountIds.add(caseMap.get(c).accountId);  //collect accountIds
   }
}
Map<Id, contact> cmap = new map<Id, contact>([select id, accountid 
                                              from contact where accountid IN : accountIds]); 
  • 2
    c is the case id... – martin Aug 6 '15 at 9:15
  • Yes, updated for the correct field – highfive Aug 6 '15 at 9:17
1

By the way, if you didn't need that case map or list of account ids for anything else, and you were able to replace LIMIT in the case query with WHERE, you could get the contact map in one step:

Map<Id, contact> cmap = new map<Id, contact>([
    select id, accountid 
    from contact 
    where accountid IN (SELECT accountId FROM case WHERE id = :someid)
]);
  • Thanks Martin.. This one seems efficient but my actual requirement is .. There is an accountId on case and I need to query this accountId's parent accountId and see its assets. I modified your code to my requirement as below: list<case> caseIds = [select Id from case limit 10]; Map<Id, asset> amap = new map<Id, asset>([select id, accountId from asset where accountid IN (SELECT account.parentId FROM case WHERE id = :caseIds)]); system.debug(amap); But it seems I cannot query parent in inner query. Can you correct me if I am wrong. Thanks – sf_user Aug 6 '15 at 9:56
  • @pmvsdt Right, the subquery cannot traverse relationships. It sounds like you will probably need to do more than one query here. – martin Aug 6 '15 at 10:06

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.