Inserting javascript into a field via code without executing the code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 06:54 AM
I want to create a Catalog Item variable via code and in the "default_value" field, there should be the following code:
But when I create the Catalog Item variable by code in a UI Policy like this:
then the "default_value" does not contain
So is there a way to insert javascript code into the default_value field via code without the code being executed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 06:59 AM
Hello @Jonas27
Try this:
var variable = new GlideRecord('item_option_new');
variable.setValue('default_value', "javascript: 'gs.getUserID()'");
//and some other field values
variable.insert();
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 07:12 AM
Thanks for the proposed solution, but this
leads to only "gs.getUserID()" getting inserted into the default_value field, so without the javascript part at the beginning.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 07:18 AM
Hello @Jonas27
- You cannot directly insert JavaScript code into the default_value field of a Catalog Item variable without it being executed.
- ServiceNow automatically processes certain script prefixes, like javascript:, when the variable is loaded.
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:16 AM - edited 03-04-2025 08:17 AM
Hello @Jonas27
To achieve something like below, use a "Custom with Label" Type field with default value as javascript:gs.getUserID() or if you want to use a text field then wrap the default value in single or double quotes: 'javascript:gs.getUserID()'
Hope that helps!