Fields being incorrectly updated in Catalogue item via a client script

matthew_hughes
Kilo Sage

I've got 5 onChange Client Scripts to be applied to 5 fields in a catalogue item that I've been working on. The five fields are:

matthew_hughes_0-1762351747342.png

The values of the five fields should automatically be updated to Severe (3) if the following question is Highly Confidential:

matthew_hughes_1-1762351840030.png

That works fine. I've got another requirement that states that if the above question is not Highly Confidential and the below question is 'Yes', then the values of the 5 fields should be set to Material(4) 

matthew_hughes_3-1762352004251.png

However, what I've found in my client script is that if the first question is Highly Confidential and the 5 fields are below Material, if I then set the PCI DSS question to 'Yes', then the fields automatically get updated to 'Material'. This should not happen since the first question is Highly Confidential and should not consider the outcome of the PCI DSS question, so the five fields shouldn't get updated The below code I'm using is:

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

    //Declare the required variables
    var reputation = g_form.getValue('confidentiality_reputation');
    var regular = g_form.getValue('confidentiality_regulatory');
    var finance = g_form.getValue('confidentiality_finance_reporting');
    var colleagues = g_form.getValue('confidentiality_colleagues_incidents_objectives');
    var customer = g_form.getValue('confidentiality_customer');

    var confidenRating = g_form.getValue('what_classification_of_data_is_consumed_processed_or_created_in_this_application');
    var pcsResponse = g_form.getValue('is_the_application_in_scope_of_pci_dss');
    var sox = g_form.getValue('sox_control') == 'true';
    var extSox = g_form.getValue('external_auditor') == 'true';

    //Set the boolean field to True
    var booleanField = true;

    //Set the context field to empty to determine the context of the three functions
    var context = '';

    // Determine context and value of the booleanField
    if (confidenRating == 'highly_confidential') {
        booleanField = isValid_HighlyConfidential(newValue, reputation, customer, regular, colleagues);
        context = 'highly_confidential_function';
        alert('Highly Confidential Function Applied');
    } else if (pcsResponse == 'yes') {
        booleanField = isValid_PCSDSS(newValue, reputation, customer, regular, colleagues);
        context = 'pcsResponse_function';
        alert('PCS Response Function Applied');
    } else if (sox && extSox) {
        booleanField = isValid_Sox(newValue, reputation, customer, regular, colleagues);
        context = 'sox_function';
        alert('Sox Function Applied');
    }

    // Apply logic based on validation result
    if (booleanField == false) {
        if (context == 'highly_confidential_function') {
            g_form.setValue('confidentiality_finance_reporting', 5);
            g_form.setMandatory('rationale_finance_reporting', false);
        } else if (context == 'pcsResponse_function' || context == 'sox_function') {
            g_form.setValue('confidentiality_finance_reporting', 4);
            g_form.setMandatory('rationale_finance_reporting', false);
        }
    } else {
        g_form.setValue('confidentiality_finance_reporting', newValue);

        // Mandatory logic based on the context variable
        if (context == 'highly_confidential_function' && newValue < 5) {
            g_form.setMandatory('rationale_finance_reporting', true);
        } else if ((context == 'pcsResponse_function' || context == 'sox_function') && newValue < 4) {
            g_form.setMandatory('rationale_finance_reporting', true);
        } else {
            g_form.setMandatory('rationale_finance_reporting', false);
        }
    }

    // Calculate Confidentiality Rating based on max score
    var max = Math.max(customer, reputation, finance, colleagues, regular);

    switch (max) {
        case 5:
            g_form.setValue('confidentiality_rating', 'Severe');
            break;
        case 4:
            g_form.setValue('confidentiality_rating', 'Material');
            break;
        case 3:
            g_form.setValue('confidentiality_rating', 'Moderate');
            break;
        case 2:
            g_form.setValue('confidentiality_rating', 'Minor');
            break;
        default:
            g_form.setValue('confidentiality_rating', 'No Impact');
    }

    // Validation Functions for the three required scenarios as part of story SFSTRY0042589

    //Apply the below function if the anwser to the question 'What classification of data is consumed, processed or created in this application?' is 'Highly Confidential'
    function isValid_HighlyConfidential(newValue, reputation, customer, regular, colleagues) {
        if (newValue == 5) return false;
        if (newValue < 4 && reputation <= 3 && customer <= 3 && colleagues <= 3 && regular <= 3) {
            alert("You must have at least one Confidentiality score at Severe/Material");
            return false;
        }
        return true;
    }

    //Apply the below function if the anwser to the question 'Is the application in scope of PCI DSS?' is 'Yes'
    function isValid_PCSDSS(newValue, reputation, customer, regular, colleagues) {
        if (newValue < 3 && reputation <= 2 && customer <= 2 && colleagues <= 2 && regular <= 2) {
            alert("You must have at least one Confidentiality score at Material/Moderate");
            return false;
        }
        return true;
    }

    //Apply the below function if the 'SOX Control' and 'External Auditor SOX Control' fields are true
    function isValid_Sox(newValue, reputation, customer, regular, colleagues) {
        if (newValue < 3 && reputation <= 2 && customer <= 2 && colleagues <= 2 && regular <= 2) {
            alert("You must have at least one Confidentiality score at Material/Moderate");
            return false;
        }
        return true;
    }
}

 

 

 

1 ACCEPTED SOLUTION

I found the issue. It was because I was referring to the wrong variable for the PCS field in a different client script

View solution in original post

6 REPLIES 6

AlexandreD83256
ServiceNow Employee
ServiceNow Employee

Hi Matthew, I found overtime that having multiple client scripts running simultaneously can cause unexpected situations. Rather than having an onChange() client script for each field that you wish to update, think of a client script for each situation that could affect your form.

In this case, I would strongly recommend you call a Script Include to host your logic and return which value each field on the form should be populated with. First it will clean your script and second improve performance. As a result, you would have 2 onChange() scripts (that call the same script include function) for the two questions that could impact the value of the 5 fields.

Hope it helps !

Cheers,
Alex

I found the issue. It was because I was referring to the wrong variable for the PCS field in a different client script