Error Message query

Joshuu
Kilo Sage

Hi All,

 

We are populating an error message on the change form through a business rule. And if we open the same change form from report or if we reload the form the error message is not getting displayed.

 

How to display the error message to as soon as the form loads?

 

Please assist.

 

 

12 REPLIES 12

If you want a message to display when the form loads, you need a display Business Rule, without the Filter Conditions as the fields won't change as the record is loaded.  The code you posted, if that's the entire script, won't work as 'gr' is undefined.  Get a basic message working before trying the concatenated, HTML formatted, pulling in fields from other tables.

Hi @Brad Bowman  ,

 

This is the complete code. Whenever the planned start date and end date is changes, this should run.

 

(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', '');

    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://instance_name.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);

Hi @Brad Bowman  ,

 

Yes, it is working with display business rule without any condition. So, can we include planned start date and planned end date changes in the script itself?

 

Please assist.

If you only want it to display when the dates change, not based on whatever the dates are when the form loads, then you need an onChange Client Script instead.  You can have both if you want to check the dates when the form loads, then display the message if the dates change and meet the criteria.

The onChange Client Script will need a GlideAJax call to a Script Include, passing in the two dates, then returning the message if it is in conflict.  Once you get it working you'll copy the script to have it trigger onChange of both fields.  If you want both scenarios - on load and change, you can call the same Script Include from the Business Rule so that the code/logic only exists one place, or ditch the Business Rule and use an onLoad Client Script instead that will be very similar to the onChange.

Hi @Brad Bowman  ,

 

Yes, we need both onload and on change. Could you please help me with the script.