Using script in widget filter

JJG
Kilo Guru

Hello,

I have a service portal page with a 'data table from instance definition' widget. It displays records from my 'hours logged' table. I need to use a script to filter the records that it shows, however, I am getting a strange result.

Filter Script:

javascript:var name = name_of_employee;
var date = date;
var getStart = new GlideRecord('x_utsll_time_manag_time_management');
getStart.addQuery('name_of_employee', name);
getStart.query();
if(getStart.next()){
var start = getStart.pay_period_start
};
date > start; //Only records that are after the getStart.pay_period_start date should be shown
name_of_employee=javascript:gs.getUserDisplayName(); //Only records that match the currently logged in user name

 

find_real_file.png

 

RESULT:

find_real_file.png

 

1 ACCEPTED SOLUTION

Both javascript statements are valid. So if your name_of_employee is a string field and not a reference, and if pay period end is Date field and not Date/Time, then this looks good.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

This out-of-the-box widget is not designed to use a query like such. Only filters like:

active=true^assigned_toDYNAMIC932905532f270090b0c2d5ea2799b6f6^EQ

or

assigned_toLIKEjavascript:gs.getUserID()^EQ

or

assigned_toLIKEjavascript:new test_filter_sp().filter()

etc.

So not sure if you can fulfill your requirement with this. Though, you could use a dynamic filter or javascript or script include like in the examples above. Could that be a possibility?

So if you could set up a Script Include which returns comma seperated string of sys_ids, then you could use that Script Include with code like:

sys_idINjavascript:new your_script_include().your_function()

 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

okay, what about this:

name_of_employee=javascript:gs.getUserDisplayName()^pay_period_end>javascript:new GlideDate();

made it more simple by adding the pay_period_end date field to the table that is having its records displayed. Now I just need records where the name matches and the end date is in the future.

Possible?

Both javascript statements are valid. So if your name_of_employee is a string field and not a reference, and if pay period end is Date field and not Date/Time, then this looks good.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Thank Mark!

this ended up working for me:

name_of_employee=javascript:gs.getUserDisplayName()^pay_period_end>=javascript:gs.endOfToday()