If Q1 is Yes and Q2 is No and Q3 is No then i need to display q4 with some value .

irfan12
Tera Contributor

Hello Everyone,

 

If Q1 is Yes and Q2 is No and Q3 is No then i need to display q4 with some value . Like this i have 5-6 probability scenarios. i have used Client scripts. How ever few scenarios are not working and for few the Q4 value is not getting cleared. Can anyone help me on this?

Hopefully looking for possible solution, Thanks in advance.

23 REPLIES 23

Ankur Bawiskar
Tera Patron
Tera Patron

@irfan12 

you will require multiple onChange client scripts

1 for Q1, another for Q2 and another for Q3 and in each of those client scripts you need to check what's the value of other 2 variables and based on that show Q4 with some value

share your 3 onChange client scripts here

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

How to write these scripts any script for reference?

Because i have 

Yes No No

Yes Yes No

Yes Yes Yes

No No No

Yes No Yes

No Yes No

@irfan12 

I already informed you will require 3 onChange and handle all the combinations

something like this, please enhance it for your case

Script 1: Q1 onChange

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

function checkConditions() {
    var q1 = g_form.getValue('Q1');
    var q2 = g_form.getValue('Q2');
    var q3 = g_form.getValue('Q3');
    
    if (q1 == 'Yes' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('Q4', 'Some Value');
    } else {
        g_form.clearValue('Q4');
    }
}

Script 2: Q2 onChange

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

function checkConditions() {
    var q1 = g_form.getValue('Q1');
    var q2 = g_form.getValue('Q2');
    var q3 = g_form.getValue('Q3');
    
    if (q1 == 'Yes' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('Q4', 'Some Value');
    } else {
        g_form.clearValue('Q4');
    }
}

Script 3: Q3 onChange

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

function checkConditions() {
    var q1 = g_form.getValue('Q1');
    var q2 = g_form.getValue('Q2');
    var q3 = g_form.getValue('Q3');
    
    if (q1 == 'Yes' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('Q4', 'Some Value');
    } else {
        g_form.clearValue('Q4');
    }
}
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

With the help of these client scripts will cover 3 scenarios, how about the remaining 3 scenarios. Need to create 3 more client scripts?