How could you get the risk assessment to calculate automatically?

geek1
Kilo Contributor

How would you get the risk assessment calculate on a change request to populate as soon as you click submit on the assessment so you don't then need to click the UI action to calculate?

 

I tried piecing both the UI actions together (see below) but it's not performing the calculation. It saves the risk assessment but that's it.

19 REPLIES 19

Yes, I think that's better. Thank you.


Thanks for the script, it seems to work great at the moment.



But just for anyone else reading I think there's a typo in the condition


current.instance.survey.name == 'Change Risk Assesment';  



It should be Assessment with four 's'



In our system we have different Risk Assessments depending on the Category so I changed the condition to


current.instance.survey.name.indexOf('Change Risk Assessment') > -1;



Hope that helps someone else looking at this.


benn23
ServiceNow Employee
ServiceNow Employee

I'm kind of late to this but here's how I've tackled this one in the past.


Create a copy of the 'calculate risk' UI Action and give it an action name (ex: calc_risk_snc).   Don't check any of the boxes that define where to show the UI Action.


Add the following line to the 'Fill Out Risk Assessment' UI Action:


if (eventObj.view.mandatoryResult) {


  isSubmitted = true;


                                                              d.close();


setTimeout(function(){ gsftSubmit(null, g_form.getFormElement(), 'calc_risk_snc') }, 1000);                                                           }


                                              });


I like this suggestion because it simplifies the solution. Some additional considerations:


  • Remember to remove or update the conditions for the new (invisible) UI action or you may get the action unauthorized message.
  • If you are only using risk assessment and you are not also using risk conditions, you can set the calculation method property to none. This will allow the calculation to only occur when it's needed: when the user submits the assessment responses (avoids running a second calculation/infoMessage display triggered by the initial calculation update, and also avoids unnecessary runs on future updates when the assessment isn't being answered).
  • If you are using risk conditions along with risk assessments, you might want to consider the earlier solution based on the survey response business rule. Since you will also have the calculation business rule running, you may be able to avoid the redundant calculation/infoMessages with this solution. When I changed the snippet below to use the setForceUpdate method instead of calling the calculator, the business rule still ran, but the duplicate messages seemed to stop so it appears to be running only once. Pair this with the additional line below in the Fill Out Risk Assessment UI action and you get the best of both worlds (the survey_response runs in the background and then the form reloads with the updated value).


function calcRisk(change) {


  //var risk = calc.calculateRisk(change);


  //chg.u_has_assessment = true;



  chg.setForceUpdate(true);


  chg.update();


  }




setTimeout(function(){ location.reload(true); }, 2000);


ebak
Giga Contributor

Hello Benn, I tried putting that line of code in as you suggested (which is missing a semi-colon after the gsftSubmit(null, g_form.getFormElement(), 'calc_risk_snc') line)



setTimeout(function(){ gsftSubmit(null, g_form.getFormElement(), 'calc_risk_snc') }, 1000);



I get the Survey popup window (Change1.jpg). However, when I click the Submit, the main browser window reloads with the exact same survey (Change2.jpg), and not with the change record. I click the Submit again and then I get the Change Record in the main browser window (Change3.jpg)



Any idea why that is happening? The Fill Out Risk Assessment and Calculate Risk (new copy as calc_risk_snc) UI Actions are OOTB.



Thanks,
Ed