Incident management - Obtaining the date and time when additional comments were registered

Shibayama
Tera Contributor

This is about incident management.
After setting the assignment destination, I would like to obtain the date and time when the first additional comment was registered.

ServiceNow support gave me the following advice:
A script is required for the contents of the business rules. However, I have no experience with JavaScript. Can you give me some advice on the content of the script?

--Advice from support--
I think the concept should flow as follows.
1. Create a new date time field called "Temporary response date and time" in the incident
2. Create a business rule for the table as additional comments will be saved in sys_journal_field

The contents of the business rule are as follows:
1. Executed at insert time
2. In the case of an incident and a comment, check if there is another comment with the same Element ID in sys_journal_field, and if there is not, set a "temporary response date and time" for the incident.

6 REPLIES 6

Anand Kumar P
Giga Patron
Giga Patron

Hi @Shibayama ,
Write before insert business rule on incident table with below script.

AnandKumarP_0-1698322745376.png

 

AnandKumarP_1-1698322784165.png

 

AnandKumarP_2-1698322800924.png

 

 

(function executeRule(current, previous /*null when async*/ ) {
    if (current.comments && current.u_date_and_time_comments == '') {     
        var gr = new GlideRecord('sys_journal_field');
        gr.addQuery('element_id', current.sys_id); 
        gr.query();
        if (gr.next()) {  
            current.u_date_and_time_comments = new GlideDateTime();
        }
    }
})(current, previous);

 

Please mark it as helpful and solution proposed if its serve you purpose.

Thanks,

Anand

@Anand Kumar Pat 
Thank you for answering.
It's been a great help.

In the script you gave me, the time when the second additional comment was registered is set to u_date_and_time_comments.


What I want to do is as follows.


Precondition: “Assigned to” value is set


Set the date and time when the first additional comment was registered after the value was set in "Assigned to" to u_date_and_time_comments.


I would appreciate your additional support.

Hi @Shibayama,

Try below script 

(function executeRule(current, previous /*null when async*/) {
if (current.comments && current.u_date_and_time_comments == '' && current.assigned_to.changes()) {
var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element_id', current.sys_id);
gr.query();
while (gr.next()) {
current.u_date_and_time_comments = new GlideDateTime();
break;
}
}
})(current, previous);

Please mark it as solution proposed if its serves your purpose.

Thanks,

Anand

Hi @Anand Kumar P 
I tried it, but the date was not set.