How To Add A Worknote To An Incident With A Script

There are a couple of ways to add a worknote to an incident. Below, we’ll cover adding a work_note to an incident with a business rule, and then we’ll also cover how to add a …

adds a worknote to the current incident script

Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now

There are a couple of ways to add a worknote to an incident.

Below, we’ll cover adding a work_note to an incident with a business rule, and then we’ll also cover how to add a work_note to an incident, with a background script. We’ll iterate on the examples a little bit.

Before we get started:

Be careful when running background scripts, and always run them first in your development environment, especially if you’re modifying data. If you run a background script in production, you must be guaranteed it will succeed before hitting “Run”, because you’ve confirmed this in the development and test environments.

We’ll work with the current object in ServiceNow scripting, which is the current record that’s being CRUD’d. We’ll also cover GlideRecord queries and how to update other records with them.

We’ll use some very common GlideRecord queries that align with best practices in ServiceNow below.

Business Rule – Add A Work Note To The Current Incident

Go ahead and create a new business rule record, set the below values.

This will be in a before insert/update business rule. Table = incident. Advanced = true. Active = true.

This example will add a worknote to the current incident, whenever a user updates the state. This is a simple use case, and will expose you to the concept of a business rule and what it can do.

Business rules run a script when CRUD operations occur on tables. You can run the script before the update, after the update, in a background job (async), etc.

Condition:

current.state.changes();

Script:

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

    var user = gs.getUserDisplayName().toString();
    var state = current.state.getDisplayValue().toString();
    current.work_notes = user + " updated the incident to " + state;
    // current.work_notes = set any value here

})(current, previous);

Here is what that business rule would look like in ServiceNow.

adds a worknote to the current incident script

And finally, to test it out.

Go to any Incident record and update the state value.

If this was setup properly, you’ll have a worknote on the incident, as seen below:

Business Rule – Add A Work Note To A Different Incident

What if you wanted to add a worknote to a difference incident?

Maybe there’s a parent incident that you want to add a worknote to, or maybe even a related change record.

For our example, we’ll add a worknote to a parent incident.

Let’s add a worknote to a parent incident, when a new one has been set.

Here’s how you’d accomplish that.

Condition:

current.parent_incident.changes();

Script:

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

    var par = current.parent_incident.toString();
    var number = current.number;

    var gr = new GlideRecord("incident");
    gr.get(par);
    gr.work_notes = number + " has been set as a child incident";
	gr.update();

})(current, previous);

Give this a shot and let us know how it works out for you.

Background Script – Add A Work Note

For this, you will need to the know the sys_id or the number of the incident. Or the condition or type of incident your trying to set.

We need this because we need to define it as we build out our GlideRecord query.

Background script:

var inc = "85071a1347c12200e0ef563dbb9a71c1";

var gr = new GlideRecord("incident");
gr.get(inc);
gr.work_notes = "Adding a worknote from a background script";
gr.update();

This add a worknote to the incident with a sys_id of the variable inc.

Or maybe you want to add a work_note to every incident with a state of 2 (for some reason).

That would look like this script:

var gr = new GlideRecord("incident");
gr.addQuery("state", 2);
gr.query();

while(gr.next()) {
gr.work_notes = "Adding a worknote from a background script";
gr.update();
}

This adds a work note to every single incident with a state of 2. This is just an example and your use case will likely be different. If you want to limit the number of incidents, in your query building, add a gr.setLimit(5); to try it out on only 5 records. Or even change your while to an if, to try it out on 1.

You can take these examples, and scale them out to suite your business needs. As it’s likely that these exact examples, won’t align with your business requirements, but may be close.

Let us know below if we can help you build any queries or scripts out for you!



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x