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

Checking for open problem tasks when trying to close problem records

mflexman
Kilo Contributor

Im trying to get a business rule to check if a problem record has any pending problem tasks when a user tries to close the record down. My script is as follows:

//Query for associated tasks to see if they are pending
var target = new GlideRecord('problem_task');
target.addQuery('problem', current.number);
target.addQuery('state', 3);
target.query();

if(target.hasNext()){
//Is the user trying to resolve or cancel the problem record?
if ((current.problem_state == 4) || (current.problem_state == 5)){
gs.addInfoMessage("You have open tasks which must be completed before this record can be closed");
//return the state back to what it was
current.problem_state = previous.problem_state;
}
}
else{
gs.addInfoMessage("All tasks have been completed");
}

It can find a match on the state but doesn't appear to be able to match the problem record number with that recorded within the child task. Ive spent far too long trying to work this out which my gut feeling is telling me is rather a simple oversight.

Can anyone help please?

Thanks in anticipation
Mark

11 REPLIES 11

Look at mflexman reply for correct script.
Why NOT put a condition on the BR, current.problem_state.changesTo(4)? You don't need to waste time querying for tasks if you are NOT closing the Problem. If you do this you can eliminate the if ((current.problem_state == 4) || (current.problem_state == 5)) if statement.
BR should be set as 'Before, Update'
current.setAbortAction(true); is the line that will cancel the record submission


Hi, I've tried to use this script but can't get it to work.



Script once edited:-


var target = new GlideRecord('problem_task');


target.addQuery('problem', current.sys_id);


target.addQuery('state', 1).addOrCondition('state', 2).addOrCondition('state', -5);


target.query();




gs.addInfoMessage("There are still open Problem Tasks which must be completed before this record can be closed");


current.setAbortAction(true);



Condition:-  


current.problem_u_state.changesTo(Closed)



Error received when BR tries to run:-


Exception (ReferenceError: "Closed" is not defined. (; line 1)) occured while evaluating'current.problem_u_state.changesTo(Closed)' in business rule 'NRF - Check PTasks Closed b4 closing PRB' on problem:PRB0001738; skipping business rule







If I modify the text within the condition brackets I can get the error to not appear, but the rule doesn't stop the state being set to closed if problem tasks are still open. The value of u_state = closed is 'closed'. I tried changing this to '4' and then amending the condition to be (4) but this still didn't work.



Any ideas?


Hi Nat,


If 'Closed' is the value of your Choice then use:


current.problem.u_state.changesTo('Closed')



If not then you will have to put in Value of Closed in there. You can find that when you Click Personalize Choices after right clicking the Status Field.



Something like:-


current.problem.u_state.changesTo(7)// Assuming 7 is the value for Closed


Thanks, I have tried this and it's not erroring but is closing the problem when there are still open associated problem tasks. I've also tried with the below...



current.problem_u_state.changesTo('Closed')




Look at mflexman reply for correct script.
Why NOT put a condition on the BR, current.problem_state.changesTo(4)? You don't need to waste time querying for tasks if you are NOT closing the Problem. If you do this you can eliminate the if ((current.problem_state == 4) || (current.problem_state == 5)) if statement.
BR should be set as 'Before, Update'
current.setAbortAction(true); is the line that will cancel the record submission