Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Else if not working in client script for state field

rmaroti
Tera Contributor

when state == null then no one reason field will be display.

 

could you please check below code:

else if(state == '')
{
g_form.setMandatory('u_in_progress_reason',false);
g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason', false);
g_form.setMandatory('u_closed_reason',false);
g_form.setDisplay('u_closed_reason', false);
g_form.setMandatory('u_resolved_reason',false);
g_form.setDisplay('u_resolved_reason',false);
}

8 REPLIES 8

Harsh_Deep
Giga Sage
Giga Sage

Hello @rmaroti 

 

You can use this -

if(!state)
{
g_form.setMandatory('u_in_progress_reason',false);
g_form.setDisplay('u_in_progress_reason', false);
g_form.setMandatory('u_new_reason',false);
g_form.setDisplay('u_new_reason',false);
g_form.setMandatory('u_cancelled_reason',false);
g_form.setDisplay('u_cancelled_reason', false);
g_form.setMandatory('u_on_hold_reason',false);
g_form.setDisplay('u_on_hold_reason', false);
g_form.setMandatory('u_closed_reason',false);
g_form.setDisplay('u_closed_reason', false);
g_form.setMandatory('u_resolved_reason',false);
g_form.setDisplay('u_resolved_reason',false);
}

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

I have try it but not working still.

Amit Gujarathi
Giga Sage
Giga Sage

HI @rmaroti ,
I trust you are doing great.
Please find the updated code

function setFieldProperties(fieldName, isMandatory, isDisplayed) {
    g_form.setMandatory(fieldName, isMandatory);
    g_form.setDisplay(fieldName, isDisplayed);
}

if (state === null || state === '') {
    var fields = [
        'u_in_progress_reason',
        'u_new_reason',
        'u_cancelled_reason',
        'u_on_hold_reason',
        'u_closed_reason',
        'u_resolved_reason'
    ];

    fields.forEach(function(field) {
        setFieldProperties(field, false, false);
    });
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thanks for help but I'm in learning phase, so  i need to achieve this is only through client script even some other case also last else if or else is not working i want to know why this happened so please help me in that.