0

I'm doing a SOQL query and am trying to use a field from that result in a 2nd SOQL query but it errors with "Initial term of field expression must be a concrete SOBJECT: LIST". Code is:

List<Case> allCaseWithMac = [SELECT Subject, MAC_Address__c FROM Case WHERE MAC_Address__c !=''];
List<Hardware__c> allHwProb = [SELECT Name FROM Hardware__c WHERE Name in :allCaseWithMac.mac_address__c ]

I can make it work by adding a loop to explicitly add the MAC addresses into their own LIST as below but was wondering if there's a better way? I looked at semi-join but that can only supports IDs.

List<Case> allCaseWithMac = [SELECT Subject, MAC_Address__c FROM Case WHERE MAC_Address__c !=''];
List<String> macs = new List<string>();
for (case c : allCaseWithMac) {
  macs.add(c.mac_address__c);
}
List<Hardware__c> allHwProb = [Select Name FROM Hardware__c Where Mac__c IN :macs];

Just wondering if there are better options?

2

allCaseWithMac is actually not a list of Strings, this is why you get the error specified. Your solution is pretty much the only way of doing this, if you are not using relations anyway (doesn't seem that way).

What you might do to improve performance is use a Set instead of a List - this automatically removes duplicates. Also use query locators (for loop over query) to improve query performance.

Set<String> macs = new Set<String>();
for (Case c : [SELECT MAC_Address__c FROM Case WHERE MAC_Address__c !='']) {
  macs.add(c.mac_address__c);
}
List<Hardware__c> allHwProb = [SELECT Name FROM Hardware__c Where Mac__c IN :macs];

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.