On change of priority in incident form, making a custom field read-only/Set to "YES"

afreen shaik
Tera Contributor

RCA Required Attribute in the Incident form:

 

Requirement:
In the Incident form, there is a custom field labelled "Is RCA required" with two options: (a) Yes,

and (b) No.

afreenshaik_0-1731134863375.png

 

 

  • When the Priority field becomes “1-Critical” at any point in its lifecycle of incident , then “Is RCA required” field should automatically be set to “Yes” and become read-only , even  if the Priority field changes to “2-High” or “3-Moderate” or “4-Low”  even then “Is RCA required” field should be read-only.
  • If the Priority is directly set to other than “1-Critical”, then “Is RCA required” field should be editable. While trying to achieve this I’m facing some issues below is the approach I followed. Please review and suggest is there any alternative way to achieve this.

 

  • We have written a client script that checks the old and new values of the Priority field to determine if it has been set to “1-Critical.” If so, it sets the “Is RCA required” field to “Yes” and makes it read-only.

 

 

Script:

 

 

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

    if (isLoading) {

 

         if (newValue === '1') {

        g_form.setValue('u_is_rca_required', 'yes');

        g_form.setReadOnly('u_is_rca_required', true);

    }

    // Check if oldValue was '1' and newValue is '2', '3', or '4'

    else if (oldValue === '1' && ['2', '3', '4'].includes(newValue)) {

        g_form.setValue('u_is_rca_required', 'yes');

        g_form.setReadOnly('u_is_rca_required', true);

    } // If the form is loading, we exit the function early.

    }

 

    // Check if the new value is '1' (P1 incident)

    if (newValue === '1') {

        g_form.setValue('u_is_rca_required', 'yes');

        g_form.setReadOnly('u_is_rca_required', true);

    }

    // Check if oldValue was '1' and newValue is '2', '3', or '4'

    else if (oldValue === '1' && ['2', '3', '4'].includes(newValue)) {

        g_form.setValue('u_is_rca_required', 'yes');

        g_form.setReadOnly('u_is_rca_required', true);

    }

}

 

 

 

 

afreenshaik_1-1731134863380.png

 

 

 

Please suggest the possible ways to achieve the above requested scenario @Ankur Bawiskar @Pranav Bhagat @Chuck Tomasi @Pradeep Sharma @Earl Duque @Laurel McDevitt 

 

1 ACCEPTED SOLUTION

Hello @afreen shaik 

After thoroughly analyzing the scenario and testing various approaches, I identified a solution to meet the requirement.

To implement this, follow these steps:

  1. Create a new field (e.g., RCA) on the Incident table. This field can be hidden on the form, and it will store a value (such as "Yes") whenever the Priority is set to Critical (P1).

  2. Set up a UI Policy to monitor the Priority field. When the Priority is "1", this policy will set the RCA field to "Yes".

  3. Create another UI Policy that checks if RCA is set to "Yes". If true, this policy will set the "Is RCA Required" field to "Yes" and make it read-only.

Using this approach, the RCA field will act as a persistent indicator of whether Priority was set to 1 at any point in the incident lifecycle, ensuring Is RCA Required is accurately set and remains read-only as needed.

Creating a new field(RCA) on incident table:

JuhiPoddar_3-1731344418741.png

UI policy in list view: 

JuhiPoddar_0-1731343443977.png

UI policy 1: configuration setup

Short description: set RCA if priority is critical

order: 100

Condition: priority is 1-critical

Reverse if false: unchecked

 

JuhiPoddar_1-1731343624031.png

 

 

function onCondition() {
    // Set RCA field to "Yes"
    g_form.setValue('u_rca', 'Yes');
}

 

 

 

UI policy 2: configuration setup

Short description: If RCA is set to Yes

order: 200

Condition: RCA is Yes

Reverse if false: unchecked

JuhiPoddar_2-1731343907671.png

 

function onCondition() {
    // Set u_is_rca_required field to "Yes"
    g_form.setValue('u_is_rca_required', 'Yes');
    // Make u_is_rca_required field read-only
    g_form.setReadOnly('u_is_rca_required', true);
}

 

 

 

"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

9 REPLIES 9

Jean-Emmanuel
Tera Guru

Hello @afreen shaik , I am not sure to understand why you remove the return syntax in the "is loading" code section. I think somethink like the following code will do the trick. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '' || oldValue == '1') {
        if (newValue == '1') {
            g_form.setValue('u_is_rca_required', 'yes');
            g_form.setReadOnly('u_is_rca_required', true);
        }
        return;
    }
	
    if (newValue == '1') {
        g_form.setValue('u_is_rca_required', 'yes');
        g_form.setReadOnly('u_is_rca_required', true);
    }
}

 

 

Hello Jean,

 

Thanks for your response. 

I have tried your code, but it only works when the 'Priority' is 'P1'. When it changes from 'P1' to 'P2' or from 'P2' to 'P3,' it does not make the custom field("u_is_rca_required") read-only.

The requirement is to set the custom field to read-only and "Yes" whenever the 'Priority' is 'P1' at any point.

 

Best Regards,

Afreen Shaik

Runjay Patel
Giga Sage

Hi @afreen shaik ,

 

You can use below script, i have tested it, working well.

 

if (g_form.isNewRecord() && newValue == '1') {
        g_form.setValue('u_is_rca_required', 'software');
        g_form.setReadOnly('u_is_rca_required', false);

    } else if (oldValue == '1' || newValue == '1') {

        g_form.setValue('u_is_rca_required', 'software');
        g_form.setReadOnly('u_is_rca_required', true);

    } else {
        g_form.clearValue('u_is_rca_required');
        g_form.setReadOnly('u_is_rca_required', false);
    }

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

SufiyanMurshad
Tera Contributor

Hello afreen shaik,

You can use client script.

 client script type: Onchange

Field Name: priority 

 

Client script:

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

if(newValue=='1'){
    g_form.setValue('u_rc','yes');
    g_form.setReadOnly('u_rc',true);
}
if(newValue=='2'){
    g_form.setValue('u_rc','no');
    g_form.setReadOnly('u_rc',true);
}
   if(newValue=='3'){
   g_form.setValue('u_rc','no');
   g_form.setReadOnly('u_rc',true);
}
if(newValue=='4'){
g_form.setValue('u_rc','no');
g_form.setReadOnly('u_rc',true);
}
}
 

Best Regards,

Sufiyan Murshad