0

We are trying to find tasks of the same Subject that occupy a lot of records. For example, there might be 4,000 tasks all titled "Opportunity closing in 3 days" and we want to find all instances where a single Subject is responsible for more than 1,000 records.

What I want to do is basically this:

SELECT subject 
FROM Task
having count(subject) > 1000 

But this results in a parsing error. Is my syntax just bad, it's more complicated than that, or can this not actually be done?

1
  • I has a try in my environment, and unfortunately you can't group by the Subject field on Task. Sep 12, 2023 at 21:12

1 Answer 1

0

It gives a parsing error (MALFORMED_QUERY) because you can only use HAVING clause after a GROUP BY one.

Moreover to know which subject is related to more than N records, you don't have to COUNT subject, you have to GROUP BY Subject and filter counting IDs since each one identify a record.

So it should have been

SELECT Subject
FROM Task
GROUP BY Subject 
HAVING COUNT(Id) > 1000

Once you got the Subjects, you could have added them to a list to query once again the task with those subjects to get their IDs.

Anyway that query won't work since you cannot GROUP BY Subject.
Reading Considerations for GROUP BY you can see that

Some object fields have a field type that doesn’t support grouping. You can't include fields with these field types in a GROUP BY clause.
[...]
To check whether a field supports grouping for standard objects, you can also check the properties for the field in the Object Reference for Salesforce and Lightning Platform

Than checking the reference for Task Object you'll see that Subject field lacks of Group property. Indeed if you try that query, you'll get

field 'Subject' can not be grouped in a query call

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .