Calculate the Risk on Client Script

Shaikh Mzhar
Tera Guru

Hi,

 

I have a requirement to calculate the risk on on-change of client script on one of the field before record created, I have checked with code below (Calling OOTB function call, from OOTB Script Include), but this code only works after record created. i have not found any function which will work before record created

 

Below is the code used, I was using in Script include which is I am returning to Client Script with GlideAjax

 

 

// Check if it's a new record or existing
var RiskValue = '';
        var isNewRecord = !this.getParameter('sysparm_sys_id');
        var new_disruption = this.getParameter('sysparm_u_disruption');
        var new_type = this.getParameter('sysparm_typ');
        var new_cmdb = this.getParameter('sysparm_cmdbci');

        // Create GlideRecord object
        var chg = new GlideRecord('change_request');

        if (isNewRecord) {
            chg.initialize();
            chg.setValue('type', new_type);
            chg.setValue('cmdb_ci', new_cmdb);

        } else {
            var id = this.getParameter('sysparm_sys_id');
            chg.get(id);
            chg.setValue('u_disruption', new_disruption);
        }
        chg.setValue('u_disruption', new_disruption);

        var riskCalculator = new global.RiskCalculator(chg);
        riskCalculator.setDryRun(true);
        riskCalculator.setHideNotification(true);
        var evaluatedRiskImpact = riskCalculator.evaluateRiskImpact();

        return evaluatedRiskImpact.riskCondition.risk.display_value;

 

 

@Maik Skoddow @Ankur Bawiskar Could you please guide?

9 REPLIES 9

you didn't get my query, I wanted to calculate the risk before form save. code will not work on record until save.

Hi @Shaikh Mzhar 

 

I'm not sure why you want to calculate the risk before saving. OOTB risk is set by default when it loads, so it's better to modify it there instead of changing that script

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

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/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Shaikh Mzhar
Tera Guru

This code is written on Script Include, Which I was calling with GlideAjax in on-Change of field disruption, if I tried same code in Background script with all value populated, this is not giving correct risk value as per criteria define for new record, same is working for exiting record. 

 

Also would like to highlight that, if I insert record after value set in chg obj in first if condition then it gives correct Risk value.

 

My concern is Can we calculate the Risk Value from for new record? if yes then the method I used will help or do i needs to use other method from OOTB Script include

@Shaikh Mzhar 
The OOTB one won’t work for new records are they aren’t created yet by the time you are calculating the risk.

 

You can check the OOTB script and try implementing a similar one on your client scripts based on fields from the change request form.

 

Downside: if you change anything within the risk formula you would need to do the same for your on-change scripts.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Shaikh Mzhar 

Why are you using the script? It's better to add that field as a question for risk assessment ⚖️ and assign a weight to it. The system will handle it for you!

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

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/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************