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
OpsterOpster 

How can I select a list of tasks where the OwnerId != CreatedById?

I am trying to do the following and getting an error: "Unexpected Token"

 

List<

What is the correct way to select tasks where the Creator != Owner?

thanks

Task> ltsk = [SELECT id, subject, CreatedById, OwnerId FROM Task WHERE OwnerId != CreatedById];

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Unfortunately, SOQL does not currently support comparing two field values in the WHERE clause. Though you can't do the above directly in SOQL, I do have a neat trick for you courtesy of our resident Force.com guru (Simon). 

 

Create a Formula field on Task that compares the 2 fields. Something like

 

IF (OwnerId == CreatedById, "true", "false")

 

In your SOQL Query, just search on the Formula field instead:

i.e. 

 

List<Task> ltsk = [SELECT id, subject, CreatedById, OwnerId FROM Task WHERE my_custom_formula__c = 'false'];

All Answers

forecast_is_cloudyforecast_is_cloudy

Unfortunately, SOQL does not currently support comparing two field values in the WHERE clause. Though you can't do the above directly in SOQL, I do have a neat trick for you courtesy of our resident Force.com guru (Simon). 

 

Create a Formula field on Task that compares the 2 fields. Something like

 

IF (OwnerId == CreatedById, "true", "false")

 

In your SOQL Query, just search on the Formula field instead:

i.e. 

 

List<Task> ltsk = [SELECT id, subject, CreatedById, OwnerId FROM Task WHERE my_custom_formula__c = 'false'];

This was selected as the best answer
Bhawani SharmaBhawani Sharma

Thanks. it's really a superb solution.