How to add Work Notes only when Urgency is updated by an external system, and avoid duplicate notes?

Joshua Prakash
Tera Expert

Hi everyone,

I'm working on a transform map that updates Incidents from an external system, and I’ve written an onBefore transform script to handle one specific requirement.

Goal:

  • Add a Work Note like "Urgency updated to <value> by External System"

  • Only when the Urgency value has actually changed

  • Avoid adding duplicate notes if the same urgency value is sent again

To achieve this, I’m:

  • Checking the most recent Work Note to ensure the note hasn’t already been added

  • All of this logic is in the onBefore transform script

Question:
Is this the right way to handle such logic in a transform map?
Are there any recommended best practices when using onBefore scripts like this to avoid unnecessary updates or duplicate notes?

 

Code:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
 
    var incGR = new GlideRecord('incident');

    if (incGR.get(target.sys_id)) { // Fetch existing incident
        var oldUrgency = incGR.urgency.toString();
        var newUrgency = target.urgency ? target.urgency.toString() : oldUrgency;

        // Only proceed if urgency is actually changed        

        if (newUrgency !== oldUrgency) {
            // Check if the last work note was already added by the external system
            var grJournal = new GlideRecord('sys_journal_field');
            grJournal.addQuery('element_id', incGR.sys_id);
            grJournal.addQuery('element', 'work_notes');
            grJournal.orderByDesc('sys_created_on');
            grJournal.setLimit(1);
            grJournal.query();

            var lastWorkNote = "";
            if (grJournal.next()) {
                lastWorkNote = grJournal.value.toString();
            }

            var externalNote = "Urgency updated to " + newUrgency + " by External System";
            if (lastWorkNote !== externalNote) {

                // Assign to work_notes field in Transform Map            
                target.work_notes = externalNote;
            }
        } else {

            // If Urgency has not changed, prevent unnecessary updates            
            target.urgency = oldUrgency;
        }
    }
})(source, map, log, target);
1 ACCEPTED SOLUTION

UI policies nor client scripts apply to transform maps as those things only apply to interactive user sessions via the browser 

View solution in original post

3 REPLIES 3

Kieran Anson
Kilo Patron

Hi,

I would advise against adding these sorts of automated work notes as they cause noise in the activity feed. Instead, display the urgency field as part of the activity feed. Field changes will then be displayed

Joshua Prakash
Tera Expert

If Work Notes are mandatory (due to a UI Policy or Client Script) when Urgency is changed by a third-party system, and this change is being processed via a Transform Map, what is the best way to handle this requirement while avoiding duplicate or unnecessary work notes?

UI policies nor client scripts apply to transform maps as those things only apply to interactive user sessions via the browser