Script to Check related records in the tab "Incidents Caused By Change" on change Form

Pnik
Tera Contributor

Dear Experts,

can someone help me with below situation/script for the same.

we have one choice field on change form with Yes/No options , we are looking this to set to "Yes" if someone relates record i.e incident(one or more) in "Incidents Caused By Change" tab on change form and sets to "No" if there is no record or someone removes the records from this tab.

Thanks 

1 ACCEPTED SOLUTION

Adi26
Tera Expert

you can achieve above functionality by writing after business rule on "Incident" table.

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

// Add your code here
if (current.caused_by) {
var gr_chg = new GlideRecord('change_request');
gr_chg.addQuery('sys_id', current.caused_by);
gr_chg.query();

while (gr_chg.next()) {
// add code here to process the incident record
gr_chg.u_boolean_1 = true;
gr_chg.update();
}
}
if (previous.caused_by) {
var gr_inc = new GlideRecord('incident');
gr_inc.addQuery('caused_by', previous.caused_by);
gr_inc.query();


var gr_chg1 = new GlideRecord('change_request');
gr_chg1.addQuery('sys_id', previous.caused_by);
gr_chg1.query();

if (gr_inc.getRowCount() > 0) {
// add code here to process the incident record
gr_chg1.u_boolean_1 = true;
gr_chg1.update();
} else {
while (gr_chg1.next()) {
gr_chg1.u_boolean_1 = false;
gr_chg1.update();
}
}

}

 

 


})(current, previous);

 

 

find_real_file.png

View solution in original post

4 REPLIES 4

Adi26
Tera Expert

you can achieve above functionality by writing business rule on "Incident" table. 

Pnik
Tera Contributor

Hi Adi,

Thanks for the response.

but i dont see any field with name "on_change" on incident form.

Do you mean "caused_by"(caused by change)..?

 
Thanks

Adi26
Tera Expert

you can achieve above functionality by writing after business rule on "Incident" table.

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

// Add your code here
if (current.caused_by) {
var gr_chg = new GlideRecord('change_request');
gr_chg.addQuery('sys_id', current.caused_by);
gr_chg.query();

while (gr_chg.next()) {
// add code here to process the incident record
gr_chg.u_boolean_1 = true;
gr_chg.update();
}
}
if (previous.caused_by) {
var gr_inc = new GlideRecord('incident');
gr_inc.addQuery('caused_by', previous.caused_by);
gr_inc.query();


var gr_chg1 = new GlideRecord('change_request');
gr_chg1.addQuery('sys_id', previous.caused_by);
gr_chg1.query();

if (gr_inc.getRowCount() > 0) {
// add code here to process the incident record
gr_chg1.u_boolean_1 = true;
gr_chg1.update();
} else {
while (gr_chg1.next()) {
gr_chg1.u_boolean_1 = false;
gr_chg1.update();
}
}

}

 

 


})(current, previous);

 

 

find_real_file.png

Pnik
Tera Contributor

Hi Adi,

It Works , Thank you very much !