incident count
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 08:29 AM - edited 07-23-2024 08:38 AM
Hi All,
In my instance, I have two tables:
- User Incident
- Software Incident
On the User Incident table, there is a field called "Related Software Incident."
On the Software Incident table, there is a related list called "User Incidents."
The query is:
When a user creates or updates a record with a value in the "Related Software Incident" field on the User Incident form and saves it, that incident will appear in the "User Incidents" related list on the Software Incident form.
I need to update the total count of these incidents in the "Total Users Count" field on the Software Incident form based on the incidents displayed in the related list.
How can we achieve this using business rules?
Here is the scripting using
after update business rule on user incident table
Condition : Related Software Incident changes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 08:39 AM
Hi @Akhil32 ,
To achieve this, you can use a business rule in ServiceNow. You'll create a business rule on the `User Incident` table to update the `Total Users Count` field on the related `Software Incident` record whenever a `User Incident` is created or updated with a value in the `Related Software Incident` field. Here's a step-by-step guide:
1. **Create a Business Rule on the `User Incident` Table:**
- Navigate to **System Definition > Business Rules**.
- Click **New** to create a new business rule.
2. **Define the Business Rule:**
- **Name**: Update Total Users Count
- **Table**: User Incident
- **Advanced**: Check the "Advanced" checkbox.
3. **Conditions and Script:**
- Set the condition to trigger when the `Related Software Incident` field is updated.
- Write a script to update the `Total Users Count` field on the `Software Incident` record.
Here's an example script for the business rule:
function executeRule(current, previous /*null when async*/) {
// Check if the Related Software Incident field has a value
if (current.related_software_incident) {
// Query to count the number of User Incidents related to the Software Incident
var userIncidentCount = new GlideAggregate('user_incident');
userIncidentCount.addQuery('related_software_incident', current.related_software_incident);
userIncidentCount.addAggregate('COUNT');
userIncidentCount.query();
if (userIncidentCount.next()) {
var count = userIncidentCount.getAggregate('COUNT');
// Update the Total Users Count field on the Software Incident record
var softwareIncident = new GlideRecord('software_incident');
if (softwareIncident.get(current.related_software_incident)) {
softwareIncident.total_users_count = count;
softwareIncident.update();
}
}
}
})(current, previous);
4. **Set When to Run:**
- **When**: Before
- **Insert**: Check this box.
- **Update**: Check this box.
5. **Save the Business Rule.**
### Explanation:
- The script checks if the `Related Software Incident` field on the `User Incident` record has a value.
- It then counts the number of `User Incidents` related to the specified `Software Incident` using a `GlideAggregate` query.
- Finally, it updates the `Total Users Count` field on the related `Software Incident` record with the count.
This business rule ensures that whenever a `User Incident` is created or updated with a related `Software Incident`, the `Total Users Count` on the `Software Incident` form is updated accordingly.
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 08:47 AM
Hi astik,
thanks for the quick reply. It' getting only single count even though we have two user incidents tagged to the parent incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 08:53 AM
@asifnoor - have a look once if you have time