Document Template - generate PDF with Subject Person name in the title
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2024 09:02 AM
Hi all,
Is there any way to generate a PDF document with "Subject Person" name in the title? We are using "Document Templates" plugin.
Thank you in advance,
Victoria
- Labels:
-
Human Resources Service Delivery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2024 10:58 AM
Hi @vstefanova ,
I faced similar issue. We can achieve this using customization.
Please create Before/insert BR in 'sys_attachment' table with global application scope.
(function executeRule(current, previous /*null when async*/ ) {
var case = new GlideRecord("sn_hr_core_case_global_mobility"); // put table name as per you configuration
case.addEncodedQuery('sys_id=' + current.table_sys_id);
case.query();
if (case.next()) {
var EmpName = case.subject_person.name;
}
current.file_name = 'Employee offer letter-' + EmpName;
// gs.info('EmpNameFilename' + current.file_name);
})(current, previous);
May be RCA record will be created post that please allowed RCA with target scope.
Please helpful & mark answer if it's really worthy for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Personally using Business Rules to rename attachments would be a messy and hard-to-maintain solution, especially across multiple case tables. It would require creating and managing separate rules just to rename files, which is not scalable. Instead, FINDING AND UPDATING the existing document generation logic is a cleaner and more maintainable approach. I traced the script that's causing the "logged in" user to appear in the generated PDF document:
UI Page: pdf_preview_document_dialog
↓
Button: Generate
↓
Script Include: HRDocumentTemplateAjax.generateDocumentForPdfTemplate(caseTable, caseId)
↓
Script Include: HRDocumentTemplateAjaxSNC.generateDocumentForPdfTemplate(caseTable, caseId)
↓
Script Include: HRDocumentTemplateUtils().generateDocumentForPdf(caseGr, caseId)
↓
Script Include: HRDocumentTemplateUtilsSNC().generateDocumentForPdf(caseGr, caseId)
↓
Function Call: HRDocumentTemplateUtilsSNC().getPdfName(documentTemplateId)
↓
THE CULPRIT IS THIS:
var documentTemplateId = caseGr.getValue('document_template');
getPdfName(documentTemplateId) {
var pdfName = docTemplateGr.name + "-" + <strong>gs.getUserDisplayName();</strong>
return pdfName;
}
To solve this, you need to edit the Script: HRDocumentTemplateUtils. You can't edit the HRDocumentTemplateUtilsSNC as it's a protected file.
You need to create a new method inside HRDocumentTemplateUtils to override what's in HRDocumentTemplateUtilsSNC. Full tested solution:
var HRDocumentTemplateUtils = Class.create();
HRDocumentTemplateUtils.prototype = Object.extendsObject(HRDocumentTemplateUtilsSNC, {
initialize: function() {
HRDocumentTemplateUtilsSNC.prototype.initialize.apply(this, arguments);
},
/*
Override the existing HRDocumentTemplateUtils.getPdfName(documentTemplateId) with the below. You need to add caseGr
*/
getPdfName: function (documentTemplateId, caseGr) {
var base = "";
var docTemplateGr = new GlideRecord("sn_doc_template");
if (docTemplateGr.get(documentTemplateId)) {
base = docTemplateGr.getValue("name");
}
var subjectPersonName = "";
try {
if (caseGr && caseGr.isValidRecord()) {
subjectPersonName = caseGr.getDisplayValue("subject_person");
}
} catch (ex) {
gs.error("HRDocumentTemplateUtils.getPdfName subject_person error: " + ex);
}
if (!gs.nil(subjectPersonName)) {
return base + "-" + subjectPersonName;
}
return base + "-" + gs.getUserDisplayName();
},
/*
You need to override too generateDocumentForPdf and generateDocumentForHtml as you need to change the way you call getPdfName.
*/
generateDocumentForPdf: function (caseGr, caseId) {
gs.info("Custom generateDocumentForPdf called from HRDocumentTemplateUtils");
// Delete already generated attachment
this._deleteGeneratedDocument(caseGr);
var documentTemplateId = caseGr.getValue('document_template');
var pdfName = this.getPdfName(documentTemplateId, caseGr);
var generatedDocumentId = new sn_doc.GenerateDocumentAPI().generateDocumentForTask(caseId, documentTemplateId, pdfName);
this._updateGeneratedDocumentFieldForCase(caseGr, generatedDocumentId);
return generatedDocumentId;
},
generateDocumentForHtml: function(htmlBody, caseGr) {
//Delete already generated document if exists.
this._deleteGeneratedDocument(caseGr);
var documentTemplateId = caseGr.getValue('document_template');
//Generate new document.
var pdfName = this.getPdfName(documentTemplateId, caseGr);
var htmlTemplateGr = new GlideRecord('sn_doc_html_template');
htmlTemplateGr.get(documentTemplateId);
var params = {};
params.htmlBody = htmlBody;
params.targetTable = caseGr.sys_class_name;
params.targetSysId = caseGr.sys_id;
params.generatedPdfName = pdfName;
params.htmlTemplateGr = htmlTemplateGr;
var generatedDocumentId = new sn_doc.HtmlTemplateUtils().generateDocumentAsAttachment(params);
// Update generated document field
this._updateGeneratedDocumentFieldForCase(caseGr, generatedDocumentId);
return generatedDocumentId;
},
type: 'HRDocumentTemplateUtils'
});
I hope this helps.
ArleneH