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.

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

Rafael Batistot
Kilo Patron

Hi @RAJPRAVEENR 

 

I can see some isues in you code:

 

  1. Case sensitivity – In ServiceNow, g_form.getValue() has a capital V, not getvalue().
  2. getEditableFields() – There is no native g_form.getEditableFields() API. That will return undefined. Instead, you need to loop through the fields you want to lock, or use g_form.getElements() if you want all elements.
  3. Best practice – Instead of looping through everything, it’s better to explicitly list the fields you want read-only.

     

 

Here’s a fixed version of your script

  1.  

function onLoad() {
var st = g_form.getValue('state'); // Capital V

if (st == 7) { 
var fieldsToLock = ['short_description', 'description', 'category', 'priority']; // add all fields you need

for (var i = 0; i < fieldsToLock.length; i++) {
g_form.setReadOnly(fieldsToLock[i], true);
}
}
}

 

Now… If you really want to lock all fields without listing them, you can do:

 

function onLoad() {
var st = g_form.getValue('state');
if (st == 7) {
var elements = g_form.getEditableFields(); // Only available in newer UI
if (elements) {
for (var i = 0; i < elements.length; i++) {
g_form.setReadOnly(elements[i], true);
}
}
}
}

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Ankur Bawiskar
Tera Patron
Tera Patron

@RAJPRAVEENR 

try this, I hope you are comparing correct choice value

function onLoad() {
    var st = g_form.getValue('state');
    if (st.toString() == '7') {
        var fields = g_form.getEditableFields();
        for (var x = 0; x < fields.length; x++) {
            g_form.setReadOnly(fields[x], true);
        }
    }
}

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

Nitish Saini
Giga Guru

Hi @RAJPRAVEENR 

 

I see the issue 👀. The problem is small but important — in ServiceNow client scripts, methods are case-sensitive.

In your script:

 

var st = g_form.getvalue('state'); // wrong

It should be:

 

var st = g_form.getValue('state'); // correct

 

 

Here’s a working corrected version of your script:

 

function onLoad() {
    var st = g_form.getValue('state');
    if (st == 7)   // assuming 7 = Closed  
{
        var fields = g_form.getEditableFields();
        for (var x = 0; x < fields.length; x++) {
            g_form.setReadOnly(fields[x], true);
        }
    }
}

Please accept my answer and mark as helpful/correct if it resolves your query.

Regards, 
Nitish Saini



 

Chaitanya ILCR
Mega Patron

Hi @RAJPRAVEENR ,

try this

function onLoad() {
    if (g_form.getvalue('state') == '7')
        g_form.disable();

}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya