Replace string in email body after sending.

Steven Watts2
Tera Guru

Hi,

We have a requirement whereby after an email is sent a particular string in the body is replaced with the work 'Redacted'. For now i'm trying to get this working in a fix script where I am getting the email body, extracting the string and then replacing it with Redacted. I can see that the value has been updated when I print it but the email body is not getting updated.

 

 

var email = new GlideRecord("sys_email");
email.get('aa14f2938746a5108354646e8bbb3592');
var splitBody = email.getValue('body');
var findPassword = splitBody.split('Password is');
var two = findPassword[1];
var trim = two.substring(0,17);
gs.print(trim);
var replace = trim.replace(trim,'Redacted');
gs.print(replace);
email.update();

 

 

Output (the below is not a password, just a random string used for testing):

*** Script:   dOc04i'XU)18]uJ
*** Script: Redacted

Can anyone assist on how I take the value in replace and use this to overwrite the value in i've taken from email body?

 

Steven

2 ACCEPTED SOLUTIONS

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Steven Watts2 ,
I trust you are doing great.
You can update the email record with the new body value by setting it using the setValue() method before calling the update() method. Here's an updated version of the script that should accomplish your requirement:

 

var email = new GlideRecord("sys_email");
email.get('aa14f2938746a5108354646e8bbb3592');
var splitBody = email.getValue('body');
var findPassword = splitBody.split('Password is');
var two = findPassword[1];
var trim = two.substring(0,17);
gs.print(trim);
var replace = trim.replace(trim,'Redacted');
gs.print(replace);
email.setValue('body', splitBody.replace(trim, replace)); // set updated body value
email.update();

 

 

Please mark the answer correct if it was helpful.

Regards,

Amit Gujarathi


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

Ahmmed Ali
Mega Sage

try below script:

 

var email = new GlideRecord("sys_email");
email.get('aa14f2938746a5108354646e8bbb3592');
var splitBody = email.getValue('body');
var findPassword = splitBody.split('Password is');
var two = findPassword[1];
var trim = two.substring(0,17);
gs.print(trim);
var updatedBody = splitBody.replace(trim,'Redacted');
email.setValue("body",updatedBody);
//gs.print(replace);
email.update();
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

3 REPLIES 3

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Steven Watts2 ,
I trust you are doing great.
You can update the email record with the new body value by setting it using the setValue() method before calling the update() method. Here's an updated version of the script that should accomplish your requirement:

 

var email = new GlideRecord("sys_email");
email.get('aa14f2938746a5108354646e8bbb3592');
var splitBody = email.getValue('body');
var findPassword = splitBody.split('Password is');
var two = findPassword[1];
var trim = two.substring(0,17);
gs.print(trim);
var replace = trim.replace(trim,'Redacted');
gs.print(replace);
email.setValue('body', splitBody.replace(trim, replace)); // set updated body value
email.update();

 

 

Please mark the answer correct if it was helpful.

Regards,

Amit Gujarathi


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Ahmmed Ali
Mega Sage

try below script:

 

var email = new GlideRecord("sys_email");
email.get('aa14f2938746a5108354646e8bbb3592');
var splitBody = email.getValue('body');
var findPassword = splitBody.split('Password is');
var two = findPassword[1];
var trim = two.substring(0,17);
gs.print(trim);
var updatedBody = splitBody.replace(trim,'Redacted');
email.setValue("body",updatedBody);
//gs.print(replace);
email.update();
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Steven Watts2
Tera Guru

@Ahmmed Ali @Amit Gujarathi 

 

Thank you, can't believe I forgot to set the value lol. Both of those solutions worked :).

 

Thanks again.

 

Steven