How to limit a user to create only 5 incident a day

Anamika Thakur
Tera Contributor

Hi All,

 

We have a requirement to limit the creation of incident in a day for a user.

A user can only create five incident a day for a particular category and on 6th attempt an error should pop up.

How can I achieve this one.

Any help would be appreciated. Thanks in Advance.

2 ACCEPTED SOLUTIONS

-Andrew-
Kilo Sage

Hi Anamika,

 

You can create a Business Rule that runs on the creation of the incident and checks the number of incidents created by the user for the specified category:

 

  1. Create a new Business Rule
  2. In the "When to run" section, select "Before" and "Insert" options
  3. In the "Advanced" section, add the following code:

 

(function executeRule(current, previous /*null when async*/) {
    var category = current.category; // replace with your incident category field name
    var user = current.opened_by; // replace with your incident user field name
    var limit = 5; // set the limit to 5 incidents per day

    var incidentCount = new GlideAggregate('incident');
    incidentCount.addQuery('category', category);
    incidentCount.addQuery('opened_by', user);
    incidentCount.addAggregate('COUNT');
    incidentCount.query();
    incidentCount.next();
    var count = incidentCount.getAggregate('COUNT');

    if (count >= limit) {
        gs.addErrorMessage("You have already created " + count + " incidents for this category today. You cannot create more than " + limit + " incidents per day.");
        current.setAbortAction(true);
    }
})(current, previous);

 

  • Replace "category" and "user" with the actual field names for the incident category and user, respectively
  • Set the "limit" variable to the desired limit for the number of incidents per day

 

Hopefully this helps you.

View solution in original post

OlaN
Giga Sage
Giga Sage

Hi,

Although I'm having trouble understanding why you would want to limit users from reporting more than 5 incidents a day, I can still help you on the way (but feel free to elaborate on the requirement).

You could set up a business rule for this.

 

Something like below should work:

Business rule that runs before insert, no conditions.

Also, I would like to point out, that this solution will also prohibit a fulfiller user from creating max 5 incidents per day, which seems unwise 😀
You might want to tweak it, so that it only runs for non ITIL users.

 

(function executeRule(current, previous /*null when async*/) {

	var userID = current.getValue('opened_by');
	var incGR = new GlideRecord('incident');
	incGR.addQuery('opened_by', userID);
	incGR.addQuery('category', current.getValue('category'));
	incGR.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
	incGR.query();
	if (incGR.getRowCount() > 4){
		gs.addErrorMessage('Cannot create more than 5 incidents on same category on the same day');
		current.setAbortAction(true);
	}

})(current, previous);

 

View solution in original post

4 REPLIES 4

-Andrew-
Kilo Sage

Hi Anamika,

 

You can create a Business Rule that runs on the creation of the incident and checks the number of incidents created by the user for the specified category:

 

  1. Create a new Business Rule
  2. In the "When to run" section, select "Before" and "Insert" options
  3. In the "Advanced" section, add the following code:

 

(function executeRule(current, previous /*null when async*/) {
    var category = current.category; // replace with your incident category field name
    var user = current.opened_by; // replace with your incident user field name
    var limit = 5; // set the limit to 5 incidents per day

    var incidentCount = new GlideAggregate('incident');
    incidentCount.addQuery('category', category);
    incidentCount.addQuery('opened_by', user);
    incidentCount.addAggregate('COUNT');
    incidentCount.query();
    incidentCount.next();
    var count = incidentCount.getAggregate('COUNT');

    if (count >= limit) {
        gs.addErrorMessage("You have already created " + count + " incidents for this category today. You cannot create more than " + limit + " incidents per day.");
        current.setAbortAction(true);
    }
})(current, previous);

 

  • Replace "category" and "user" with the actual field names for the incident category and user, respectively
  • Set the "limit" variable to the desired limit for the number of incidents per day

 

Hopefully this helps you.

slimgam
Tera Contributor

Hi @-Andrew- 

 

i have a similar type of requirement, want to stop to incident creation through mail, and condition is we need to check last four hours, and more than 20 records , if that condition met need to stop creating incident.

 

could you please help me how to achieve this through inbound email action

OlaN
Giga Sage
Giga Sage

Hi,

Although I'm having trouble understanding why you would want to limit users from reporting more than 5 incidents a day, I can still help you on the way (but feel free to elaborate on the requirement).

You could set up a business rule for this.

 

Something like below should work:

Business rule that runs before insert, no conditions.

Also, I would like to point out, that this solution will also prohibit a fulfiller user from creating max 5 incidents per day, which seems unwise 😀
You might want to tweak it, so that it only runs for non ITIL users.

 

(function executeRule(current, previous /*null when async*/) {

	var userID = current.getValue('opened_by');
	var incGR = new GlideRecord('incident');
	incGR.addQuery('opened_by', userID);
	incGR.addQuery('category', current.getValue('category'));
	incGR.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
	incGR.query();
	if (incGR.getRowCount() > 4){
		gs.addErrorMessage('Cannot create more than 5 incidents on same category on the same day');
		current.setAbortAction(true);
	}

})(current, previous);

 

john343
Giga Contributor

To achieve this requirement, you would need to implement some logic on the server side that tracks the number of incidents created by each user for a particular category. Here's a simple algorithm you could use:

When a user creates an incident for a particular category, retrieve the number of incidents they have created for that category on the current day.
If the number of incidents is less than 5, allow the user to create the incident and increment the incident count for the category.
If the number of incidents is equal to 5, prevent the user from creating the incident and display an error message.
At the end of the day, reset the incident count for each category for each user.
Here's some sample code in Python that implements this algorithm using a dictionary to keep track of the incident count for each user and category:

pythonCopy code
from datetime import datetime # initialize dictionary to keep track of incident counts incident_counts = {} def create_incident(user_id, category😞 # get today's date as a string today_str = datetime.now().strftime('%Y-%m-%d') # generate a key for the user and category key = f'{user_id}-{category}-{today_str}' # get the current incident count for the key, defaulting to 0 count = incident_counts.get(key, 0) if count < 5: # allow the user to create the incident and increment the count incident_counts[key] = count + 1 print('Incident created') else: # prevent the user from creating the incident and display an error message print('Error: you have reached the maximum number of incidents for this category today') # example usage create_incident('user123', 'category1') # should print 'Incident created' create_incident('user123', 'category1') # should print 'Incident created' create_incident('user123', 'category1') # should print 'Incident created' create_incident('user123', 'category1') # should print 'Incident created' create_incident('user123', 'category1') # should print 'Incident created' create_incident('user123', 'category1') # should print 'Error: you have reached the maximum number of incidents for this category today'

Note that this implementation uses the current date to determine when to reset the incident count. If you need to support multiple time zones or have other requirements around the reset time, you may need to modify the implementation accordingly.