0
String str = 'hellotest';
String qry = 'select id,Products__c from Account where End_Date__c > :system.today() and Start_Date__c < :system.today() and IsValid__c = true and Approved__c =' + '\'' + 'Yes' + '\'';
        //order by createddate desc
        if(!str.equals('NoPromo')){
            qry = qry + ' and teststr__c = :str' ;
            qry = qry + ' and Offer_Type__c ='  + '\'' + 'Promo Code Campaign' + '\'';
        }
            qry = qry + ' order by createddate desc';
            system.debug(qry);
                for(Account pc: Database.Query(qry)){}

When i am trying to execute the above logic it is throwing the error

System.QueryException: unexpected token: '('

Can someone please help me where i am doing wrong here

2

You cannot use complex Apex binds (expressions like System.today()) in Dynamic SOQL. Dynamic SOQL only support simple binds to Apex variables.

Assign the dates you need to use to Apex variables, e.g.,

Date today = System.today();

and then bind to those variables:

where End_Date__c > :today
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.