0

I have a silly problem, when I pass Date from lightning input (type=date) i pass it as a string, and when i creating a query with this paramiter i recived error like below

value of filter criterion for field 'StartDate' mu…of type date and should not be enclosed in quotes

So when i converted to Date type using Date.parse i recived error code

Invalid date: 2020-03-12

But when i convert String to Date using Date.valueOf

no viable alternative at character ''

here is my code

  if(!key.equals(START_DATE)) {
                        query += key + ' = ' + QUOTES + String.valueOf(inputMap.get(key)) + QUOTES + ' AND ';
                    }else{
                        startDate = Date.valueOf(inputMap.get(key));
                        query += key + ' = ' +startDate+ ' AND ';
                    }

And here's an example of the generated SOQL:

SELECT Id, AccountId ,CustomerSigned.Name, Name, Status, ContractNumber, Account.Name, Account.BillingCity, Education_Level__c, isActive__c, StartDate, ActivatedDate, EndDate, Category__c, Performance_Evaluation__c
    FROM Contract
    WHERE Account.Name = 'TestName' AND StartDate = 2020-03-12 00:00:00
3
  • Please add an example of what the generated query looks like and check the documentation for what a literal date value looks like.
    – Phil W
    Mar 26, 2020 at 8:39
  • >SELECT Id, AccountId ,CustomerSigned.Name, Name, Status, ContractNumber, Account.Name, Account.BillingCity, Education_Level__c, isActive__c, StartDate, ActivatedDate, EndDate, Category__c, Performance_Evaluation__c FROM Contract WHERE Account.Name = 'TestName' AND StartDate = 2020-03-12 00:00:00
    – artist
    Mar 26, 2020 at 8:41
  • If at all possible you should generate your query using binding variables. If you can't do that you will need to ensure that you use escapeSingleQuotes for any user-originated data you insert into the query.
    – Phil W
    Mar 26, 2020 at 8:43

1 Answer 1

1

As can be seen from the documentation, the required format for a date literal is:

YYYY-MM-DD

To generate this, use:

startDate.format('yyyy-MM-dd')

As mentioned in the comments on your question, you also need to ensure you escape single quotes in any string values you insert into the query, since you are not using bindings (if you don't you are open to SOQL injection hacks):

String.escapeSingleQuotes(String.valueOf(inputMap.get(key)))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .