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

Ankur Bawiskar
Tera Patron
Tera Patron

@Vinod S Patil 

in email script you need to use RegEx to extract incident number

something like this you can use and enhance for your requirement

var str = 'INC# INC0528025 / ATT_NI_RMM_VC_ABCDE  / 1 - Critical / New / GENERIC DEVICE / test';

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

var match = regex.match(str);

gs.info('Incident number->' + match);

AnkurBawiskar_0-1751890574108.png

 

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

@Ankur Bawiskar 

Below code not working the email script, can you suggest

var str = ' ';

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

var match = regex.match(str);

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

@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

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