cancel second assessment on closure of first assessment survey instance

Surendra6
Tera Contributor

Hi ,

 

We have two survey assessment instances added to the same incident whenever it is closed. We want second could be cancel if we complete the first survey instance. 

 

I have written a after business rule with below code however it is not working.

 

if (current.state == complete) {

var gr2 = new GlideRecord('asmt_assessment_instance');
gr2.addQuery('parent', current.parent);
// gr2.addQuery('state', 'ready');
gr2.addQuery('sys_id', '!=', current.sys_id);
gr2.query();
if (gr2.next()) {
gr2.sate = 'canceled';
gr2.update();

}

}

 

Can someone please help how to solve the issue.

 

Thanks!!

3 REPLIES 3

SwarnadeepNandy
Mega Sage

Hello @Surendra6,

 

  • One possible reason why your business rule is not working is that you have a typo in your code. You wrote gr2.sate = 'canceled'; instead of gr2.state = 'canceled';. This might cause the state of the second survey instance to remain unchanged. You should correct this typo and try again.
  • Another possible reason why your business rule is not working is that you are using the parent field to query the survey instances. The parent field is a reference to the asmt_metric_type table, which stores the metric types for the surveys. This means that your query might return more than one survey instance that has the same metric type, but not necessarily the same incident. You should use the task_id field instead, which is a reference to the task table, which stores the incidents. You should change your query to gr2.addQuery('task_id', current.task_id); and try again.
  • A third possible reason why your business rule is not working is that you are using an after-business rule, which runs after the current record is inserted or updated. This might cause a race condition, where the second survey instance is not yet created when the business rule runs. You should use a before business rule instead, which runs before the current record is inserted or updated. This way, you can prevent the second survey instance from being created in the first place. You should change your business rule type to before and try again.

I hope this helps

 

Kind Regards,

Swarnadeep Nandy

Hi @SwarnadeepNandy,

 

I have updated the code as you suggested and tried but still the second assessment instance is not moving to cancel state.

 

if (current.state == complete) {

var gr2 = new GlideRecord('asmt_assessment_instance');
gr2.addQuery('task_id', current.task_id);
gr2.addQuery('sys_id', '!=', current.sys_id);
gr2.query();
if (gr2.next()) {
gr2.state = 'canceled';
gr2.update();

}
}

 

Please help here.

 

Thanks!!

Surendra,

 

Not sure if you are still having this issue but it also appears to have a typo in the first line. Your code is comparing 'current.state' to a variable called 'complete'. Should be;

if (current.state == 'complete') {

 

Outside of that, if the second survey doesn't matter why are you guys generating it?