Send all work notes history to External system via flow designer

Priyadharashin1
Tera Contributor

Hi all,

 

I need to send the work notes history in the below format to external system using flow designer. Also, while Inbound, I need to process the work notes history and store it in Incident table. For inbound I am using Transform Map. Kindly provide some suggestions. Thanks.

"workNotesHistory": [
{
"workNote": "XXX",
"date": "XXX",
"user": "XXX",
"order": "XXX"
},
{
"workNote": "XXX",
"date": "XXX",
"user": "XXX",
"order": "XXX"
}
]

 

1 ACCEPTED SOLUTION

Hayo Lubbers
Kilo Sage

Hi @Priyadharashin1,

 

Getting all the work_notes out, should be possible via the getJournalEntry function.

 

var grIncident = new GlideRecord('incident');
if (grIncident.get('407bfe6b97142110b4f672571153afc9')) {

    //gets all journal entries as a string where each entry is delimited by '\n\n'
    var notes = grIncident.work_notes.getJournalEntry(-1);
    //stores each entry into an array of strings
    var na = notes.split("\n\n");

    for (var i = 0; i < na.length; i++)
        gs.info(na[i]);
}

 

Would deliver (in a background script):

*** Script: 20-02-2023 19:34:56 - System Administrator (Work notes)
Work Note #3
*** Script: 20-02-2023 19:34:51 - System Administrator (Work notes) Work Note #2
*** Script: 20-02-2023 19:34:45 - System Administrator (Work notes) Work note #1
*** Script:

Based on : https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideElementAPI#r_Glid...

 

If you combine that and split it (lazy splitting, since it is an example, you might want to split it in a better way with a proper regex to prevent issues with different date/time formatting etc.), you get something like this:

 

var grIncident = new GlideRecord('incident');
if (grIncident.get('407bfe6b97142110b4f672571153afc9')) {

    //gets all journal entries as a string where each entry is delimited by '\n\n'
    var notes = grIncident.work_notes.getJournalEntry(-1);
    //stores each entry into an array of strings
    var na = notes.split("\n\n");

    var result = [];
    for (var i = na.length; i >= 0 ; i--) {
        var nb = na[i].split("\n");
        if (nb && nb[0]) {
            var yourDate = nb[0].substr(0, 10);
            var yourTime = nb[0].substr(11, 8);
            var yourDateTime = nb[0].substr(0, 19);
            var yourUser = nb[0].substr(22).split('(')[0];
            var yourWorknote = nb[1];
            var worknote = {
                "workNote": yourWorknote,
                "date": yourDateTime,
                "user": yourUser,
                "order": i
            }
            result.push(worknote);
        }
    }

    gs.info(JSON.stringify(result))
}

 

resulting in:

[{"workNote":"Work note #1","date":"20-02-2023 19:34:45","user":"System Administrator ","order":2},{"workNote":"Work Note #2","date":"20-02-2023 19:34:51","user":"System Administrator ","order":1},{"workNote":"Work Note #3","date":"20-02-2023 19:34:56","user":"System Administrator ","order":0}]

 

If the answer helped you in any way, please mark it helpful. If it your solution, please accept it.

View solution in original post

5 REPLIES 5

Hayo Lubbers
Kilo Sage

Hi @Priyadharashin1,

 

Getting all the work_notes out, should be possible via the getJournalEntry function.

 

var grIncident = new GlideRecord('incident');
if (grIncident.get('407bfe6b97142110b4f672571153afc9')) {

    //gets all journal entries as a string where each entry is delimited by '\n\n'
    var notes = grIncident.work_notes.getJournalEntry(-1);
    //stores each entry into an array of strings
    var na = notes.split("\n\n");

    for (var i = 0; i < na.length; i++)
        gs.info(na[i]);
}

 

Would deliver (in a background script):

*** Script: 20-02-2023 19:34:56 - System Administrator (Work notes)
Work Note #3
*** Script: 20-02-2023 19:34:51 - System Administrator (Work notes) Work Note #2
*** Script: 20-02-2023 19:34:45 - System Administrator (Work notes) Work note #1
*** Script:

Based on : https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideElementAPI#r_Glid...

 

If you combine that and split it (lazy splitting, since it is an example, you might want to split it in a better way with a proper regex to prevent issues with different date/time formatting etc.), you get something like this:

 

var grIncident = new GlideRecord('incident');
if (grIncident.get('407bfe6b97142110b4f672571153afc9')) {

    //gets all journal entries as a string where each entry is delimited by '\n\n'
    var notes = grIncident.work_notes.getJournalEntry(-1);
    //stores each entry into an array of strings
    var na = notes.split("\n\n");

    var result = [];
    for (var i = na.length; i >= 0 ; i--) {
        var nb = na[i].split("\n");
        if (nb && nb[0]) {
            var yourDate = nb[0].substr(0, 10);
            var yourTime = nb[0].substr(11, 8);
            var yourDateTime = nb[0].substr(0, 19);
            var yourUser = nb[0].substr(22).split('(')[0];
            var yourWorknote = nb[1];
            var worknote = {
                "workNote": yourWorknote,
                "date": yourDateTime,
                "user": yourUser,
                "order": i
            }
            result.push(worknote);
        }
    }

    gs.info(JSON.stringify(result))
}

 

resulting in:

[{"workNote":"Work note #1","date":"20-02-2023 19:34:45","user":"System Administrator ","order":2},{"workNote":"Work Note #2","date":"20-02-2023 19:34:51","user":"System Administrator ","order":1},{"workNote":"Work Note #3","date":"20-02-2023 19:34:56","user":"System Administrator ","order":0}]

 

If the answer helped you in any way, please mark it helpful. If it your solution, please accept it.

Hi Hayo,

 

Thanks for the response. I did few modifications, but this script worked..Thank you

This script helps for a single record help me to fetch the same for multiple records along with the incident number for each work notes with that specific incident number

You should already be preselecting your Incidents in the flow.  Then execute the script from within a For Each.  May need to create your own custom action.