Copying a field content to another

rob_blakey
Tera Expert

Hi ServiceNow Community,

I am having some scripting issues attempting to copy the following:

I created a new field within the Change Management Module called "Change Description". The reason for this is that I wanted to have the change description field HTML but not impact the description field that sits across the task table.

What I would like to do is copy the text content from the Change Description field "u_change_description_html" into the standard "description" field.

I have attempted business rules and client scripts, but nothing I have tried seemed to help. I did see a few comments about this in other forums however, nothing seemed to work for me.

If anyone has any suggestions or has already created a script for this it would be greatly appreciated.

Regards,

Rob

1 ACCEPTED SOLUTION

salemsap
Tera Expert

use regular expression to remove the html tags like below,


copy the below code and paste it in the business rules.


var text = current.u_change_description_html.toString();


var regX = /(<([^>]+)>)/ig;


var finalText = text.replace(regX, " ");


current.description = finalText;



try and lemme know if any issues


View solution in original post

13 REPLIES 13

Gurpreet07
Mega Sage

if you need to copy the data into new field for already present records , run following code into background script.



var gr = new GlideRecord('change_request');


gr.query();


while(gr.next()){


gr.autoSysFields(false);


gr.u_change_description_html = gr.description ;                   // Please review for back end names


gr.update();


}




Now , for new inserts or updates you need to create a before update , insert business rule having   following code .



current.u_change_description_html = current.description ;


Hi Gurpreet,



This works thanks! however, in the string 'description' it now has a bunch of HTML tags? Is there a way to strip out these tags?



I have created the Business Rule as described:



function onBefore() {


current.description = current.u_change_description_html;


}


Make sure to call the same function in the same script .



Well if the description field includes HTML tags then you may or may not   be able to format the string. It depends on the structure of the tags . Is there same set of tags for all records ? if yes can u please share data from one of the description field.


Hi,



For example:


Change Description = "## This is a Test Change ##



Then Description:


<p></p>


<p style="text-align: center;"><span style="font-size: 24pt;">## This is a Test Change ##</span></p>


<p></p>



I would like it only to show "## This is a Test Change ##" and remove the HTML Tags