0

I'm doing a Date comparison for NOW() on SOQL for javascript button but getting a MALFORMED QUERY Error:

     var d = new Date(); 
     alert(d);  
     var relatedrecords = sforce.connection.query("SELECT Id,(SELECT Id from Child_Records__r where Start_Time__c > d) from Order where Id = '{!Order.Id}'"); 
     var records = relatedrecords.getArray("records"); 
     if(records != null && records[0] != null){ 
        alert("Cannot cancel future records exists"); 
       }
1
  • 1
    Can you update your question to include an exact quote of the full text of the error message? Can you also put a screenshot of the alert box that you are generating so we can see it's output? Lastly, have you compared the output from your alert to this documentation to verify the format is correct? Commented Aug 23, 2018 at 20:33

1 Answer 1

3

There's two issues I can see:

First, you're outputting "d" directly into your query, you are not adding the value of the date. You'd fix that by doing the following:

var relatedrecords = sforce.connection.query("SELECT Id,(SELECT Id from Child_Records__r where Start_Time__c > " + d + ") from Order where Id = '{!Order.Id}'");

Second, you need to verify that the value of d outputs in the correct format. You can verify that by comparing your alert output to this documentation.

d.toISOString();

This should do the trick (thanks sfdcfox)

1
  • 1
    d.toISOString() so you don't have to import jQuery plus an additional library. Commented Aug 23, 2018 at 21:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.