addEncodedQuery() for date field not working

prasadpagar
Kilo Contributor

Hi All,

I am running following query in backgroud script to validate if Encoded query takes date field. I formed this query using list filter ->   copy query but its not working. Any suggestion how between operator to enter in encoded query string.

My code:

var tableData = new GlideRecord('incident');

tableData.addEncodedQuery('sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2');

tableData.query();

gs.addInfoMessage("Count: " +tableData.getRowCount());

I am getting this error:

Output:

Javascript compiler exception: missing ) after argument list (<refname>; line 2) in:

var tableData = new GlideRecord('incident');

tableData.addEncodedQuery('sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2');

tableData.query();

gs.addInfoMessage("Count: " +tableData.getRowCount());

Thanks in Advance.

Regards,

Prasad

1 ACCEPTED SOLUTION

Prasad,



You've gotten the answer to your issue, but here my attempt to explain the why it works. Your issue had to do with the single-quote as part of the date specification:


sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)



You wrapped the input to the addEndcodedQuery function in single quotes like this: addEncodedQuery('sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2')



The function had trouble parsing the input because Javascript would break that apart by items encapsulated between the single-quotes which would break down like this:


input1: sys_created_onBETWEENjavascript:gs.dateGenerate( // String used as query but would fail due to incompleteness of the query, which aligns with the error you were seeing


input2: 2015-10-07 // only Ints and vars can be passed this way, so the string would have caused an error, discarded because unexpected input


input3: , // String but no handler because addEncodedQuery is looking for a single input, discarded because unexpected input


input4: 00:00:00 // only Ints and vars can be passed this way, so the string would have caused an error, discarded because unexpected input


input5: )@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2 // String but no handler because addEncodedQuery is looking for a single input, discarded because unexpected input



All because Javascript tried to break the inputs into chunks using the single-quote delimiter.   By using double quotes to wrap the string you get the single input you were looking to pass to the addEncodedQuery function: addEncodedQuery("sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2")



input1: sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2



Hope that helps your understanding



View solution in original post

7 REPLIES 7

Malaya
Giga Expert

Query needs to be in double quotes.



tableData.addEncodedQuery("sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2");



Thanks,


Malaya


Thanks Malaya. I have tested with 99 other queries and they worked fine with single quotes. Do you know the reason why only BETWEEN dosen't work with single quotes?


That shouldn't be the case.. i am assuming you generated encoded query using copy query function..



Just put that in



tabledata.addencodedquery("enter your query here as is what you have copied");



find_real_file.png



Thanks,


Malaya



PS - Please mark Helpful, Like, or Correct Answer if applicable.


asilva3
Mega Expert

I suspect it's you're quoting in line 3.



Build your encodedQuery seperate from the addEncodedQuery:



var myQuery = "sys_created_onBETWEENjavascript:gs.dateGenerate('2015-10-07','00:00:00')@javascript:gs.daysAgoEnd(0)^priority=1^severityIN1,2";


tableData.addEncodedQuery(myQuery);