We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

how to create dynamic encoded query

Krishna Nagara1
ServiceNow Employee

Hi ,  

Goal: to calculate incident opened in each month until the current month

Question: Can i use a dynamic placeholder( date ) in gs.dateGenerate() method.  

Details: I am trying to use gs.dateGenerate(placeholder for a date value) by supplying the argument dynamically. I am trying to construct a date with gs.monthsAgoStart and gs.monthsagoEnd . something like this....  

 

1> var startDate = gs.monthsAgoStart(7);

2> var endDate = gs.monthsAgoEnd(7);

3> var encodedQuery = "active=true^sys_created_onBETWEENjavascript:gs.dateGenerate(startDate)@javascript:gs.dateGenerate(endDate)";

4>       var gr = new GlideRecord('incident');

5>     gr.addEncodedQuery(encodedQuery);

6>       gr.query();  

 

On line 3 when i try and plug the variable startDate and endDate the query does not run. If is use gs.monthsAgoStart() directly it works. However my need is that I use a variable   something like this....          

 

for(int i=0;i<8;i++){

1> var startDate = gs.monthsAgoStart(i);

2> var endDate = gs.monthsAgoEnd(i);

3> var encodedQuery = "active=true^sys_created_onBETWEENjavascript:gs.dateGenerate(startDate)@javascript:gs.dateGenerate(endDate)";

4>       var gr = new GlideRecord('incident');

5>     gr.addEncodedQuery(encodedQuery);

6>       gr.query();   }

observe the i counter in monthsAgoStart() and monthAgoEnd() to calculate incident in different months.  

 

Any suggestions.help. Appreciated.    

2 REPLIES 2

acretion
Tera Expert

havent tried this, but try something like ...




var encodedQuery = "active=true^sys_created_onBETWEENjavascript:gs.dateGenerate(" + startDate + ")@javascript:gs.dateGenerate(" + endDate + ")";


Hi Chris,



Thanks for reply. Actually I found the issue...when you use gs.monthsAgosStart or any date time related funtion it is internally using gs.dateGenerate and I was again using the same in my encodedQuery,which is wrong. I tried this active=true^sys_created_onBETWEEN"+startDate+"@"+endDate; and it worked. Thanks to Abhishek Rakshe.