Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Why is the cmdb_ci field still editable when it should not be?

lonesoac01
Giga Guru

I am trying to troubleshoot why the cmdb_ci field is still editable  after I apply "Run If False" code in a UI Policy.

 

Here is my Run if True code:

function onCondition() {
    if (g_user.hasRole('usb_unauth_change_manager') || g_user.hasRole('change_manager')) {
        g_form.setReadOnly('cmdb_ci', false);
        g_form.setReadOnly('short_description', false);
		//alert("Andrew Wuz Here 1");
    } else {
        g_form.setReadOnly('cmdb_ci', true);
        g_form.setReadOnly('short_description', true);
		//alert("Andrew Wuz Here 2");
    }
}

Here is my run if false code:

function onCondition() {
    if (g_user.hasRole('usb_unauth_change_manager') || g_user.hasRole('change_manager')) {
        g_form.setReadOnly('cmdb_ci', true);
        g_form.setReadOnly('short_description', true);
		alert("Andrew Wuz Here 3");
    } else {
        g_form.setReadOnly('cmdb_ci', false);
        g_form.setReadOnly('short_description', false);
		//alert("Andrew Wuz Here 4");
    }
}

 

The alert dialog for "Andrew Wuz Here 3" appears fine.  What is overriding my readonly lines?

5 REPLIES 5

Brad Bowman
Mega Patron

I'll infer from the last word of your post, not specified earlier, that short description is also editable? When this script runs are either or both fields populated?  Is either field mandatory? It would also be extremely helpful to know on which form/table is this UI Policy.  What is the condition for this policy? When all 4 alerts are uncommented, do you only receive the correct one in each test case?  You likely have a different UI Policy, Data Policy, or Client Script on the same table affecting these fields.

Ankur Bawiskar
Tera Patron

@lonesoac01 

may be some other UI policy of higher order?

did you debug that?

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

lonesoac01
Giga Guru

I ended up adjusting the code a little bit.  Here is the new If False code:

function onCondition() {
    if (g_user.hasRole('usb_unauth_change_manager') || g_user.hasRole('change_manager')) {
        g_form.setReadOnly('cmdb_ci', true);
        g_form.setReadOnly('short_description', true);
		jslog('USB Make CI Field ReadOnly for Unauth Change Manager');
    } else {
        g_form.setReadOnly('cmdb_ci', false);
        g_form.setReadOnly('short_description', false);
		//alert("Andrew Wuz Here 4");
    }
}

I turned on the unholy feature of "Debug whole session" and got the output from the console.  I have attached the logs to this response.  I have reviewed the logs, searched for: "

USB Make CI Field ReadOnly for Unauth Change Manager" in the file/logs

and according to the log, my UI Policy is the last one executed.  The last I remember, the last Client Side Script that runs on a field "wins", right?

 

Edit:  Oh! I almost forgot.  I click on the little bug icons next to the cmdb_ci and short_description fields and I see nothing but green from the ACLs.  So, ACLs are not the issue.  Something is happening client side.

I ended up creating a new UI Policy where it is the very last UI Policy to be executed with no filtering criteria at all.  All it does is just sets the cmdb_ci field to readonly all the time.  The field is still EDITABLE!  How can this be?!

 

lonesoac01_0-1775833362404.png