.replace() function not working properly.

Hrishabh Kumar
Giga Guru

I am trying to update an HTLM by using .replace() function, which is working for certain text and not working for certain text.

 

I have shared the code and html below.

 

HTML:

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.certificate {
border: 2px solid #000;
padding: 20px;
max-width: 600px;
margin: 0 auto;
}
.certificate-title {
text-align: center;
font-size: 24px;
margin-bottom: 20px;
}
.certificate-content {
text-align: center;
font-size: 18px;
}
.certificate-name {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.certificate-date {
font-size: 18px;
}
</style>
<div class="certificate">
<div class="certificate-title">Certificate of Achievement</div>
<div class="certificate-content">This certificate is awarded to:
<div class="certificate-name">[Recipient's Name]</div>
for their outstanding performance in [Course/Event Name].
<div class="certificate-date">Date: [Date]</div>
</div>
</div>

 

Business Rule :

commented line 3 ie..var newHtml = html.replace("Certificate of Achievement", "Sachin Tendulker"); is working properly and html is getting updated, and I can see the changed on the generated PDF.

But same function is not working for line 4
ie.. var newHtml = html.replace("[Recipient's Name]""Sachin Tendulker");
 
I need to update "[Recipient's Name]" text in HTML and it is not getting updated.

 

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

	// Add your code here
	gs.addInfoMessage('certificate will be provided soon 7');
	var html = current.u_certificate;
	// var newHtml = html.replace("Certificate of Achievement", "Sachin Tendulker");
	var newHtml = html.replace("[Recipient's Name]", "Sachin Tendulker");
	var table = 'sys_user';
	var user_sys = current.getValue('u_user');
	var filename = 'Ecertificate'+current.u_user.name;

	var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
	var result = v.convertToPDF(newHtml, table, user_sys, filename);

})(current, previous);

 

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage
Tera Sage

@Hrishabh Kumar 

 

I think it is getting confused with "Recipient's Name"

 

Can you update it as "Recipients Name" and try below code

 

var newHtml = html.replace("[Recipients Name]", "Sachin Tendulker");

 

 

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

View solution in original post

5 REPLIES 5

Prince Arora
Tera Sage
Tera Sage

@Hrishabh Kumar 

 

I think it is getting confused with "Recipient's Name"

 

Can you update it as "Recipients Name" and try below code

 

var newHtml = html.replace("[Recipients Name]", "Sachin Tendulker");

 

 

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

SANDEEP28
Mega Sage

@Hrishabh Kumar You need to regular expression here. Try below code

 

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

	// Add your code here
	gs.addInfoMessage('certificate will be provided soon 7');
	var html = current.u_certificate;
	// var newHtml = html.replace("Certificate of Achievement", "Sachin Tendulker");
   var regExp = /\[Recipient's Name\]/;
	var newHtml = html.replace(regExp , "Sachin Tendulker");
	var table = 'sys_user';
	var user_sys = current.getValue('u_user');
	var filename = 'Ecertificate'+current.u_user.name;

	var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
	var result = v.convertToPDF(newHtml, table, user_sys, filename);

})(current, previous);

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

@SANDEEP28 , tried but it is not working.

@Hrishabh Kumar It should work. I stored your html content in description field of one incident and updated using below script & it worked.

 

With Regular expression, you can match any text.

 

var grIncident = new GlideRecord('incident');
if (grIncident.get('a83820b58f723300e7e16c7827bdeed2'))
{
var html = grIncident.description
var regExp = /\[Recipient's Name\]/;
var newHtml = html.replace(regExp , "Sachin Tendulker");
gs.info(newHtml);
}

 

 

SANDEEP28_1-1690443591016.png