Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help with mail script

Gemma Shepherd
Tera Contributor

Hi,

 

I have the below script, which currently returns correctly:

08/07/2024 14:15:45 BST - [User Name] (Actions taken) WIP

But I would just like it to display the date and what the actions taken, rather than with the name in as well, is there a way the code can be amended just to show it?

 

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {
      var activities = current.incident_alert.source_incident.actions_taken.getJournalEntry(-1);
    var activityArray = activities.split("\n\n");
    template.print("<br/>");
    for (var i = 0; i < activityArray.length; i++)
        template.print(activityArray[i] + "<br/>"+"<br/>");
       
})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

Dnyaneshwaree
Mega Sage

Hello @Gemma Shepherd ,

Please refer below code for a example and update it as per your req:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {
    
    // Get the most recent journal entry
    var activities = current.incident_alert.source_incident.actions_taken.getJournalEntry(-1);
    var activityArray = activities.split("\n\n");

    template.print("<br/>");

    for (var i = 0; i < activityArray.length; i++) {
        var entry = activityArray[i];
        // Assuming the format is: "date - [User Name] (Actions taken)"
        var parts = entry.split(" - ");
        if (parts.length > 1) {
            var date = parts[0];
            var actions = parts[1].split(") ")[1]; // Split and take the second part after ") "
            template.print(date + " - " + actions + "<br/><br/>");
        }
    }

})(current, template, email, email_action, event);

 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Hi @Gemma Shepherd ,

Please refer below script 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {
      var activities = current.incident_alert.source_incident.actions_taken.getJournalEntry(-1);
    var activityArray = activities.split("\n\n");
    template.print("<br/>");
    for (var i = 0; i < activityArray.length; i++)
        template.print(activityArray[i] + "-"+ current.assigned_to + current.action_taken);
       
})(current, template, email, email_action, event);

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Anirudh Pathak
Mega Sage

Hi @Gemma Shepherd ,

Please try below code, assuming that "(Actions taken)" is a string present in the "activities" variable which is defined in the email script - 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {
      var activities = current.incident_alert.source_incident.actions_taken.getJournalEntry(-1);
      var activityArray = activities.split(' ');
      var index;
      template.print("<br/>");
      for (var i = 0; i < activityArray.length; i++) {
          var str = activityArray[i].toString();
		if(str.contains("taken")) { 
			index = i;
			break;
		}
   }
   var finalString = activityArray[0] + " ";    // arr[0] will contain the Date
	for(var j = index + 1; j < activityArray.length; j++) {
		finalString += arr[j] + " ";
	}
  template.print(finalString); 
       
})(current, template, email, email_action, event);

 

James Chun
Kilo Patron

Hi @Gemma Shepherd,

 

You can use Regex to extract the data you need from the string or query the [sys_journal_field] table (https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0529930).

 

One caveat with Regex is that you will need to consider a lot of cases as the journal entry is a free text.

It may be difficult or impossible to come with a single Regex that can catch everything.

 

When you query the [sys_journal_field] directly, you don't have to worry about the above but it does mean that you are querying data from a large database which is considered as a bad practice.

 

Refer to another old post as well - https://www.servicenow.com/community/developer-forum/formatting-journal-entries-on-mail-scripts/m-p/...

 

Cheers

 

Dnyaneshwaree
Mega Sage

Hello @Gemma Shepherd ,

Please refer below code for a example and update it as per your req:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {
    
    // Get the most recent journal entry
    var activities = current.incident_alert.source_incident.actions_taken.getJournalEntry(-1);
    var activityArray = activities.split("\n\n");

    template.print("<br/>");

    for (var i = 0; i < activityArray.length; i++) {
        var entry = activityArray[i];
        // Assuming the format is: "date - [User Name] (Actions taken)"
        var parts = entry.split(" - ");
        if (parts.length > 1) {
            var date = parts[0];
            var actions = parts[1].split(") ")[1]; // Split and take the second part after ") "
            template.print(date + " - " + actions + "<br/><br/>");
        }
    }

})(current, template, email, email_action, event);

 

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru