How to get number of incident records for a user in User form?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2020 03:22 AM
I would like to display the incidents count by particular user(i.e incidents caller_id=User's sys_id) on the User form. Can anyone help me how to achieve that?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2020 04:11 AM
Hi,
Write a SI and call that under default value like below
var getIncidentsCount = Class.create();
getIncidentsCount.prototype = {
initialize: function() {},
getIncCount: function() {
var agg = new GlideAggregate('incident');
agg.addQuery('caller', gs.getUserID());
agg.addAggregate("COUNT");
agg.query();
if (agg.next()) {
count = agg.getAggregate("COUNT");
if (count > 0)
return count;
}
return 0;
},
type: 'getIncidentsCount'
};
new getIncidentsCount().getIncCount();
Mark the comment as a correct answer and helpful if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2020 11:35 PM
Hi,
Thanks for your reply. It looks like gs.getUserID() returns the currently logged in user's sys_id. But, I would like the count displayed for all users with this script. Any thoughts?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2020 02:49 AM
Hi,
you can pass the user sysid like below
javascript: var answer=new getIncidentsCount().getIncCount(current.sys_id);answer;
And in your SI make these changes and check
var getIncidentsCount = Class.create();
getIncidentsCount.prototype = {
initialize: function() {},
getIncCount: function(user_sysid) {
var agg = new GlideAggregate('incident');
agg.addQuery('caller', user_sysid);
agg.addAggregate("COUNT");
agg.query();
if (agg.next()) {
count = agg.getAggregate("COUNT");
if (count > 0)
return count;
}
return 0;
},
type: 'getIncidentsCount'
};
Mark the comment as a correct answer and also helpful if it helps to solve your problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2020 02:50 AM
Hi,
Open the Incident Count Dictionary Form in advanced view and paste the below code in calculation box:
(function calculatedFieldValue(current) {
var inc=new GlideRecord('incident');
inc.addQuery('caller_id',current.sys_id);
inc.query();
var count = inc.getRowCount();
return count;
})(current);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2020 03:08 AM
Hello Jahnavi,
Why can't you used background script,like
var gr=new GLideRecord('incident');
gr.addQuery('caller_id','<sys_id_of_user_who want to find incident of in quote>');
gr.query();
gs.info('Total incident-'+gr.getROwCount());
This will help you to find exactly you are searching for.
Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Reach out to me if any more help required.
Regards
Yash.K.Agrawal