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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Crhistopher,



Two erros I can see in your script



Replace while (g.next()){ with while (conflict.next()){


Also conflict.addQuery('change',current.change); I don't see any field on change_request table with field column "change". Can you please verify the same once.




fredi_fr
ServiceNow Employee
ServiceNow Employee

Hi,


I see a couple of issues in your BR.



1) The while loop condition reads "g.next()", but "g" is not defined, should be "conflict.next()".


2) The condition inside the loop is wrong, you're comparing a sys_id with a string. You need to dot-walk to the schedule's name like this:


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



Hope this solves you issue


Regards,


Guillermo


Chuck Tomasi
Tera Patron

The schedule field on the conflict table is a reference field which means it stores a sys_id. You can either replace "Fiscal Freeze" with the sys_id of the record it relates to in the schedule table, or you can use the getDisplayValue() to get the (human readable) name that appears in the field.



Ex:



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


        // Abort and error here


}


Also change g_user.hasRole()



to gs.hasRole() in your condition statement.



g_user is a client side object.