+ Start a Discussion
ALAL 

SOQL Query And/Or Help

Hello, I'm trying to create a soql query to pull account records that have a type of sub-customer. Additionally I'd like the accounts to not be linked to an association record  OR the accounts can be linked to a system record where the system record's server type is retired. Below is the following query I've created, but I get an error message of unexpected token: OR. Any help would be appreciated. 

SELECT Id, Name FROM Account WHERE Type = 'Sub-Customer'AND ID not IN (Select Account__c from Associations__c)
 OR ID IN SELECT Primary_Account__c, Server_Type__c from System__c where Server_Type__c == 'Retired'
Best Answer chosen by AL
Sunil RathoreSunil Rathore
Hi,

Let's keep it simple. Use the following code snippet to keep all the query separated instead of using in the where clause.
 
List<Associations__c> lstAssociate = new List<Associations__c>([Select Account__c from Associations__c]);
List<System__c> lstSystem = new List<System__c>([SELECT Primary_Account__c, Server_Type__c from System__c where Server_Type__c == 'Retired']);

SELECT Id, Name FROM Account WHERE (Type = 'Sub-Customer') AND (ID NOT IN: lstAssociate OR ID IN: lstSystem);

You can change AND/OR condition as per your requirement.

Hope this will help you. If does then mark it as the best answer so it can also help others in the future.

Many Thanks,
Sunil Rathore