0

I am trying to filter out the null values of Account Id related to the corresponding child contacts in the following query, but it is not happening.

When I update the active__c checkbox field in the contact object for the record which doesn't have any associated parent account Id in the Account object, The column Count(Account.Name) is giving the problem by returning the null record having 0 value.

SELECT Account.Id, Count(Account.Name) Namecount FROM Contact WHERE Active__c = true AND 
Account.Id != NULL AND Account.Name != NULL GROUP BY Account.Id ORDER BY Account.Id

The same query when I run separately in the query editor is working fine and giving the expected results. (only Non zero Non-null values)

But, when I use this same query inside the SOQL in apex method, it is returning the Null values also(0 count record), and the output is impacted.

Can anyone please suggest how to get rid of these null value records inside SOQL queries?

1 Answer 1

0

I think, this is giving correct results in execute anonymous apex -

List < AggregateResult > cons = [SELECT Account.Id, Count(Account.Name) Namecount
FROM Contact WHERE Active__c = true AND Account.Id != NULL 
AND Account.Name != NULL GROUP BY Account.Id 
ORDER BY Account.Id];
System.debug(cons);

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.