Metric Definitions for first replies on sc_req_item

Yvan1
Tera Contributor

I'm having some difficulty with a scripted metric definition for sc_req_item that.

At first the field being monitored was "-None-" the being it would watch for any update to the record and we could capture if it was a comment or a work note with a specific starting string (ideally would have been for an email created via the email client in SN to the Requestor). This failed to trigger entirely.

Then the one Metric Definition was divided into two, one with field set to comments and another with field set to work_notes. These would trigger, but the below content test for those would fail:

 

example:
if(current.comment != '') //current.comment shows empty when not, so this fails

example:
if(current.work_notes.indexOf("example start string") == 0 ) //current.work_notes shows empty when not, so this fails

Is there a better way to do this?

2 REPLIES 2

Abhay Kumar1
Giga Sage

@Yvan1 Creating a scripted metric definition in ServiceNow for sc_req_item that monitors changes to comments and work notes can be tricky, especially when dealing with empty values. The goal is to capture specific changes and ensure your conditions evaluate correctly. 

You can use below code if helps you:

For Comments:

if (current.changes() && current.comment.changes() && current.comment != previous.comment) {

    // Check if the new comment is not empty

    if (current.comment && current.comment.trim() !== '') {

        // Trigger your metric definition or whatever logic you want here

        gs.info("Comment added: " + current.comment);

        // Optionally, check for specific string

        if (current.comment.indexOf("example start string") === 0) {

            // Do something specific for this case

        }

    }

}

 

For Work Notes:

if (current.changes() && current.work_notes.changes() && current.work_notes != previous.work_notes) {

    // Check if the new work note is not empty

    if (current.work_notes && current.work_notes.trim() !== '') {

        // Trigger your metric definition or whatever logic you want here

        gs.info("Work note added: " + current.work_notes);

        // Optionally, check for specific string

        if (current.work_notes.indexOf("example start string") === 0) {

            // Do something specific for this

case

        }

    }

}

Hope this will help you.

@Abhay Kumar1  I think the problem I'm seeing is less an update with empty values and more that the values that should be there are not registering as existing. For example: if I were to update work_notes or comments on my sc_req_item record with "this is text", the corresponding calls (current.comments or current.work_notes) would show empty or more specifically like so:

current.comment shows undefined
current.getValue("comment") shows as null
current.getDisplayValue("comment") shows as null

 

You'll see in the below screenshot I've tried combining the metric_definitions by leaving the "Field" as "--None--" on the metric_definition but I'm not even seeing the info log being triggered.

 

Yvan1_0-1730816242337.png

 

I've gone through the developer reference and learning documentation, and I've haven't noticed anything that explicitly states that the Field value must be set.