g_form.setMandatory in client script is not working on the workspace but working in native view.

Sathwik1
Tera Expert

 

g_form.setMandatory in client script is not working on the workspace but working in normal view.



OnChange Client script

OnChange Field Name : Account

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
var gId = '1234493d47ba6a507c23de79116d7890';
if (newValue ===gId) {
g_form.setMandatory('service', false);
g_form.setMandatory('subcategory', false);
} else {
g_form.setMandatory('service', true);
g_form.setMandatory('subcategory', true);
}

return;
}
gId = '1234493d47ba6a507c23de79116d7890';
if (newValue ===gId) {
g_form.setMandatory('service', false);
g_form.setMandatory('subcategory', false);
} else {
g_form.setMandatory('service', true);
g_form.setMandatory('subcategory', true);
}
}

@Ankur Bawiskar 

11 REPLIES 11

Ankur Bawiskar
Tera Patron

@Sathwik1 

screenshots please and client script config screenshot as well

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

@Sathwik1 

what debugging did you do?

try this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    var gId = '1234493d47ba6a507c23de79116d7890';

    if (isLoading || newValue === '') {
        if (newValue === gId) {
            g_form.setMandatory('service', false);
            g_form.setMandatory('subcategory', false);
        } else {
            g_form.setMandatory('service', true);
            g_form.setMandatory('subcategory', true);
        }
        return;
    }

    if (newValue == gId) {
        g_form.setMandatory('service', false);
        g_form.setMandatory('subcategory', false);
    } else {
        g_form.setMandatory('service', true);
        g_form.setMandatory('subcategory', true);
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar  If we select "1234493d47ba6a507c23de79116d7890" account and save it and reload the same form in workspace.. then it shows mandatory.. and remaining scenarios it was fine.

same happened with my script and your script also.