- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2019 02:45 PM
Hi there,
how can I get the Head_count field in the Departments table to be calculated based on the total of users with that department selected. This needs to be updated perhaps once every 24 hours.
Script examples would be very much appreciated 🙂
regards
Paul
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2019 05:53 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2019 04:42 PM
Hi, please go to the dictionary entry for head_count and change it to a calculated value (must click advanced view to see this).
Then insert the following script
(function calculatedFieldValue(current) {
var record = new GlideAggregate("sys_user");
record.addAggregate("COUNT");
record.addQuery("department",current.getUniqueValue());
record.query();
if(record.next()){
return record.getAggregate("COUNT");
}else{
return 0;
}
})(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2019 03:04 PM
I would go one step further and also include this before
record.query();
to only show active users.
record.addQuery("active=true");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 06:32 AM
Hi
Thanks for the solution!
Any possibility of storing it on the real table field?
My problem here is that the field HC is pretty much useless when filtering or ordering the table (I think because calculating this way is dynamic...)
If I want to use it somehow, I have to download to excel and operate with it...
Regards!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 06:35 AM
Hi
Thanks for the solution!!
Is there any possibility of storing it on table "cmn_department" field?
My problem is that i cant filter or order by HeadCount field because of this dynamic calculation...
I have to export information to excel and operate then...
Regards!