Fuji client scripts: document object is null

aklimenko
Mega Expert

Hi

We use document.getElementById() or gel() in client scripts to get controls. In some places we cannot replace it with g_form. For example, I need to get a catalog item sys_id in variable set client script so the only way I could do that was document.getElementById("sysparm_id").value

What I found out is that for any client script document object is null:

[Error] TypeError: null is not an object (evaluating 'document.getElementById')

Any ideas?

42 REPLIES 42

Greg,



Have you tried using the GlideRecord "getters and setters" for this rather than direct DOM manipulation? You should be able to use those without problem in Fuji. My example is a bit different in that I'm trying to set style.display to none for a button/object not supported by GlideRecord getters and setters.



Have a look at this wiki page: Make Fields Read-Only Script - ServiceNow Wiki



I have code in Fuji working fine where I put server-side logic in a business rule and then set g_scratchpad (these values only need to be set read only or not on initial load); a UI Policy client script implements the change with g_form.setReadOnly().



function onCondition() {


  if (g_scratchpad.applicationReadOnly == true){


  g_form.setReadOnly('cmdb_ci', true);


  g_form.setReadOnly('u_no_application', true);


  }


  else if (g_scratchpad.applicationReadOnly == false){


  g_form.setReadOnly('cmdb_ci', false);


  g_form.setReadOnly('u_no_application', false);


  }


}


Ben,


Thanks.   What does work are my two "g_form" assignments. What doesn't work is when I try to reference "view" as in "if view.startswith" .   What I am trying to do is only do the protecting of those two fields when the form displayed is "ESS".   Is there any way within the UI Policy script to tell if the form is an "Ess" form that you know of.  


Thanks,


Greg


Greg,



You're right, in that case you're needing to do exactly what is in the wiki I referenced with document.getElementById() to get 'sysparm_view'...



I'm very interested to hear if SN has an alternative to gel() and document.getElementById(); perhaps additional g_form methods to access the DOM Object coming, or ways of using the existing methods? The getUniqueValue() is a good start, but often more than sys_id is needed.


Ben,


That worked.   If we go to Fuji I'll have to find out what the alternative is to the getElementById() is.   Thanks for the help.


Any solution for the getElementByID? I am attemping to hide some icons next to fields in Fuji based on role.