The CreatorCon Call for Content is officially open! Get started here.

Scheduled Script Encoded Query Issue

Purushotham1992
Tera Contributor

 

 

I added the encoded query highlighted in orange below and my scheduled script is not working as expected. It does however run as a background script.

 

 

 

 

 

var gr = new GlideRecord('sys_user');
gr.addEncodedQuery("active=true^u_employee_type!=");
gr.addEncodedQuery('sys_created_on>javascript:gs.beginningOfLast60Days()^last_login_time>javascript:gs.beginningOfLast30Days()^ORlast_login_timeISEMPTY');
gr.query();
while (gr.next()) {
var grM = new GlideRecord('sys_user_grmember');
grM.addQuery('group', '24288fe5db9a2384682b9ee5db9619e7');
grM.addQuery('user', gr.sys_id);
grM.query();
if (!grM.next()) {
var grmb = new GlideRecord("sys_user_grmember");
grmb.initialize();
grmb.group = '24288fe5db9a2384682b9ee5db9619e7';
grmb.user = gr.sys_id;
grmb.insert();


}


}

 

 

Any help would be appreciated , thanks in advance  

6 REPLIES 6

Anand Kumar P
Giga Patron

Hi @Purushotham1992 ,
You can use below code

 

 

 

gr.addQuery('sys_created_on', '>', gs.beginningOfLast60Days());
gr.addOrQuery('last_login_time', '>', gs.beginningOfLast30Days());
gr.addOrQuery('last_login_time', 'ISEMPTY');

 

 

 

(or)

 

 

 

gr.addEncodedQuery('active=true^ORu_employee_type!=^sys_created_on>' + gs.beginningOfLast60Days() + '^last_login_time>' + gs.beginningOfLast30Days() + '^ORlast_login_timeISEMPTY');

 

 

 

 Please mark it as helpful if its works .

Thanks,

Anand

 

Purushotham1992
Tera Contributor

Still facing same issue , 

Jaspal Singh
Mega Patron
Mega Patron

Hi Purushotham,

Script seems fine. Can you add log statements for a check. There does not seems to be anything wrong with the query either in Backgroun script or used as a Scheduled Job.

Bert_c1
Kilo Patron

Hi Purushotham1992,

 

I tested your script in scripts background, and as Scheduled Job (onDemand). And got the same results. (I did remove the u_employee_type from the first addEncodedQuery(), and used a different group sys_id.) I don't see any problem. But using "gr" as a GlideRecord object can cause problems. I've seen that removing group members.

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0713029

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0819895

 

However, for me, all users were returned, so to get you 2nd encoded query right, go to a list view of sys_user records and build a filter condition that has sys_created_on and last_login_time values for those you want. Then 'copy query' the filter definition, to use in your script.