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

@irfan12 

same 3 onChange client scripts can handle all the combinations.

The above scripts are just for your reference. please enhance it further

I hope I answered your question.

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

@irfan12 

here are the updated client scripts

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', 'Public');
    } else if (q1 == 'Yes' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('Q4', 'Private');
    } else if (q1 == 'Yes' && q2 == 'Yes' && q3 == 'Yes') {
        g_form.setValue('Q4', 'General');
    } else if (q1 == 'No' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('Q4', 'General');
    } else if (q1 == 'Yes' && q2 == 'No' && q3 == 'Yes') {
        g_form.setValue('Q4', 'Public');
    } else if (q1 == 'No' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('Q4', 'Public');
    } 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', 'Public');
    } else if (q1 == 'Yes' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('Q4', 'Private');
    } else if (q1 == 'Yes' && q2 == 'Yes' && q3 == 'Yes') {
        g_form.setValue('Q4', 'General');
    } else if (q1 == 'No' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('Q4', 'General');
    } else if (q1 == 'Yes' && q2 == 'No' && q3 == 'Yes') {
        g_form.setValue('Q4', 'Public');
    } else if (q1 == 'No' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('Q4', 'Public');
    } 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', 'Public');
    } else if (q1 == 'Yes' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('Q4', 'Private');
    } else if (q1 == 'Yes' && q2 == 'Yes' && q3 == 'Yes') {
        g_form.setValue('Q4', 'General');
    } else if (q1 == 'No' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('Q4', 'General');
    } else if (q1 == 'Yes' && q2 == 'No' && q3 == 'Yes') {
        g_form.setValue('Q4', 'Public');
    } else if (q1 == 'No' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('Q4', 'Public');
    } 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

Hello Ankur,

 

Thank you for your reply.

Initially i have written first Catalog Client Script. How ever its not working.

Any Changes?

..........................................

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

function checkConditions() {
    var q1 = g_form.getValue('user_will_have_private_danone_equipment_such_as_a_laptop_desktop');
    var q2 = g_form.getValue('user_will_be_a_field_worker');
    var q3 = g_form.getValue('user_will_be_a_factory_worker');
   
    if (q1 == 'Yes' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('type_of_license', 'regular');
    } else if (q1 == 'Yes' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('type_of_license', 'regular');
    } else if (q1 == 'No' && q2 == 'No' && q3 == 'Yes') {
        g_form.setValue('type_of_license', 'basic');
    } else if (q1 == 'No' && q2 == 'No' && q3 == 'No') {
        g_form.setValue('type_of_license', 'basic');
    } else if (q1 == 'Yes' && q2 == 'No' && q3 == 'Yes') {
        g_form.setValue('type_of_license', 'regular');
    } else if (q1 == 'No' && q2 == 'Yes' && q3 == 'No') {
        g_form.setValue('type_of_license', 'light');
    } else {
        g_form.clearValue('type_of_license');
    }
}

 

@irfan12 

the script I shared should work fine.

Are you giving the correct choice value in the comparisons?

I could see in your below screenshot it did populate value as Regular for your combination

So it's working as expected

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

I want like this 

irfan12_0-1735025395712.png