how to get the incident number from the work note

Vinod S Patil
Tera Contributor

Hello Everyone

I have a requirement to pick only incident number from the updated work note of the sctask using email script.

Please suggest me on this. for your reference attached screenshot 

VinodSPatil_0-1751890255202.png


@Ankur Bawiskar 

 

2 ACCEPTED SOLUTIONS

@Vinod S Patil 

you didn't pass any string so how it will get the INC number from it.

since you want it from latest work notes use this

var str = current.work_notes.getJournalEntry(1);

var regex = new SNC.Regex('/INC\\d{7}/im');

var match = regex.match(str);

template.print('Incident: ' + match);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Shubham_Jain
Mega Sage
Mega Sage

@Vinod S Patil See if this helps you 

 

This is working script which will help you to extract the incident number from the Work Notes. Kindly note that if you have customized the Incident Number formatting then regex needs to be altered accordingly. Below script will perfectly work for you in PDI as it is OOB. 

 

var incGR = new GlideRecord('incident');
if (incGR.get('INC0010018')) {
    var incidentSysId = incGR.getUniqueValue();
    var journalGR = new GlideRecord('sys_journal_field');
    journalGR.addQuery('element_id', incidentSysId);
    journalGR.addQuery('element', 'work_notes');
    journalGR.query();
    var regex = /INC\d{7}/g;
    var foundIncidents = [];
    while (journalGR.next()) {
        var note = journalGR.getValue('value');
        var matches = note.match(regex);
        if (matches) {
            for (var i = 0; i < matches.length; i++) {
                if (foundIncidents.indexOf(matches[i]) === -1) {
                    foundIncidents.push(matches[i]);
                }
            }
        }
    }
    if (foundIncidents.length > 0) {
        gs.print('Incident Numbers found in work notes of INC0010018:');
        for (var j = 0; j < foundIncidents.length; j++) {
            gs.print(foundIncidents[j]);
        }
    } else {
        gs.print('No Incident Numbers found in work notes.');
    }
} else {
    gs.print('Incident INC0010018 not found.');
}

 

Outcome of the script would be something like this: 

 

Shubham_Jain_0-1751892284995.png

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


View solution in original post

6 REPLIES 6

@Vinod S Patil Thank you for marking my response as Helpful! 🙂 

 

Kindly mark the response as correct if it solved your query as marking solution as correct will help others for their reference and visibility. 

 

Thank you! 

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


@Vinod S Patil 

I don't think querying sys_journal_field is required here as you can simply get the latest work notes using this syntax. It will lead to performance issue.

current.work_notes.getJournalEntry(1)

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader