document.getElementById : how to set value when ... - ServiceNow Community

document.getElementById : how to set value when there is no ID?

andy_dufresne
Tera Expert

I need to set some values from the console in this process I have used something similar on the same form and works great:

document.getElementById("s2id_sp_formfield_subcategory").value = "Car Hire";

The above was a text field.

I have a reference variable and I want to add value similar to above.  The problem is I cannot find the 'ID'.  What options do I have to populate in that text box from console as above?

 

find_real_file.png

17 REPLIES 17

Harsh Vardhan
Giga Patron
Giga Patron

document.getElementById() does not work on portal, why are you setting the value from console ? may i know the exact requirement ?

 

Note: DOM does not work when you are using client script, 

 

did you try with getElementsByClassName()

 

https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

Tanaji Patil
Tera Guru
Hi, It's not clear what and where you are trying to achieve. But as already mentioned by other member DOM is not recommended in ServiceNow. It won't work on portal as well as it might not work after upgrades if the elements are renamed. If you are trying to debug or set it on the backed catalog form (not via portal) then you can use CTLR +SHIFT + J with your admin account. That will give you a JavaScript editor where you can write your client script to manipulate values like g_form.setValue(). For reference field pass the sys_id of the reference record.

andy_dufresne
Tera Expert

Hi All,

Thank you for the response.

This is not a Development effort.  I'm not writing any scripts here.

I'm trying to help our testers on a one-time effort but needs to be tested with various combinations.  

Imagine a catalog variable on a portal is referencing the 'subcategory' table (also has a field called subcategory).  As a part of a larger exercise, our testers are expected to hand-pick few such records and test various scenarios.

It would be useful to either write a small javascript from console or use the 'Auto fill' plugin (which also uses javascript) to try those scenarios.

 

 

I have used in the past even on a portal (including the current catalog item but for text fields) and works like a charm.  Unfortunately I cannot see an 'ID' for the above element.

 

I tried getElementsByClassName etc., too but without success.

 

Hence if you know an alternative way to populate values in the field where 'ID' is not visible, that would be a big favor for me.

 

Thanks for reading!

why don't you try with ATF , just build your test cases and let them test it. 

 

Doc link for further details. 

 

https://docs.servicenow.com/bundle/newyork-it-service-management/page/product/service-catalog-manage...

 

Testing Service Catalog in Service Portal Using ATF