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

Hello @irfan12 

You will have to create 3 client script on each variable Q1, Q2, Q3.

onChange Client script for Q1, Q2, Q3 variable:

 

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

    //Type appropriate comment here, and begin script below
    // Get the values of q1, q2, q3
    var q1 = g_form.getValue('q1') === 'yes';
    var q2 = g_form.getValue('q2') === 'yes';
    var q3 = g_form.getValue('q3') === 'yes';

    // Initialize q4 value
    var q4 = '';

    // Condition 1: q1.(!q2) + (!q1).q2.(!q3)
    if ((q1 && !q2) || (!q1 && q2 && !q3)) {
        q4 = 'public';
    }
    // Condition 2: q1.q2.(!q3)
    else if (q1 && q2 && !q3) {
        q4 = 'private';
    }
    // Condition 3: q1.q2.q3 + (!q1).(!q2).(!q3)
    else if ((q1 && q2 && q3) || (!q1 && !q2 && !q3)) {
        q4 = 'general';
    } else {
		g_form.clearValue('q4');
	}
    // Set q4 field value
    g_form.setValue('q4', q4);
}

 

Note: The client script will remain same for all the 3 variables.

I have simplified all the 6 condition and separated the condition for public, private, general.

The condition works in my PDI.

Hope this helps!

 

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

 

Thank You
Juhi Poddar

Hello @irfan12 

Thank you for marking my response as helpful. 

If this helped you to resolve the issue, can you please mark this as an accepted solution.

 

Thank You

Juhi Poddar

Hello @irfan12 

Did you try this solution?

 

Thank You

Juhi Poddar

Runjay Patel
Giga Sage

Hi @irfan12 ,

 

You have to write three on change client script for Q1,Q2 and Q3.

Use same script in all three client script.

 

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

    var q1Value = g_form.getValue('q1'); 
    var q2Value = g_form.getValue('q2');
    var q3Value = g_form.getValue('q3');

    // Scenario 1
    if (q1Value == 'Yes' && q2Value == 'No' && q3Value == 'No') {
        g_form.setValue('q4', 'Some Value'); // Set desired value for Q4
    }
    // Scenario 2
    else if (q1Value == 'No' && q2Value == 'Yes' && q3Value == 'No') {
        g_form.setValue('q4', 'Another Value');
    }
/// Like wise you can create more scenarios.


    // Default case: Clear Q4
    else {
        g_form.clearValue('q4');
    }
}

 

 

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

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

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