Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Add worknote to an incident

anvitha ash
Tera Contributor

Hi community,

Scenario:

I’ve implemented a scheduled job that runs every 10 minutes. This job checks for incidents where the Assigned to field is empty. If it's empty, the job calls a Script Include that:

 

1. Checks for any active users in the corresponding Assignment Group.

2. If no active users are found, it updates the Work Notes of the incident with the message: "No users were found."

 

This process continues every 10 minutes until an active user is found and the incident is assigned. Functionally, everything is working fine.

 

Issue:

Because the job runs every 10 minutes, the same work note "No users were found." is added repeatedly each time no user is found. This clutters the work notes

 

Goal:

I want to update the Work Notes with "No users were found." only the first time this condition is met — and avoid repeating the same message in subsequent job runs, until a user is eventually assigned.

 

How to achieve this? Thanks in advance 😃 

 

2 REPLIES 2

BillMartin
Mega Sage
Mega Sage

Hi @anvitha ash , your verifications and conditions is well written. my question to you is why do you need it to use a scheduled job? should you just use an event that calls your script that contains validation of the given field conditions are not achieved? there are quite a few ways of achieveing the desired action like onchange, onsubmit, flow designer etc.

Sandeep Rajput
Tera Patron
Tera Patron

@anvitha ash You can include following method to your script include to check if the incident already have a worknote with no user found if it doesn't then add the worknote.

 

checkAndLogNoUsers: function(incidentGr) {
	var groupId = incidentGr.assignment_group;

	// Check if any active user is part of the assignment group
	var memberGr = new GlideRecord('sys_user_grmember');
	memberGr.addQuery('group', groupId);
	memberGr.query();

	var foundActiveUser = false;

	while (memberGr.next()) {
		var userGr = new GlideRecord('sys_user');
		if (userGr.get(memberGr.user) && userGr.active == true) {
			foundActiveUser = true;
			break;
		}
	}

	// If no active users found, check if work note already exists
	if (!foundActiveUser) {
		var noteExists = false;

		var journalGr = new GlideRecord('sys_journal_field');
		journalGr.addQuery('element_id', incidentGr.sys_id);
		journalGr.addQuery('element', 'work_notes');
		journalGr.addQuery('value', 'CONTAINS', 'No users were found.');
		journalGr.query();

		if (journalGr.hasNext()) {
			noteExists = true;
		}

		if (!noteExists) {
			incidentGr.work_notes = 'No users were found.';
			incidentGr.update();
		}
	}
}

 

Hope this helps.