Extract user details from case comment

SiluniP
Tera Contributor
I have created a trigger to send email notification.
I want extract user who added the  service now case comment and get their email ID
 
In the following I have accessed the each comment added in the Service now case
 
var response_comment = fd_data.trigger.current.comments_and_work_notes.getJournalEntry(-1);
var na = response_comment.split("\n\n");
 
for (var i = 0; i < na.length; i++) {
 
     if( na.length == 1 )
    {

 

     gs.print(na[i]);
     return response_comment;
    }
}
 
1 REPLY 1

Pushkar-Snow
Mega Guru

Hi @SiluniP 

 

I can suggest 2 ways to get the user details from comment. Once you received comments added to the record from the above script.

 

First Example:

You can extract the user name from the comment. Then use the name to query the sys_user table to get the email of the user who added that comment.

 

Example of the comment retrieved from the incident record:

"Script: 2025-05-01 07:20:30 - Abel Tuter (Architect) (Work notes) This is test comment"

You can refer below code to get the user name from the comment:

var retrivedComment = "Script: 2025-05-01 07:20:30 - Abel Tuter (Architect) (Work notes) This is test comment";
        var inputString = retrivedComment;
        // Split the string by the hyphen
        var parts = inputString.split('-');
        // Check if there are at least three parts
        if (parts.length >= 4) {
            // Get the text after the third hyphen
            var textAfterThirdHyphen = parts[3].trim();
            // Find the index of "(Work notes)"
            var workNotesIndex = textAfterThirdHyphen.indexOf("(Work notes)");
            // Extract the text before "(Work notes)"
            var extractedText = workNotesIndex !== -1 ? textAfterThirdHyphen.substring(0, workNotesIndex).trim() : textAfterThirdHyphen.trim();
            gs.info(extractedText); // Output: Abel Tuter (Architect)
        } else {
            gs.info("Text not found");
        }
    

 

Once the output received query user table with output received (User Name) from the above script to get user email.

 

Second Example:

You can extract the user name and comment from the record. Then use both name and comment to query "sys_journal_field" table where all the comment will be stored. after query when record found use createdby field to get the email id.

 

I will recommend the First example. Because in Second Example the table use to make query will have so many records which may impact the instance performance.

 

If I am able to help you with your question, Please click the Thumb Icon and mark as Correct. 

 

Regards,

Pushkar