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

Hi Andres,




Thanks for reply. I have tried with double quotes and I dont know how it worked. This should not be the case as all other queries work with single quotes. Still confused. Do you have any idea?





Cheers,


Prasad


Hi Prasad



If anyone of ours answer was correct. Could you please mark it correct for others in future for referencing?



Thanks,


Malaya



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


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