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
mjohnson-TICmjohnson-TIC 

SOQL Query.....

I can't find what I am doing wrong here though I have done it 10000 time before, something is not returning correctly.

 

I have an Custom_Object__c with fields Status__c and Type__c

 

I have 10 records Status__c = 'Open' and Type__c = 'New'

I have 20 records Status__c = 'Closed' and Type__c = 'New'

 

I run this query and for some reason am running into this problem

 

for(Custom_Object__c c: [Select Status__c from Custom_Object__c where (Status__c = 'Open' OR Status__c = 'Closed') and Type__c = 'New']){}

 

This query 10 rows are returned

 

for(Custom_Object__c c: [Select Status__c from Custom_Object__c where (Status__c = 'Closed' OR Status__c = 'Open') and Type__c = 'New']){}

 

This query 20 rows are returned.....any ideas whats wrong with this logical OR statement that is not returning all 30 rows?

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Nothing looks wrong in your query but just try this for now

 

Set<String> setStatus = new Set<String>{'Open' , 'Close'};
for(Custom_Object__c c: [Select Status__c from Custom_Object__c where Status__c in: setStatus and Type__c = 'New']){}

 let me know with the results.

All Answers

Ankit AroraAnkit Arora

I tried same thing on my org :

 

List<Account> accLst = [select id from account where (Name = 'MyAnkit1' OR Name = 'MyAnkit2') and Day__c = 'Thursday'] ;
System.debug('accLst ::: ' + accLst.size()) ;

 

And it is working absolutely fine, wondering why not working at your end. Hope there is not any data issues on your org.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

Nothing looks wrong in your query but just try this for now

 

Set<String> setStatus = new Set<String>{'Open' , 'Close'};
for(Custom_Object__c c: [Select Status__c from Custom_Object__c where Status__c in: setStatus and Type__c = 'New']){}

 let me know with the results.

This was selected as the best answer
mjohnson-TICmjohnson-TIC

Lists seem to return all results, but doesn't explain why the OR statements are not returning the expected results. I probably have hundreds of lines of code where I have used OR statements in the past and retrieved the expected result - I will go back and check if those no longer work as well.

 

Shashikant SharmaShashikant Sharma

I also have used OR statement with success I will need to check also. But I would request you to mark it as solution so that in case some one other also gets into same issue he has a solution to move ahead. So please mark it as Accepted solution so that otehrs can also benifitted from it.

Ankit AroraAnkit Arora

This is strange, will this solution work for the date fields as well???

 

The problem is why OR is not working. As it is working at our end then why not yours?

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

mjohnson-TICmjohnson-TIC

Very sorry for this, the query I was running was an actually much more complex version of what I stated. I did alot of data manipulation to create a complex binding query to return a result and realized there was an extra space before one of my text literal values so it was not returning results of that value. Thanks to those who looked into it - sorry for wasting your  time.

Shashikant SharmaShashikant Sharma

No issues we all do these things. Thanks for updating on it. 

Ankit AroraAnkit Arora

That's what I was wondering. There is nothing extra done using a set or a list instead of OR then what exactly should be the problem. And have already notified you that it must be data issue.

 

Anyways, thanks for sharing this. I really appreciate it.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page