Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
soql = 'select A__c,EmplId__c, B__c,Date__c from Transaction__c Where Transaction__c.name!=null';

    if (!branch.equals(''))
    system.debug('test br1');
      soql += ' and A__c LIKE \''+String.escapeSingleQuotes(branch)+'%\'';

    if (!ordernum.equals(''))
      soql += ' and B__c LIKE \''+String.escapeSingleQuotes(ordernum)+'%\'';
    if (!emplid.equals(''))
      soql += ' and EmplId__c LIKE \''+String.escapeSingleQuotes(emplid)+'%\'';
    if (!paybillcode.equals(''))
      soql += ' and PB_Code__c  LIKE \''+String.escapeSingleQuotes(paybillcode)+'%\'';  
    if (!wkenddate.equals(''))

    soql += ' and Date__c = wkenddate';  

I am getting an error when binding Date. can someone tell me what is the correct syntax?

share|improve this question

4 Answers 4

If you are referencing an Apex variable in a SOQL statement, remember to prefix the variable name with :.

Try switching

 soql += ' and Date__c = wkenddate';  

to

 soql += ' and Date__c = :wkenddate';  
share|improve this answer

Two ways to accomplish this:

Binding Statement

soql += ' and Date__c = :wkenddate'; // Include the ':'

Note: DOES NOT WORK with expressions, such as Map.keySet(), array[index], etc. Only simple primitives. You can use sets and lists, however, provided you mean to use the entire list.

Formatting

soql += ' and Date__c = '+DateTime.newInstance(wkenddate, Time.newInstance(0, 0, 0, 0)).format('yyyy-dd-MM'); // Make sure it's normal SOQL formatted.
share|improve this answer
4  
I have to protest that the :var format doesn't work with complex types. It absolutely does, but does not evaluate expressions. See salesforce.stackexchange.com/a/3527/60 –  ca_peterson Dec 19 '13 at 20:52
1  
I usually add UTC to the DateTime too: String myStart = dateTimeVar.format('yyyy-MM-dd')+'T00:00:01.000Z'; –  mast0r Dec 19 '13 at 21:03
    
@ca_peterson That's really more of what I meant. You can bind to myArray, but not myArray[0], or myMap.keySet(). Anything "complex." I'll clarify with your verbiage, as I like that better anyways. We are talking about the same thing, though. –  sfdcfox Dec 19 '13 at 21:15
1  
@mast0r That's correct for Date/Time fields, but the time part is implied for simple date fields and so not strictly necessary. –  sfdcfox Dec 19 '13 at 21:17

I just noticed this response today. In the meantime i ended up having a workaround.

However today I tried the suggestion by @sfdcfox , I get an error DateTime not defined.

So I tried the following and getting the error 'Error: Variable does not exist mydate'

public void runQuery() {

            try {
             system.debug('see how the SOQL is printed:' +soql);   
                obj=Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 1000 ');
            } catch (Exception e) {
              ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
            }
    }

public PageReference runSearch() {

String weekenddate = Apexpages.currentPage().getParameters().get('weekenddate'); Date mydate = Date.parse(weekenddate);

soql = 'select Name, ,,** Date_c from Object_c Where Object__c.name!=null ' ;

if (!weekenddate.equals('')) soql += ' and Date__c = :mydate';

runQuery();

return null; }

I am wondering what am i doing wrong?

Thanks.

share|improve this answer

Just bind using the regular syntax:

List<Transaction__c> = [select A__c, EmplId__c, B__c, Date__c from Transaction__c
                        where Date__c = :wkenddate];

The only snag with this is that the variable used for the binding must be local to the method's scope, so if wkenddate is a class member you'll want to do something like this:

Date theDate = wkenddate;
List<Transaction__c> = [select A__c, EmplId__c, B__c, Date__c from Transaction__c
                        where Date__c = :theDate];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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