The CreatorCon Call for Content is officially open! Get started here.

Show an error message when exist a "Conflict" in Change Request.

crhistopherjuar
Kilo Expert

Hi Experts,

I have a requirement to show an error message when a ITIL user try to schedule a Change request, when exist a specific "Conflict" in Change Request.

I tried to resolve this with an onBefore Business Rule when the confict status is "Conflict" and the user have the ITIL role.

Here the Business Rule:

Condition:   g_user.hasRole('itil')

var change = new GlideRecord('change_request');

  change.addQuery('sys_id', current.change);

  change.query();

var conflict = new GlideRecord('conflict');      

    conflict.addQuery('change',current.change);      

    conflict.query();      

while (g.next()){

if (conflict.schedule == 'Fiscal Freeze'){

  current.setAbortAction(true);  

  gs.addInfoMessage('No change allowed in fiscal freeze. Please alter the schedule.');

}

}

My Business Rule doesn't work and I don't know what I am missing.

I hope that you can help me.

Regards

1 ACCEPTED SOLUTION

The key to business rules is understanding that they are triggered by database operations. If nothing has updated that conflict record, then running the business rule there isn't going to do any good.



Here's a thought... rather than an if, incorporate that in to the query. Your business rule should actually look like this (inside a function)



Name: Check for conflict


When: Before


Insert: true


Update: true


Condition: Conflict status | is | Conflict




(function executeRule(current, previous /*null when async*/) {



        var conflict = new GlideRecord('conflict');


        conflict.addQuery('change', current.sys_id);


        conflict.addQuery('schedule.name', 'Fiscal Freeze 2017');


        conflict.query();



        if (conflict.next()) {


                  gs.addErrorMessage('No change allowed in fiscal freeze. Please alter the schedule');


                  current.setAbortAction(true);


        }



})(current, previous);


View solution in original post

23 REPLIES 23

The business rule has a condition to only run when the user has itil role in the condition field above the script.



gs.hasRole('itil')



If you think it is running at the wrong time (or not running at the right time), go to System Diagnostics> Debug Business Rules, click it, and test again. The transcript of which BRs are run and skipped is on the form at the bottom.


Hi Chuck.  I was looking for a way to prevent creation/submission of a new change request if a conflict (blackout schedule) was detected and I came across this thread.

I set up the new BR as you have described, but it doesn't work the way I was expecting.  

When a change request is new (with a conflict) and is submitted, I can see the business rule executes, but it doesn't stop the submission/creation of the change request.   

If, after creation, i open the same change request and make a simple change then click Update, the BR executes again but this time I see expected behavior - i get error message at top and I stay on the change request form.

It seems like the BR works when changes are made to existing change requests, but not on the submission of a new change request.

Please see attached doc for screenshots of BR and my testing.

Thanks for any help you can provide.

@jammer   did you get the solution for your requirement?

I'm also having the same requirement and facing same issue.

If you got the solution, Could you please provide that to me?

Thanks!

@Chuck Tomasi 

Aishwarya Shelake

We ended up with an onSubmit client script that calls a script include to evaluate if dates are within a blackout period.

 

Here is the onSubmit code...

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var dateStart = g_form.getValue('start_date');
    var dateEnd = g_form.getValue('end_date');
    var changeType = g_form.getValue('u_impact_level');
    var returnValue = true;
    var confirmation, blackoutScheduleURL, urlString, instanceName;
    var errorString = 'The planned start or end date is during a designated risk advisory period at the beginning or end of an academic semester.';
   
    //link to kb article that contains blackout dates
    blackoutScheduleURL = 'nav_to.do?uri=%2Fkb_view.do%3Fsysparm_article%3DKB0011790';
   
    urlString = '&nbsp&nbsp <a class="web" target="_blank" href="' + blackoutScheduleURL + '">' + 'View designated risk advisory periods.' + '</a>';

    var ga = new GlideAjax('KMCChangeRequestinBlackoutPeriod');
    ga.addParam('sysparm_name', 'isChangeInBlackoutSchedule');
    ga.addParam('sysparm_start_date', dateStart);
    ga.addParam('sysparm_end_date', dateEnd);
    ga.getXMLWait();

    var answer = ga.getAnswer();

    if (answer == 'true') {
        if (changeType == '3') {
            confirmation = confirm('The Emergency Change Request being submitted has a planned start or end date within a blackout period.' + '\n' + '\n' + 'Click Cancel to remain on the form to change your dates or OK to submit your change.');

            if (confirmation == false) {
                g_form.addErrorMessage(errorString + urlString);
                return false;
            } else {
                return true;
            }
        } else {
            confirmation = confirm('You are submitting a change with a planned start date or end date during a designated risk advisory period at the beginning or end of an academic semester. If this proposed change has the potential to impact students, we strongly recommend that you reschedule this change outside of the risk advisory period, if possible.' + '\n' + '\n' + 'Click Cancel to remain on the form to change your dates or OK to submit your change.');

            if (confirmation == false) {
                g_form.addErrorMessage(errorString + urlString);
                return false;
            } else {
                return true;
            }

        }

    }

    return returnValue;
}
 
 
 
//this is the code from the script include to check the dates..
isChangeInBlackoutSchedule: function() {
        var isInSchedule = false;
       
        var changeStartDate = this.getParameter('sysparm_start_date');
        var changeEndDate = this.getParameter('sysparm_end_date');
        var changeStartDateGDT = new GlideDateTime(changeStartDate);
        var changeEndDateGDT = new GlideDateTime(changeEndDate);
       
        var grSchedules = new GlideRecord("cmn_schedule_blackout");
        grSchedules.get("sys_id", "576ab52d1b24f410495efdd51a4bcb0f");
       
        var sched = new GlideSchedule(grSchedules.sys_id);
        var scheduleEntry = new GlideRecord('cmn_schedule_span');
        scheduleEntry.addQuery('schedule', '576ab52d1b24f410495efdd51a4bcb0f');
        scheduleEntry.query();

        var scheduleEntryStartDate, scheduleEntryEndDate;
       
        while (scheduleEntry.next()) {
       
       
            scheduleEntryStartDate = scheduleEntry.start_date_time.getDisplayValue();
            scheduleEntryEndDate = scheduleEntry.end_date_time.getDisplayValue();
           
            if (sched.isInSchedule(changeStartDateGDT) || sched.isInSchedule(changeEndDateGDT) || (changeStartDate <= scheduleEntryStartDate && changeEndDate >= scheduleEntryEndDate) ) {
         
                isInSchedule = true;
            }
           
        }
       
        return isInSchedule;
    },