function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hamza akouayrihamza akouayri 

Need to run a report to get all Accounts that doesn't have attachments

Need to run a report to get all Accounts that has close won opportunities and don't have attachments related to them
i am not sure if i can approach this using report or SOQL

what is the best approach to use here

Thanks in Advance
AbhishekAbhishek (Salesforce Developers) 
Since there is a polymorphic relation between attachment and the object it's related to, I don't think you'd be able to do it with a report. Maybe SOQL, depending on your data volume:
SELECT ID FROM Account WHERE ID NOT IN (SELECT ParentID FROM Attachment)

A more efficient, future proof method of doing it would be to mark your accounts as having or not having attachments using a trigger on the attachment object. The trigger would control a checkbox that you would use to report. 

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
hamza akouayrihamza akouayri
i have used this query :

SELECT Id, Name,(SELECT stageName FROM Opportunities where stageName = 'Closed Won'),(SELECT Title,ParentId FROM CombinedAttachments) FROM Account  WHERE ID NOT IN (SELECT ParentId FROM CombinedAttachments )

but I keep getting this error I try to add filter using recordType = 'Account' didn't work as well 

User-added image