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
srinu namani 12srinu namani 12 

I have a lastSignificantInteraction__c field in my task object. I need to write a soql query which returns records with lastSignificantInteraction__c < CreatedDate. How can i write Query for this with this condition

Does this works?

List<Task> Lst = [select Id from Task where lastSignificantInteraction__c  < CreatedDate];
KaranrajKaranraj
Srinu - Salesforce doesn't allow direct field to field comparison in SOQL query.To achieve this you may create a formula field that will compare fields and return a value (like true or false) which you may use in a WHERE clause.  Create a formula field of type Boolean in the Task object with the name eg.,DateCondtion with the below formula
IF(lastSignificantInteraction__c  < CreatedDate, true, false)
Now your query will look like this and it will return the task record lastSignificantInteraction__c < CreatedDate
List<Task> Lst = [select Id from Task where DateCondtion = true];
Waqar Hussain SFWaqar Hussain SF
List<Task> tasks = [Select lastSignificantInteraction__c , CreatedDate from Task ];
List<String> querytaskids = new List<String>();
for (task t : tasks){
if(t.lastSignificantInteraction__c != null && t.lastSignificantInteraction__c  < t.CreatedDate){
querytaskids.add('\''+t.Id+'\'');
}
String soql =  'select id, title lastSignificantInteraction__c , CreatedDate from Task where Id IN '+querytaskids;
List<Task> finalTasks = Database.query(soql);