How to calculate Risk level on change table using Risk conditions

kumar junior11
Tera Contributor

when we click on related link (calculate risk) on change record it will set risk level. time window is a new custom choice field having options(business, on call), i tried this but only one condition satisfying but not other from the below. can any one help me here, 

Change TypeService (business unit)Time Windowauto set Risk Level
Normalcriticality 1On CallHigh
Normalcriticality 2Business HoursModerate
NormalCriticality 3On callmoderate
Normalcriticality 4Business Hours/On CallLow
Normalcriticality 1business HoursHigh

 

2 REPLIES 2

AnkaRaoB
Mega Guru

Hi @kumar junior11 ,

When clicking “Calculate Risk”, only one condition works while others don’t. This happens due to case mismatches, overlapping conditions, or incorrect if/else logic.

 

 Follow the below Steps

Step 1: Normalize Field Values

Make sure choice values match exactly (case + spacing).

var changeType = current.type + '';

var criticality = current.u_service_criticality + '';

var timeWindow = current.u_time_window + '';

criticality = criticality.toLowerCase().trim();

timeWindow = timeWindow.toLowerCase().trim();

Step 2: Use Proper if / else if Logic

Avoid multiple independent if statements.

if (changeType == 'Normal') {

    if (criticality == 'criticality 1' && timeWindow == 'on call') {

        current.risk = 'High';

    } else if (criticality == 'criticality 1' && timeWindow == 'business hours') {

        current.risk = 'High';

    } else if (criticality == 'criticality 2' && timeWindow == 'business hours') {

        current.risk = 'Moderate';

    } else if (criticality == 'criticality 3' && timeWindow == 'on call') {

        current.risk = 'Moderate';

    } else if (

        criticality == 'criticality 4' &&

        (timeWindow == 'business hours' || timeWindow == 'on call')

    ) {

        current.risk = 'Low';

    }

}

Step 3: Debug Values (If Needed)

Check what values are actually coming from the form.

gs.info('Criticality: ' + criticality);

gs.info('Time Window: ' + timeWindow);

(View logs in System Logs → All)

 Best Practices

Instead of hard-coding logic:

Create a Risk Matrix lookup table

Query the table based on Change Type, Criticality, and Time Window

Set Risk dynamically

✔ Easier maintenance

✔ No script changes needed later

✔ Cleaner logic

If this helps you then mark it as helpful and accept it as solution.

Dr Atul G- LNG
Tera Patron

Hi @kumar junior11 

 

I created a video on the same :

 

https://youtu.be/0gdkXuN9KFc

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************
LinkedIn:https://www.linkedin.com/in/atulgroverservicenowconsultant Redistribution or copying of functionality is not allowed!