Copy journalentry without date and updated by

Hafsa1
Mega Sage

Hi

I'm copying  all journal entry from one table to another using below code. But I don't want datestamp and updated by. Just want the comments to be updated. How to remove those?

var notes = gr.u_work_notes.getJournalEntry(-1);

current.comments = notes;

current.update();

Hafsa1_0-1692590183807.png

 

4 REPLIES 4

Abdul_Rahiman
Tera Guru

Hi Mega,

    var work_notes_get_all = current.work_notes.getJournalEntry(-1);
    var na = work_notes_get_all.split(/\n\n(?=[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} - )/);
    for (var i = 0; i < na.length; i++)
    gs.addInfoMessage(na[i].match(/\n.*/gm).join('').replace(/^\s*\n/gm, ""));
 
 
 
use the above script as its working for me.
 
Thanks and Reagrds,
Abdul

I updated code as below, but it's just removing first line and not all.

var gr = new GlideRecord('kb_knowledge');
gr.addQuery('sys_id', current.document_id);
gr.query();
if (gr.next()) {
var work_notes_get_all = gr.u_work_notes.getJournalEntry(-1);
var na = work_notes_get_all.split(/\n\n(?=[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} - )/);
for (var i = 0; i < na.length; i++)
//gs.addInfoMessage(na[i].match(/\n.*/gm).join('').replace(/^\s*\n/gm, ""));
current.comments = na[i].match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
current.update();
}

Hi Mega,

 

This is Working from below is the screen shot and script

 

Screen shot  taken from Incident record

Fix.JPG

 

var gr = new GlideRecord('problem');
    gr.addQuery('sys_id', "c92c2fd3b7b023002bc49a91ee11a9ce");
    gr.query();
    if (gr.next()) {
        var work_notes_get_all = gr.work_notes.getJournalEntry(-1);
        var na = work_notes_get_all.split(/\n\n(?=[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} - )/);
        for (var i = 0; i < na.length; i++)
            //gs.addInfoMessage(na[i].match(/\n.*/gm).join('').replace(/^\s*\n/gm, ""));
            current.comments = na[i].match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
        current.update();
    }

SwarnadeepNandy
Mega Sage

Hello @Hafsa1,

 

To remove the datestamp and updated by from the journal entry, you can use a regular expression to match and replace the unwanted parts of the string.

A regular expression is a sequence of characters that defines a search pattern for text. You can use regular expressions in ServiceNow to manipulate strings using the String class methods.

 

//Get the last journal entry from the source table 
var notes = gr.u_work_notes.getJournalEntry(-1);
//Create a regular expression to match the datestamp and updated by 
var regex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} - .*\n/gm;
//Replace the matched parts with an empty string 
var comments = notes.replace(regex, “”);
//Set the comments field in the target table to the modified string 
current.comments = comments;
å //Update the target record 
current.update();

 

This code will remove any lines that start with a datestamp in the format YYYY-MM-DD HH:MM:SS followed by a dash and any characters, and end with a newline character.

For example, it will remove lines like this:

2023-08-21 11:07:33 - John Smith

You can modify this code according to your needs and preferences.

 

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy