+ Start a Discussion
Abhishek Singh 88Abhishek Singh 88 

how to filter SOQL query

Hi developer,
I have a doubt regarding to filtering the SOQL query.
I have a three set of id's that is:
Set<id> source;
Set<id>destination;
Set<id> sharedserver;(contains id of source and destination as well)
I have wrote query 
baselist=[select id ,name from base element where id in:sharedserver And( not in:source And not in: destination)]

but its not giving exact output. Can anyone help me out?
Thanks.
Best Answer chosen by Abhishek Singh 88
Ravi Dutt SharmaRavi Dutt Sharma
Hi Abhishek,

When you add NOT condition in SOQL, it has a negative impact on the performance of the SOQL. I would suggest you to create another set which will have the values of sharedServer minus source minus destination and use that set in your SOQL. Below is the sample code on how to code it.
 
Set<String> source = new Set<String>();
source.add('A');
source.add('B');
Set<String> destination = new Set<String>();
destination.add('C');
destination.add('D');
// Has values from source and destination. Plus has some unique values as well
Set<String> sharedServer = new Set<String>();
sharedServer.add('A');
sharedServer.add('C');
sharedServer.add('E');
sharedServer.add('F');
// Copy values from sharedServer and then remove the values of source and destination
Set<String> uniqueValues = new Set<String>(sharedServer);
uniqueValues.removeAll(source);
uniqueValues.removeAll(destination);
System.debug('uniqueValues'+uniqueValues);