- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours ago
Hi Community, Good Day!
I have a requirement on the Change Request form to add a custom button called “Generate PDF”. When this button is clicked, the system should generate and download a PDF of the Change Request form, include all related Change Tasks in the same PDF, and automatically attach the generated PDF to the Change Request record. The goal is to have a complete document containing the Change Request and its associated Change Tasks for sharing and record-keeping purposes.
Thanks in Advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
Hi @Kripa_23 , Please try following approach:
Create UI Action: (that you have already created)
Add the following code n script area:
try {
function dv(field) {
if (current[field] && current[field].getDisplayValue)
return current[field].getDisplayValue();
return "";
}
var pdfAPI = new sn_pdfgeneratorutils.PDFGenerationAPI();
var html = "";
// ===== HEADER =====
html += "<h2>Change Request Details</h2>";
html += "<p><b>Number:</b> " + current.number + "</p>";
html += "<p><b>Requested by:</b> " + dv("requested_by") + "</p>";
html += "<p><b>Category:</b> " + dv("category") + "</p>";
html += "<p><b>Service:</b> " + dv("business_service") + "</p>";
html += "<p><b>Service Offering:</b> " + dv("service_offering") + "</p>";
html += "<p><b>Configuration Item:</b> " + dv("cmdb_ci") + "</p>";
html += "<p><b>Priority:</b> " + dv("priority") + "</p>";
html += "<p><b>Risk:</b> " + dv("risk") + "</p>";
html += "<p><b>Impact:</b> " + dv("impact") + "</p>";
html += "<p><b>Short Description:</b> " + (current.short_description || "") + "</p>";
html += "<p><b>Description:</b> " + (current.description || "") + "</p>";
html += "<p><b>Model:</b> " + dv("model") + "</p>";
html += "<p><b>Type:</b> " + dv("type") + "</p>";
html += "<p><b>State:</b> " + dv("state") + "</p>";
html += "<p><b>Conflict Status:</b> " + dv("conflict_status") + "</p>";
html += "<p><b>Conflict Last Run:</b> " + (current.conflict_last_run || "") + "</p>";
html += "<p><b>Assignment Group:</b> " + dv("assignment_group") + "</p>";
html += "<p><b>Assigned To:</b> " + dv("assigned_to") + "</p>";
html += "<hr>";
html += "<h3>Change Tasks</h3>";
html += "<table border='1' width='100%' cellpadding='5'>";
html += "<tr>";
html += "<th>Number</th>";
html += "<th>Short Description</th>";
html += "<th>State</th>";
html += "<th>Assigned To</th>";
html += "</tr>";
var ct = new GlideRecord("change_task");
ct.addQuery("change_request", current.sys_id);
ct.query();
var found = false;
while (ct.next()) {
found = true;
html += "<tr>";
html += "<td>" + ct.number + "</td>";
html += "<td>" + (ct.short_description || "") + "</td>";
html += "<td>" + ct.state.getDisplayValue() + "</td>";
html += "<td>" + ct.assigned_to.getDisplayValue() + "</td>";
html += "</tr>";
}
if (!found) {
html += "<tr><td colspan='4'><b>No Change Tasks found</b></td></tr>";
}
html += "</table>";
var fileName = current.number + "_Change_Request.pdf";
var result = pdfAPI.convertToPDF(
html,
current.getTableName(),
current.getUniqueValue(),
fileName
);
if (result)
gs.addInfoMessage("PDF generated and attached successfully.");
else
gs.addErrorMessage("PDF generation failed.");
} catch (e) {
gs.addErrorMessage("PDF Error: " + e);
}
action.setRedirectURL(current);
After clicking on the button:
And this will be the preview:
(you can apply css according to your requirement)
If this answer helped you, please mark it as the accepted solution and give it a thumbs up.
Best regards,
Ibrar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
Hi @Kripa_23 , Please try following approach:
Create UI Action: (that you have already created)
Add the following code n script area:
try {
function dv(field) {
if (current[field] && current[field].getDisplayValue)
return current[field].getDisplayValue();
return "";
}
var pdfAPI = new sn_pdfgeneratorutils.PDFGenerationAPI();
var html = "";
// ===== HEADER =====
html += "<h2>Change Request Details</h2>";
html += "<p><b>Number:</b> " + current.number + "</p>";
html += "<p><b>Requested by:</b> " + dv("requested_by") + "</p>";
html += "<p><b>Category:</b> " + dv("category") + "</p>";
html += "<p><b>Service:</b> " + dv("business_service") + "</p>";
html += "<p><b>Service Offering:</b> " + dv("service_offering") + "</p>";
html += "<p><b>Configuration Item:</b> " + dv("cmdb_ci") + "</p>";
html += "<p><b>Priority:</b> " + dv("priority") + "</p>";
html += "<p><b>Risk:</b> " + dv("risk") + "</p>";
html += "<p><b>Impact:</b> " + dv("impact") + "</p>";
html += "<p><b>Short Description:</b> " + (current.short_description || "") + "</p>";
html += "<p><b>Description:</b> " + (current.description || "") + "</p>";
html += "<p><b>Model:</b> " + dv("model") + "</p>";
html += "<p><b>Type:</b> " + dv("type") + "</p>";
html += "<p><b>State:</b> " + dv("state") + "</p>";
html += "<p><b>Conflict Status:</b> " + dv("conflict_status") + "</p>";
html += "<p><b>Conflict Last Run:</b> " + (current.conflict_last_run || "") + "</p>";
html += "<p><b>Assignment Group:</b> " + dv("assignment_group") + "</p>";
html += "<p><b>Assigned To:</b> " + dv("assigned_to") + "</p>";
html += "<hr>";
html += "<h3>Change Tasks</h3>";
html += "<table border='1' width='100%' cellpadding='5'>";
html += "<tr>";
html += "<th>Number</th>";
html += "<th>Short Description</th>";
html += "<th>State</th>";
html += "<th>Assigned To</th>";
html += "</tr>";
var ct = new GlideRecord("change_task");
ct.addQuery("change_request", current.sys_id);
ct.query();
var found = false;
while (ct.next()) {
found = true;
html += "<tr>";
html += "<td>" + ct.number + "</td>";
html += "<td>" + (ct.short_description || "") + "</td>";
html += "<td>" + ct.state.getDisplayValue() + "</td>";
html += "<td>" + ct.assigned_to.getDisplayValue() + "</td>";
html += "</tr>";
}
if (!found) {
html += "<tr><td colspan='4'><b>No Change Tasks found</b></td></tr>";
}
html += "</table>";
var fileName = current.number + "_Change_Request.pdf";
var result = pdfAPI.convertToPDF(
html,
current.getTableName(),
current.getUniqueValue(),
fileName
);
if (result)
gs.addInfoMessage("PDF generated and attached successfully.");
else
gs.addErrorMessage("PDF generation failed.");
} catch (e) {
gs.addErrorMessage("PDF Error: " + e);
}
action.setRedirectURL(current);
After clicking on the button:
And this will be the preview:
(you can apply css according to your requirement)
If this answer helped you, please mark it as the accepted solution and give it a thumbs up.
Best regards,
Ibrar
