cLient script not working here i wrote script to make readonly when ticket moved to closed state.

RAJPRAVEENR
Giga Contributor
function onLoad() {
    var st = g_form.getvalue('state');
    if (st == 7) {

        var fields = g_form.getEditableFields();
        for (var x = 0; x < fields.length; x++) {
            g_form.setReadOnly(fields[x], true);
        }
    }
10 REPLIES 10

pranita-24
Giga Guru

Hi @RAJPRAVEENR 

Yes, this can be achieved with an onLoad Client Script. In your script, there are two issues:

getValue() should be used (case-sensitive).

g_form.getEditableFields() is not a supported API in client scripts.

Try this script
function onLoad() {
var st = g_form.getValue('state');
if (st == 7) { // 7 = Closed (for Incident table)
var elements = g_form.getFormElement().elements;
for (var i = 0; i < elements.length; i++) {
var fieldName = elements[i].getAttribute('name');
if (fieldName) {
g_form.setReadOnly(fieldName, true);
}
}
}
}
If you only want to make specific fields read-only instead of all, you can simplify it like this:

function onLoad() {
if (g_form.getValue('state') == 7) {
g_form.setReadOnly('short_description', true);
g_form.setReadOnly('description', true);
g_form.setReadOnly('comments', true);
}
}

This way, once the ticket moves to Closed state, the form fields become read-only for the end user.

If my answer helped you, please mark it - Solution accepted. 

Thanks,
Pranita D