Workspace and DOM manipulation?

NagyDan1
Tera Expert

Hi, I need to run a small script on a UI Action in the Workspace:

 

This runs on the HR Case form. It takes the Description field into the "description" variable.

Then it tries to do this magic trick with the description variable:

 

 

var e = document.createElement('div');
e.innerHTML = description;
return e.childNodes[0].nodeValue;

 

 
Now this code fails as this is DOM manipulation and "document" is not available on Workspace.
 
What is a good way to substitute this small function with something that runs on Workspace? Or how can I enable DOM manipulation on HR Agent Workspace?
1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

Hi @NagyDan1 ,

 

In HR Agent Workspace, direct DOM manipulation is not supported due to security and performance reasons. However, you can achieve similar functionality using ServiceNow's client-side APIs and libraries. Here's an alternative approach to replace the DOM manipulation code:

 

-->> Use the gel() function to retrieve the value of the Description field. This function is available in the ServiceNow client script API and allows you to access field values on the form.

 

var description = gel('description').value;

 

-->>Instead of creating a new DOM element, you can directly manipulate the description variable using JavaScript string manipulation functions. For example, you can use the replace() function to modify the description string.

 

var modifiedDescription = description.replace('<div>', '');

 

-->> Return the modified description or use it in further processing as required.

 

return modifiedDescription;

 

 

Also, refer below articles:
https://snprotips.com/blog/2017/7/21/how-to-enable-dom-manipulation-in-the-servicenow-service-portal

 

https://www.servicenow.com/community/developer-articles/access-all-servicenow-inbuilt-methods-define...

 

Thanks,

Ratnakar