0

Here I am trying to filter the the records based on the output provided by the first soql, not getting the result I am looking for.

public class DMLTest {
    public void DMLtesting() {
        List<Object> mdu = [SELECT Name FROM sumchans__AddressReportMATeam__c group by Name];
        List<sumchans__AddressReportMATeam__c> units = new List<sumchans__AddressReportMATeam__c>();
        for(Object c: mdu) {
            List<sumchans__AddressReportMATeam__c> addr = [select Id, Name from sumchans__AddressReportMATeam__c where Name in 'c']; 
            units.add(addr);        
        }
    }
}
2

Your second SOQL, is referring to the String constant 'c' instead of actual record. That means its trying to find all records having name 'C'. You have to use the field from 1st SOQL to solve your issue.

ie :

 List<sumchans__AddressReportMATeam__c> addr = [select Id, Name from sumchans__AddressReportMATeam__c where Name =:c.Name]; 

That being said, Assigning List from SOQL to List doesn't seem a good idea as you cant access fields without typecasting.

Also you are doing SOQL in for loop.. which is a big red flag in Salesforce ecosystem.

public class DMLTest {
    public void DMLtesting() {
        List<AggregateResult> mdu = [SELECT Name FROM sumchans__AddressReportMATeam__c GROUP BY Name];
        List<String> nameList = new List<String>();
        for(AggregateResult c: mdu) {
            nameList.add((String)c.get('Name'));        
        }
        //Then 2nd SOQL after for loop

        List<sumchans__AddressReportMATeam__c> units = [SELECT Id, Name FROM  sumchans__AddressReportMATeam__c
                                                        WHERE Name IN : nameList]; 

        System.debug(units);

    }

}
0

I suggest you remove the GROUP BY clause from the SOQL and use SET instead of List in the above code. Replace

    List<String> nameList = new List<String>(); 

with

    Set<String> nameSet = new Set<String>();

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.