Searching a string and erasing a part of it

Community Alums
Not applicable

Hi,

I am trying to implement a fix/workaround to an existing issue in ServiceNow that they are not planning on fixing. We began commenting the Work Notes and Additional Comments into our close notes and occasionally get a "_123STREAMENTRY321_". I would like to be able to search a variable that we have called endNotes for that string and remove it. However, I can't even detect the string within it and am not sure what I am doing wrong. You can see the original below along with my first attempt (which had positive results, but did not fix all of them.) which tries to filter it out before it event hits endNotes, and my second attempt, which is, again, me trying to search the string for the value and just erase it, but I'm not getting that right.

(function executeRule(current, previous /*null when async*/) {
	
	// Add your code here
	var endNotes = "";
	var timeDate = gs.nowDateTime();
	var work = current.getValue('work_notes');
	var additional = current.getValue('comments');
	
	/*//Filter out _123STREAMENTRY321_ Attempt 1 - Reduced number of entries, but not all
	var resw = work.substring(0,18);
	var resa = additional.substring(0,18);
	if (resw == "_123STREAMENTRY321_") {
		work = work.substring(19,-1);
	}
	if (resa == "_123STREAMENTRY321_") {
		additional = additional.substring(19,-1);
	}*/
	
	//Filter out _123STREAMENTRY321_ Attempt 2
	regex = "\b(_123STREAMENTRY321_)\b/g";
	if (work != ''){
		endNotes = "Work Notes (" + timeDate + "):\n" + work + "\n\nAdditional Comments (" + timeDate + "):\n" + additional;
		if(endNotes.indexOf("_123STREAMENTRY321_") > -1){
			gs.addErrorMessage("We've detected it");
		}
		current.setValue('close_notes',endNotes);
	}
	else {
		endNotes = "Additional Comments (" + timeDate + "): \n" + additional;
		current.setValue('close_notes',endNotes);
	}
	
	/*// Original below
	if (work != ''){
		endNotes = "Work Notes (" + timeDate + "):\n" + work + "\n\nAdditional Comments (" + timeDate + "):\n" + additional;
		current.setValue('close_notes',endNotes);
	}
	else {
		endNotes = "Additional Comments (" + timeDate + "): \n" + additional;
		current.setValue('close_notes',endNotes);
	}*/
	
})(current, previous);
1 ACCEPTED SOLUTION

Stewe Lundin
Mega Guru

Hi this will remove the '_123STREAMENTRY321_', much easier than using a Reg exp. works bith in Client or Server side scripts. 



var myString = 'Filter out _123STREAMENTRY321_ Attempt 1 - Reduced number of entries, but not all';

var myNewString = myString.replace('_123STREAMENTRY321_ ', '');

gs.info(myNewString )

 

 

View solution in original post

8 REPLIES 8

Learn something
Help someone
Share something

This should replace all strings

var myString = 'Filter out _123STREAMENTRY321_ Attempt 1 - Reduced number of entries, but not allFilter out _123STREAMENTRY321_ Attempt 1 - Reduced number of entries, but not allFilter out _123STREAMENTRY321_ Attempt 1 - Reduced number of entries, but not all';

var myNewString = myString.replaceAll('_123STREAMENTRY321_ ', '');

gs.info(myNewString )

That was a neat solution. 

Community Alums
Not applicable

Thanks Vignesh, much cleaner.