OnChange client script to revert state to new

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2023 01:44 AM
Hi everyone I had a requirement asking there needs to be a check that the risk has been assessed before progressing to the Assess state. This could be a check on the existence of a change risk detail record, so that the change cannot progress without risk having been assessed. An onChange client script on change of state to Assess with an Ajax lookup of the change risk detail records would work for this - putting the state field value back to New if the risk assessment hasn't been run
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2023 09:15 AM
Hi @Community Alums
For this, write a code on Assess UI action, to check if the Risk Assessment exits or not, if not don't move the change to Next state.
I guess it is OOTB avail.
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2023 08:59 PM
HI @Community Alums ,
I trust you are doing great.
Please find the below scripts for the same :
Client script :
// Client Script - Type: onChange
// Field Name: state (adjust according to your field name)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Check if state is changing to 'Assess'
var targetState = 'Assess'; // Replace with the appropriate value or sys_id of the Assess state
if (newValue === targetState) {
// Perform AJAX to check for risk assessment
var ga = new GlideAjax('CheckRiskAssessment');
ga.addParam('sysparm_name', 'hasRiskAssessment');
ga.addParam('sysparm_change_sys_id', g_form.getUniqueValue());
ga.getXML(checkRiskResponse);
}
}
function checkRiskResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer === 'false') {
// Revert state to 'New' and alert the user
g_form.setValue('state', 'New'); // Replace 'New' with the appropriate value or sys_id
alert('Risk assessment is required before progressing to Assess.');
}
}
And the corresponding Script Include:
// Script Include
var CheckRiskAssessment = Class.create();
CheckRiskAssessment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasRiskAssessment: function() {
var changeSysId = this.getParameter('sysparm_change_sys_id');
var riskAssessmentFound = false;
// Query the change risk detail table to check for associated records
var gr = new GlideRecord('change_risk_detail'); // Replace with your table name
gr.addQuery('change_record', changeSysId);
gr.query();
if (gr.next()) {
riskAssessmentFound = true;
}
return riskAssessmentFound;
},
type: 'CheckRiskAssessment'
});
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi