How to make a mandatory field as READONLY after it is filled and saved

Nisha30
Kilo Sage

Hi ,

I have a UI policy in sc_task for 

conditions : state is one of closed complete/incomplete

UI Action: Time worked = mandatory

 

now once this is filled and saved it should be readonly . But if I up[date the same UI policy it is not allowing me to fill the field .

How can I make this read only once already filled in.

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Nisha30 

add extra condition here and ensure UI policy doesn't run on form load

conditions : state is one of closed complete/incomplete AND Time Worked is empty

OR

use onchange client script on State field, UI Type - ALL

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

    // Check if the state is one of 'Closed Complete' or 'Closed Incomplete'
    var state = g_form.getValue('state');
    if (state == 'Closed Complete' || state == 'Closed Incomplete') {
        g_form.setMandatory('time_worked', true);

        // Check if 'Time worked' has a value and make it read-only if it does
        var timeWorked = g_form.getValue('time_worked');
        if (timeWorked != '') {
            g_form.setReadOnly('time_worked', true);
        } else {
            g_form.setReadOnly('time_worked', false);
        }
    } else {
        g_form.setMandatory('time_worked', false);
        g_form.setReadOnly('time_worked', false);
    }
}

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Nisha30 

add extra condition here and ensure UI policy doesn't run on form load

conditions : state is one of closed complete/incomplete AND Time Worked is empty

OR

use onchange client script on State field, UI Type - ALL

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

    // Check if the state is one of 'Closed Complete' or 'Closed Incomplete'
    var state = g_form.getValue('state');
    if (state == 'Closed Complete' || state == 'Closed Incomplete') {
        g_form.setMandatory('time_worked', true);

        // Check if 'Time worked' has a value and make it read-only if it does
        var timeWorked = g_form.getValue('time_worked');
        if (timeWorked != '') {
            g_form.setReadOnly('time_worked', true);
        } else {
            g_form.setReadOnly('time_worked', false);
        }
    } else {
        g_form.setMandatory('time_worked', false);
        g_form.setReadOnly('time_worked', false);
    }
}

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

Thanks @Ankur Bawiskar that was really silly yep the extra condition worked out perfectly as well as CS.

Ankur Bawiskar
Tera Patron
Tera Patron

@Nisha30 

are you making the field mandatory via UI policy?

If yes then simply add this condition

conditions : state is one of closed complete/incomplete AND Time Worked is empty

Also create another onLoad client script which checks if state is Closed Complete or Closed Incomplete and if Time worked is filled, then make it readonly

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

@Nisha30 

Thank you for marking my response as helpful.

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