Help with script to display broadcast message based on role

Nya Valdez1
Tera Contributor

I have a requirement to create a popup message to logged in users of a group when a communication approval is needed on an incident - rather than an email approval request.  To achieve this I created a new role (approval_message) and assigned it to the group and then wrote a workflow script that adds a record to the sys_broadcast_message table with each of the fields set as well as the length of time that the message should run. Next, I added to the flow for this incident type. It works perfectly in terms of the display message, except that I noticed:

1. On the popup message the incident number does not show.  It shows "undefined" instead but when you click on the link it takes you to the correct incident number that needs approval. 

find_real_file.png

2. The message should only pop up for 15 mins. but when I check the sys_broadcast_table entry, the timing is off by hours and the *Notify users until date is set for many many hours later. Can someone please tell me what I'm doing wrong in my script?

find_real_file.png

Script:

var gr = new GlideRecord('sys_broadcast_message');
var date = gs.nowDateTime();
var end = gs.minutesAgo(15);
var incident = current.incident.number;
var sysID = current.sys_id;
var url = 'https://onedev.service-now.com/u_operational_mechanical_incident.do?sys_id='+ sysID;
var urlString = '<a class="web" target="_blank" href = "'+ url + '">' +incident + '</a>';
gr.initialize();
gr.message = 'Incident ' + urlString + ' has been submitted for a communication approval. Please refer to the My Approvals link to review the details of this incident and either approve or reject the approval';
gr.user_filter = 'approval_message';
gr.at_login= 'false';
gr.logged_in = 'true';
gr.email = 'false';
gr.notify_users_after_date = date;
gr.notify_users_until_date = end;
gr.insert();
1 ACCEPTED SOLUTION

var start = new GlideDateTime();
var end = new GlideDateTime();
end.addSeconds(900); //15 minutes is 900 secs.

 

//These are true/false fields. please adjust as below.

gr.at_login= false;
gr.logged_in = true;
gr.email = false;

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

7 REPLIES 7

palanikumar
Giga Sage

Where are you running the above script? I can see you using current.incident.number. Is incident a valid field in the current record?

Thank you,
Palani

Hello Palanikumar!!

The script is running against a custom table that extends from the incident table, where there are two incident forms (Table Name: "u_operational_mechanical_incident) ... The number field is the OOTB number field on the incident forms.

My thought process was that by adding the full URL to direct to the incident number into the script, it would display the incident number - instead it displays "undefined" but when you click the undefined link it opens to the correct incident that needs approval.

Any other thoughts on this??

vkachineni
Kilo Sage
Kilo Sage

//Try. untested

var date = gs.nowDateTime();
var end = gs.minutesAgo(15);
var inc_link = createLinkForObject(current.getTableName(),current.sys_id, current.number);
function createLinkForObject(strTableName, strSysID, number){
   return '<a class="web" href="' + gs.getProperty('glide.servlet.uri') + gs.generateURL(strTableName, strSysID) + '" target="_blank">' + number + '</a>';
}
var gr = new GlideRecord('sys_broadcast_message');
gr.initialize();
gr.message = 'Incident ' + inc_link + ' has been submitted for a communication approval. Please refer to the My Approvals link to review the details of this incident and either approve or reject the approval';
gr.user_filter = 'approval_message';
gr.at_login= 'false';
gr.logged_in = 'true';
gr.email = 'false';
gr.notify_users_after_date = date;
gr.notify_users_until_date = end;
gr.insert();
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022