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
Ankit Gupta SFDCLearnerAnkit Gupta SFDCLearner 

SOQL not working for custom object

Just want to fetch no of parents records who have no child records, I need it's SOQL. Below soql is running for standard object SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Opportunity)

I need it for custom object. Please try to solve.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ankit,

Greetings to you!

You can use the below query for custom objects:
SELECT Id, Name FROM Parent_Object__c WHERE Id NOT IN (SELECT Parent_Field_In_Child__c FROM Child_Object__c)

Suppose, I have Book__c parent object and Test__c child object. I have created a lookup relationship (or master-detail) in Test__c with the name BookMaster__c. I can use the below query to fetch the parent records with no child records:
SELECT Id, Name FROM Book__c WHERE Id NOT IN (SELECT BookMaster__c FROM Test__c)

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Ankit,
What is exactly query you are using can you please provide because I have checked with the same query as used for standard object, it is working fine.
Let Class__c is ParentObject and Student__c is Child object.
Please try this query:
SELECT Id, Name FROM Class__C WHERE Id NOT IN (SELECT Class__C FROM Student__c)
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi