The CreatorCon Call for Content is officially open! Get started here.

Replace string text multiple times

Carl Fransen1
Kilo Sage

Hi All,

I have a requirement where I need to replace the 'Approval' email to use the HR teams email and not the IT team email.  The out of the box approve message has a link to approve and reject via email.  These have a link 'Click here to Approve <number>' which opens a reply email with a default reply email, subject and includes the original watermark. 

I have created a business rule, before, insert, on sys_email table to check for text and replace, see below:

(function executeRule(current, previous /*null when async*/) {

	current.body = current.body.replace('mailto:itaddress@', 'mailto:hraddress@');
	
})(current, previous);

This works great, but only changes the first instance it finds, so the 'Approve' link, but it doesn't carry on to replace the 'Reject' link  also with the new email.

Anyone know how I can have this check for ALL matches of the text and replace?

Thanks

Carl.

1 ACCEPTED SOLUTION

Carl Fransen1
Kilo Sage

Hi All,

Found the answer, quite simple really, just needed to surround the original string with some slashes and a g to make it look globally, see below for updated script:

(function executeRule(current, previous /*null when async*/) {

	current.body = current.body.replace(/mailto:itaddress@/g, 'mailto:hraddress@');
	
})(current, previous);

 

This now works a treat!

 

Hope it helps someone else

 

Cheers

Carl.

View solution in original post

1 REPLY 1

Carl Fransen1
Kilo Sage

Hi All,

Found the answer, quite simple really, just needed to surround the original string with some slashes and a g to make it look globally, see below for updated script:

(function executeRule(current, previous /*null when async*/) {

	current.body = current.body.replace(/mailto:itaddress@/g, 'mailto:hraddress@');
	
})(current, previous);

 

This now works a treat!

 

Hope it helps someone else

 

Cheers

Carl.