Using a Business Rule to cascade a value up
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2012 04:14 PM
There is a currently a Business Rule to cascade field values down and its pretty easy to copy. Unfortunately I have a requirement to cascade a state up. I'm having issues with it just not wanting to work. It seemed like it would be pretty simple. I've tried two different ways and i just doesn't want to flip the state or the parent change request. At least I can't get it to work at all. Anyone have a clue where I'm going wrong?
Case 1:
//Creates a query and searches the Change Task table for any additional tasks associated with the change.
var ctask = new GlideRecord("change_task");
var closereq = 1;
ctask.addQuery("change_request", current.change_request);
ctask.query();
while (ctask.next())
{
//Checks each task to see if there is one that remains open. If it finds an open task it flips the closereq variable to 2 to prevent the next piece of code.
if (ctask.state =='-5' || ctask.state=='-4' || ctask.state=='1' || ctask.state=='2'){
closereq = 2;
}
}
//if the query found no remaining tasks open search for the parent request and close it.
if (closereq==1){
current.change_request.state = '3';
}
Case 2:
//Creates a query and searches the Change Task table for any additional tasks associated with the change.
var ctask = new GlideRecord("change_task");
var closereq = 1;
ctask.addQuery("change_request", current.change_request);
ctask.query();
while (ctask.next())
{
//Checks each task to see if there is one that remains open. If it finds an open task it flips the closereq variable to 2 to prevent the next piece of code.
if (ctask.state =='-5' || ctask.state=='-4' || ctask.state=='1' || ctask.state=='2'){
closereq = 2;
}
}
if (closetask == 0) //runs if it finds no more open records and searches for the change request associated with the task and closes it.
{
var creq = new GlideRecord("change_request");
creq.addQuery("sys_id", current.change_request);
creq.query();
while (creq.next())
{
creq.state = '3';
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2012 04:37 PM
Case 1 isn't working because you're trying to set a value on a record other than current. You'd need to query for the change_request GlideRecord, like you're doing in Case 2.
In Case 2, you checking a "closetask" variable for a value of 0, but I don't see that you're defining that variable name first. The variable you do define, closereq, initially has a value of 1. And you're not doing an update() on that GlideRecord after updating its state value.
See if the following gives you better results. There are other ways, like doing the query such that if it returns ANY records, you could exit, but I've modified your script as little as possible:
[code]
//Creates a query and searches the Change Task table for any additional tasks associated with the change.
var closereq = 1;
var ctask = new GlideRecord("change_task");
ctask.addQuery("change_request", current.change_request);
ctask.query();
while (ctask.next()) {
//Check each task to see if there is one that remains open
if (ctask.state == '-5' || ctask.state == '-4' || ctask.state == '1' || ctask.state == '2') {
closereq = 2;
break; // no need to keep looking
}
}
if (closereq == 1) {
// get the change request associated with the task and close it.
var creq = new GlideRecord("change_request");
if (!creq.get(current.change_request)) // we didn't find it
return;
creq.state = '3';
creq.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2012 05:01 PM
Ah yeah thanks for the help. Still on a lerning curve and my js kung foo is still a little weak. It didn't like the if statement you placed in the second piece, i was getting an invalid return error but i ended up using this. Can you see if there would be any problem? It works fine or appears too.
//Creates a query and searches the Change Task table for any additional tasks associated with the change.
var ctask = new GlideRecord("change_task");
var closereq = 1;
ctask.addQuery("change_request", current.change_request);
ctask.query();
while (ctask.next())
{
//Checks each task to see if there is one that remains open.
if (ctask.state =='-5' || ctask.state=='-4' || ctask.state=='1' || ctask.state=='2'){
closereq = 2;
break; //no need to keep looking
}
}
if (closereq==1){
//gets the change request associated with the task and closes it
var creq = new GlideRecord("change_request");
creq.addQuery("sys_id", current.change_request);
creq.query();
while (creq.next()){
creq.state = '3';
creq.active = false;
creq.update();
}
}