How to make fields mandatory in client script once the other field is filled?

Community Alums
Not applicable

Hi All,

I had a requirement to make the fields on the incident form as mandatory based on other fields(multiple fields ) populated.

I have written a onload client script

 

function Onload {
    //Type appropriate comment here, and begin script below
    var a = g_form.getValue('assignment_group');
    var mjr = g_form.getValue('major_incident_state');
    var al = g_form.getValue('u_incident_started');
        var b = g_form.getValue('u_alert_reported');
        var c = g_form.getValue('u_on_call_notified');
        var d = g_form.getValue('u_required_group_engaged');
        var e = g_form.getValue('u_corrective_action_determined');
        var f = g_form.getValue('u_system_at_a_useable_level_incident_ended');
        var g = g_form.getValue('u_checkout_completed');
    if ((a == '83581c2f13459f4086a774285144btr2' || a == '43581c2f13459f4086a7742851444564') && (mjr == 'accepted')) {

 

       if (al != '' && b != '') 
            g_form.setMandatory("u_alerting_comment", true);

        if (b != '' && c != '') 
            g_form.setMandatory("u_awareness_comment", true);

if(c!='' && d!='')

g_form.setMandatory("u_escalate_comment", true);

    }

 

}

 

I want the fields mentioned in this above code as mandatory once the fields are filled, but the script is working after saving the form.

 

Is there any other way to do this once the fields is populated.

 

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

You would have to change this script from onLoad to onChange - of the last field, or one script onChange of every field so they can be populated out of order.  Try doing this instead with a UI Policy.

Community Alums
Not applicable

Hi @Brad Bowman  

Can we try creating 6 UI policies instead of client script I created 6 UI policies for 6 fields since each field is having difference scenario and it is working as expected now

 

I need to know is it a good way of creating 6 UI policies

@Community Alums,

Yes UI Policies would be a good way.

Yes - UI Policies are the best practice for making fields/variables mandatory, visible, and/or read-only.  It can get tricky when there are a lot and they overlap / affect each other, but it sounds like you are on the right path now.