Calculate the Risk on Client Script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 07:14 PM - edited 03-09-2025 01:51 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 07:29 PM
Hello Shaikh Mzhar ,
As you mentioned if this scripts written on on change client scripts then it is not working on form loading.
When your record is new which its before to create and not existing in the database.
Below are the steps you can use to troubleshoot.
1.Find any the script /any default value defined for risk field.
2.If any such type script code exist you can reactive then and write your or modified same.
3. Write alternative script on load form when record is new and defined your condition.
Supriya Waghmode |ServiceNow Consultant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 07:42 PM
@Shaikh Mzhar
Have you created on change client scripts for all fields used for risk calculation?
Do you check during calculation if some of those fields are still empty?
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 07:52 PM
For new record:
var riskCalculator = new global.RiskCalculator(chg);
Chg is -1 as the change request is not yet created.
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 07:57 PM
Hello @Shaikh Mzhar ,
There is an existing method in g_form to check if it is a new record.
g_form.isNewRecord() - please work with this exactly.
Thanks and Regards,
Shivalika Gupta
Servicenow Developer - Schaeffler