We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Make Risk field read only in change request form

karunasrikuna
Tera Contributor

Hi , I am currently working on few use cases where I am facing a challenge on one of the use cases, which is described below. 

 

In the change request form, when we select "Impacted application" as "xyz" or " Affected CI" as "123" or "Service Related CIs" as "abc" then "Risk" field should be default to "moderate" and read only. To achieve this, I have used below client script:

 

   var impApp = g_form.getValue('u_impacted_application');
    var ci = g_form.getValue('u_related_ci_list');
    var affci = g_form.getValue('ci_item');
    if (impApp == 'xyz'); {
        g_form.setValue('risk', 3);
        g_form.setReadOnly('risk', true);

    }
    if (ci == 'abc'); {
        g_form.setValue('risk', 3);
        g_form.setReadOnly('risk', true);

    }
    if (affci == '123'); {
        g_form.setValue('risk', 3);
        g_form.setReadOnly('risk', true);
    }
   
}
 
I have used the same script for "on submit" as well. When I am creating a change request this is working but after submitting the request the 'Risk" field is editable. Attaching Snippets for your reference.
 
@Ankur Bawiskar AG - LearnNGrow any quick help will be appreciated.
4 REPLIES 4

Siddhesh Jadhav
Kilo Sage

Hi @karunasrikuna ,

You can try this 

 

1. Fix the Client Script (onChange Event):

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var impApp = g_form.getValue('u_impacted_application');
var ci = g_form.getValue('u_related_ci_list');
var affci = g_form.getValue('ci_item');

if (impApp == 'xyz' || ci == 'abc' || affci == '123') {
g_form.setValue('risk', 3);
g_form.setReadOnly('risk', true);
}
}

 

2. Add an onLoad Client Script to Maintain Read-Only Property After Submission:

 

function onLoad() {
var impApp = g_form.getValue('u_impacted_application');
var ci = g_form.getValue('u_related_ci_list');
var affci = g_form.getValue('ci_item');

if (impApp == 'xyz' || ci == 'abc' || affci == '123') {
g_form.setValue('risk', 3);
g_form.setReadOnly('risk', true);
}
}

 

If this helps resolve your query, please accept the answer and mark it as helpful.

 

Best Regards,
Siddhesh Jadhav

Ankur Bawiskar
Tera Patron

@karunasrikuna 

ensure your onChange runs onLoad as well

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {

        // your here
        var impApp = g_form.getValue('u_impacted_application');
        var ci = g_form.getValue('u_related_ci_list');
        var affci = g_form.getValue('ci_item');
        if (impApp == 'xyz'); {
            g_form.setValue('risk', 3);
            g_form.setReadOnly('risk', true);

        }
        if (ci == 'abc'); {
            g_form.setValue('risk', 3);
            g_form.setReadOnly('risk', true);

        }
        if (affci == '123'); {
            g_form.setValue('risk', 3);
            g_form.setReadOnly('risk', true);
        }

        return;
    }

   // your further logic here


}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Medi C
Giga Sage

@karunasrikuna 

Please add an onLoad script as well

function onLoad() {
var impApp = g_form.getValue('u_impacted_application');
var ci = g_form.getValue('u_related_ci_list');
var affci = g_form.getValue('ci_item');

if (impApp == 'xyz' || ci == 'abc' || affci == '123') {
g_form.setValue('risk', 3);
g_form.setReadOnly('risk', true);
}
}

Thanks & Best regards,
Medi

Dr Atul G- LNG
Tera Patron

Hi @karunasrikuna 

 

The experts have already answered, but when you run the risk assessment, the outcome may change based on the findings from the assessment.

*************************************************************************************************************
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]

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