Sunday, 30 October 2011

How to compare date in Dynamic SOQL

First convert the date value into datetime value, because format method can't used with the Date Type.
Date orig = customPage.startDate;


DateTime dtConverted = Datetime.newInstance(orig.year(),orig.month(),orig.day(),0,0,0);
       
System.debug(' converted date ' + dtConverted.format('yyyy-MM-dd\'T\'h h:mm:ss\'Z\'');




String qry = 'Select id, name, CreatedDate from Account ' +
        'where createddate > ' +  dtConverted.format('yyyy-MM-dd\'T\'h h:mm:ss\'Z\'') + ' order by name ';


List<Account> searchResults = Database.query(qry);

1 comment:

  1. The debug line has a missing ')', and the format has a blank space between the two "h"s.
    Thanks for the solution!

    ReplyDelete