Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule condition not working

leachy23
Kilo Expert

Happy Monday all, 

Im trying to get a business rule to trigger when a 'Resolve' UI Action is clicked on a 'parent' ticket within a scoped app, but for some reason it never triggers.

my condition is current.state.changesTo(6)

Its an after business rule (I have tried before) . 

the BR itself is 

function closeRelatedTasks(me) {
var task = new GlideRecord("x_insoa_investec_a_table_caa");
task.addQuery("parent", '=', me.sys_id);
task.query();
while (task.next()) {


gs.print("Task " + task.number + ' closed based on closure of task ' + me.number);
task.state.setValue(6);
task.close_notes.setValue("Resolved by closure of Parent");
task.update();
}


this.closeIncident(me);
}

All help is appreciated!

1 ACCEPTED SOLUTION

leachy23
Kilo Expert

It was a mistake in the the code, I replaced 

gs.info("case " + lend.number + ' closed based on closure of case ' + me.number);

with 

gs.info("case " + lend.number + ' closed based on closure of case ' + current.number);

 

and it all worked fine! Thanks for the replies all

View solution in original post

11 REPLIES 11

You described that the UI Action, changes the state of a parent ticket. Is that correct? The Business Rule is running on the parent ticket I assume?

Can you share the full code of the Business Rule? You only pasted a snipped I guess? Or if not: how is the function being called?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark, 

Yes a UI Action with both client and server code and the BR runs on the parent. 

Full code for both below.

UI Action 

//Client-side 'onclick' function
function moveToResolved(){
g_form.setValue ('state', 6);



//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'resolve_iba_case'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined')
runBusRuleCode();

//Server-side function

function runBusRuleCode(){
current.state = 6;
current.update();
// gs.addInfoMessage('You did it!');
action.setRedirectURL();
}

 

Business Rule

updateChildCases();
function updateChildCases() {
if (current.state.changesTo(6))
resolveRelatedCases();
else{
updateChildren('work_notes', gs.getMessage('Work note copied from Parent Case'));
}
}

function updateChildren (fieldName, msg){
var rec = newGlideRecord("x_insoa_investec_a_table_caa");
rec.addQuery("parent", current.sys_id);
rec.addQuery("state", "!=", 6);
rec.addActiveQuery();
rec.query();
while (rec.next()){
var fieldRawValue = current.getValue(fieldname) + '';
var fieldValue = fieldRawValue.trim();
if (!fieldValue || filedValue.length <= 0)
return;
if (fieldRawValue.indexOf(msg) ==0)
rec[fieldName] = msg + ": " + fieldRawValue;
rec.update();

}
}


function resolveRelatedCases() {
var lend = new GlideRecord("x_insoa_investec_a_table_caa");
lend.addActiveQuery();
lend.addQuery("parent", current.sys_id);
lend.addQuery("state", "!=", 6);
lend.query();
var msg = "";
while (lend.next()) {
gs.info("case " + lend.number + ' closed based on closure of case ' + me.number);
lend.state = 6;
if (lend.close_notes.nil()){
msg = gs.getMessage('{0} copied from Parent Case', current.close_notes.getLabel());
if (current.close_notes.toString().indexOf(msg) == 0)
lend.close_notes = current.close_notes;
else
lend.close_notes = msg + ": " + current.close_notes;
}
msg = gs.getMessage("Resolved based on resolution of Parent Case");
lend.update();







}
}
gs.addInfoMessage('Your condition worked');

 

 

The debugger is returning the child record but its not resolving?

so you code is having problem in this section

function resolveRelatedCases() {
var lend = new GlideRecord("x_insoa_investec_a_table_caa");
lend.addActiveQuery();
lend.addQuery("parent", current.sys_id);
lend.addQuery("state", "!=", 6);  //--> problem here
lend.query();

 

replace it as

end.addQuery("state!=6");

again

lend.state = 6; needs to be replaced as lend.state = "6";

If I look at an out-of-the-box example, it uses:

current.state.changesTo(IncidentState.RESOLVED)

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn