Business Rule Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2024 09:34 AM
Hi All,
On the change request form, there is a custom field called Maintenance Window. And through below script we are populating schedule entries in the same field and also the error message/the banner. And this is list type field.
And there is another field as business service, and we are checking T1 priority business services.
Now we should also add a validation that the business Service should not trigger its own defined window alert.
For example, below is a scenario.
user selected Business service contains 'Loyalty' on the change form.
Scheduled entries in the maintenance window field should not populate 'Loyalty' named choice.
Below are the business services and scheduled entries which we have. How to add this validation in this script? Please assist.
Business Services:
B2B
B2C
RES ESB
IRIS
Loyalty
Availability ESB
Concerto
Network
(function executeRule(current, previous /*null when async*/ ) {
var startdate = current.start_date;
var crend = current.getDisplayValue('end_date');
var crstart = current.getDisplayValue('start_date');
var gdt = new GlideDateTime(startdate);
var dow = gdt.getDayOfWeek();
var endate = current.end_date;
var crstarttime = crstart.split(" ");
var onlycrstarttime = crstarttime[1];
var endgdt = new GlideDateTime(endate);
var crendtime = crend.split(" ");
var onlycrendtime = crendtime[1];
var arr = [];
var uniqueSchedules = {}; // Object to track unique schedule names
current.setValue('u_maintainence_window', '');
if (current.u_business_service.u_priority == 'T1') {
var gr = new GlideRecord("cmn_schedule_span");
gr.addEncodedQuery('schedule.nameINAvailability ESB Maintenance Window,B2B Maintenance Window,B2C Web Maintenance Window,Concerto Maintenance Window,Network/Infrastructure Maintenance Window,Network Maintenance Window,Network Firewall Maintenance,Loyalty Maintenance Window - PM,Loyalty Maintenance Window - AM,IRIS Weekly Maintenance Window,RES ESB Maintenance Window');
gr.addEncodedQuery('days_of_weekCONTAINS' + dow);
gr.query();
while (gr.next()) {
var startmaint = gr.getDisplayValue('start_date_time');
var startdatetime = new GlideDateTime(startmaint);
var endmaint = gr.getDisplayValue('end_date_time');
var startonlymaint = startmaint.split(" ");
var endtimemaint = endmaint.split(" ");
var starttimeonlymaint = startonlymaint[1];
var endtimeonlymaint = endtimemaint[1];
var rec = [];
rec.push(gr.schedule.u_contacts);
var a = rec.join(',');
if ((onlycrstarttime < starttimeonlymaint && starttimeonlymaint < onlycrendtime) ||
(onlycrstarttime < endtimeonlymaint && endtimeonlymaint < onlycrendtime) || (starttimeonlymaint < onlycrstarttime && onlycrstarttime < endtimeonlymaint) ||
(starttimeonlymaint < onlycrendtime && onlycrendtime < endtimeonlymaint)) {
var scheduleName = gr.schedule.name.toString(); // Ensure schedule name is treated as string
if (!uniqueSchedules.hasOwnProperty(scheduleName)) {
uniqueSchedules[scheduleName] = true; // Mark schedule name as seen
arr.push(scheduleName); // Push unique schedule name into the array
gs.log("RK | BR Inside if: " + scheduleName);
}
current.u_maintainence_window = arr.join(',');
var messageallnew = '<b><font size="3">"The defined Change Request window overlaps Maintenance Window,"</font></b>' +
'<b><font size="4">' + gr.schedule.name + '</font></b>' +
'<b><font size="3">"Coordinate with the Technical Manager"</font></b><br>' +
'<font size="3"><b>Primary Contact - ' + gr.schedule.u_pri_contact.getDisplayValue() + '</b></font><br>' +
'<font size="3"><b>Secondary Contact - ' + gr.schedule.u_ec_contact.getDisplayValue() + '</b></font>';
gs.addErrorMessage(messageallnew);
var link = '<a href="https://ihguat.service-now.com/now/nav/ui/classic/params/target/cmn_schedule_span_list.do%3Fsysparm_query%3Dschedule.nameLIKEMaintenance%2520Window%26sysparm_first_row%3D1%26sysparm_view%3D%26sysparm_choice_query_raw%3D%26sysparm_list_header_search%3Dtrue">Maintenance Window</a>';
gs.addInfoMessage(link);
gs.log("RK | Schedule Message: " + gr.schedule.name);
gs.eventQueue("noifyownerofchngemaint", current, gr.schedule.name, rec);
}
}
}
})(current, previous);
Best Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2024 11:46 AM - edited 09-27-2024 11:48 AM
Try this
var flag = true;
if (scheduleName.includes(current.u_business_service.toString()))
{
flag =false;
}
if ( flag==true)
{
if (!uniqueSchedules.hasOwnProperty(scheduleName)) {
uniqueSchedules[scheduleName] = true; // Mark schedule name as seen
arr.push(scheduleName); // Push unique schedule name into the array
gs.log("RK | BR Inside if: " + scheduleName);
}
current.u_maintainence_window = arr.join(',');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2024 03:25 AM - edited 09-28-2024 06:49 AM
Hi @Mani A ,
Thank you for your response. But still, it is not working. If you see below, the maintenance window field should ignore loyalty choices as loyalty connect is selected as business service. In this example, it should only populate B2C web choice here.
Please assist how to proceed.
Best Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2024 04:43 AM - edited 09-28-2024 04:54 AM
Can you share where did u update suggested logic in BR and why can't you go for reference qualifier script for maintainance window field if we change business service??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2024 06:37 AM - edited 09-28-2024 06:37 AM
Hi @Mani A ,
I have updated the script as below. Please correct me if it is wrong. Could you also suggest reference qualifier script how to filter when we change business service.
(function executeRule(current, previous /*null when async*/ ) {
var startdate = current.start_date;
var crend = current.getDisplayValue('end_date');
var crstart = current.getDisplayValue('start_date');
var gdt = new GlideDateTime(startdate);
var dow = gdt.getDayOfWeek();
var endate = current.end_date;
var crstarttime = crstart.split(" ");
var onlycrstarttime = crstarttime[1];
var endgdt = new GlideDateTime(endate);
var crendtime = crend.split(" ");
var onlycrendtime = crendtime[1];
var arr = [];
var uniqueSchedules = {}; // Object to track unique schedule names
current.setValue('u_maintainence_window', '');
if (current.u_business_service.u_priority == 'T1') {
var gr = new GlideRecord("cmn_schedule_span");
gr.addEncodedQuery('schedule.nameINAvailability ESB Maintenance Window,B2B Maintenance Window,B2C Web Maintenance Window,Concerto Maintenance Window,Network/Infrastructure Maintenance Window,Network Maintenance Window,Network Firewall Maintenance,Loyalty Maintenance Window - PM,Loyalty Maintenance Window - AM,IRIS Weekly Maintenance Window,RES ESB Maintenance Window');
gr.addEncodedQuery('days_of_weekCONTAINS' + dow);
gr.query();
while (gr.next()) {
var startmaint = gr.getDisplayValue('start_date_time');
var startdatetime = new GlideDateTime(startmaint);
var endmaint = gr.getDisplayValue('end_date_time');
var startonlymaint = startmaint.split(" ");
var endtimemaint = endmaint.split(" ");
var starttimeonlymaint = startonlymaint[1];
var endtimeonlymaint = endtimemaint[1];
var rec = [];
rec.push(gr.schedule.u_contacts);
var a = rec.join(',');
if ((onlycrstarttime < starttimeonlymaint && starttimeonlymaint < onlycrendtime) ||
(onlycrstarttime < endtimeonlymaint && endtimeonlymaint < onlycrendtime) || (starttimeonlymaint < onlycrstarttime && onlycrstarttime < endtimeonlymaint) ||
(starttimeonlymaint < onlycrendtime && onlycrendtime < endtimeonlymaint)) {
var scheduleName = gr.schedule.name.toString(); // Ensure schedule name is treated as string
var flag = true;
if (scheduleName.includes(current.u_business_service.toString())) {
flag = false;
}
if (flag == true) {
if (!uniqueSchedules.hasOwnProperty(scheduleName)) {
uniqueSchedules[scheduleName] = true; // Mark schedule name as seen
arr.push(scheduleName); // Push unique schedule name into the array
gs.log("RK | BR Inside if: " + scheduleName);
}
current.u_maintainence_window = arr.join(',');
}
var messageallnew = '<b><font size="3">"The defined Change Request window overlaps Maintenance Window,"</font></b>' +
'<b><font size="4">' + gr.schedule.name + '</font></b>' +
'<b><font size="3">"Coordinate with the Technical Manager"</font></b><br>' +
'<font size="3"><b>Primary Contact - ' + gr.schedule.u_pri_contact.getDisplayValue() + '</b></font><br>' +
'<font size="3"><b>Secondary Contact - ' + gr.schedule.u_ec_contact.getDisplayValue() + '</b></font>';
gs.addErrorMessage(messageallnew);
var link = '<a href="https://ihguat.service-now.com/now/nav/ui/classic/params/target/cmn_schedule_span_list.do%3Fsysparm_query%3Dschedule.nameLIKEMaintenance%2520Window%26sysparm_first_row%3D1%26sysparm_view%3D%26sysparm_choice_query_raw%3D%26sysparm_list_header_search%3Dtrue">Maintenance Window</a>';
gs.addInfoMessage(link);
gs.log("RK | Schedule Message: " + gr.schedule.name);
gs.eventQueue("noifyownerofchngemaint", current, gr.schedule.name, rec);
}
}
}
})(current, previous);
Thanks for all your help.