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.

Calculating Risk Rating

Sanjay33
Tera Contributor

Hi Everyone,

I have two fields ( 'Risk Value' and 'Risk Rating') on the form, based on the Risk Value like,

0-5->low
6-10->Medium
11-15->High
>16-very high

Risk rating will be calculated like low, Medium, High, etc.

Code:

After BR: updated checked

Condition: Risk Value changes

------------------------------------------------------------------------------------

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var result = '';
    var val = current.getValue('risk_value');
    gs.info('val');
    if (val < 5)
        result = 'LOW';
    else if (val < 10)
        result = 'Medium';
    else if (val < 15)
        result = 'High';
    else if (val > 15)
        result = 'Very High';
    var ans = result.split('.');
    gr.risk_rating = ans[0];
    gr.update();
    gs.addInfoMessage("Risk score:" + risk_rating);

 

})(current, previous);

Code is not working, Could you please help me on this.

 

FYI,

 

Thank you.

2 REPLIES 2

Kalyani Jangam1
Mega Sage

Hi Try below code

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var result = '';
    var val = current.getValue('risk_value');
    gs.info(val);
    var result = '';
   if (val < 5){

 result = 'LOW';
}
    else if (val < 10){
        result = 'Medium';
}
    else if (val < 15){
        result = 'High';
}
    else if (val > 15){
        result = 'Very High';
}
    var ans = result.split('.');
current.setValue('risk_rating',ans); // rather than  gr.risk_rating = ans[0] because we never declared gr object

})(current, previous);

Please check it helpful or not

Sanjay33
Tera Contributor

Its working as expected now by using this code.

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var result = '';
    var val = current.getValue('risk_value');
    gs.info('val');
    if (val < 5)
        result = 'LOW';
    else if (val < 10)
        result = 'Medium';
    else if (val < 15)
        result = 'High';
    else if (val > 15)
        result = 'Very High';
    var ans = result.split('.');
    current.risk_rating = ans[0];          // changed to current from gr
    current.update();                       // changed to current from gr
    gs.addInfoMessage("Risk score:" + risk_rating);

 

})(current, previous);