How to get number of incident records for a user in User form?

Jahnavi6
Kilo Expert

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?

 

9 REPLIES 9

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.

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?

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.

Preethi24
Tera Contributor

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);

 

find_real_file.png

 

Yash Agrawal1
Tera Guru

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