You need to sign in to do that
Don't have an account?

SOQL Query for Contacts with no Accounts
Hey all,
I am trying to perform a SOQL query in my controller to pull all Contacts in my org that have no associated AccountId. Somehow some of my users, without access to the API, were able to create orphaned Contacts (Contacts without an Account selected upon creation). This is breaking some custom Apex/VF we have programmed because of various Exceptions including "de-reference null object", et al.
I tried searching the web for some answer to this, but couldn't come up with anything. When I perform the SOQL query it returns no information. As soon as I add an account to the Contact in question it pulls perfectly with the query.
Can anyone tell me if what I am trying to do is even possible? Is there a restriction with SOQL that only allows Contacts to be pulled IF they have an Account associated with them? Is there any viable workaround?
Thanks!
Adam
I am trying to perform a SOQL query in my controller to pull all Contacts in my org that have no associated AccountId. Somehow some of my users, without access to the API, were able to create orphaned Contacts (Contacts without an Account selected upon creation). This is breaking some custom Apex/VF we have programmed because of various Exceptions including "de-reference null object", et al.
I tried searching the web for some answer to this, but couldn't come up with anything. When I perform the SOQL query it returns no information. As soon as I add an account to the Contact in question it pulls perfectly with the query.
Can anyone tell me if what I am trying to do is even possible? Is there a restriction with SOQL that only allows Contacts to be pulled IF they have an Account associated with them? Is there any viable workaround?
Thanks!
Adam
Can you please post your code here ?
Thanks!
Based upon your question
"Somehow some of my users, without access to the API, were able to create orphaned Contacts (Contacts without an Account selected upon creation). "
You need to create a validation rule or make account field on contact required on page layout so that users cannot create orphaned contacts.
Or you will need to modify your controller where
query will look like
Can you confirm/deny that thought?
Thanks!
PS - Your query suggestion wouldn't work because I need to pull the opposite, where accountId = null
For this, you have to filter contact whose AccountId is NULL.
The query looks like:-
List<Contact> conList=[SELECT Id,LastName,AccountId FROM Contact WHERE AccountId=NULL];
System.debug('ConList---->: '+conList);
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi