How to automatically rename attachment? Should show opened for, not assigned to.

Leo25
Kilo Contributor

We have a requirement for an employment verification letter record producer, where when the attachment is created, it's name should include whoever opened the case, not who signed the document. Screenshot below, instead of the attachment displaying "Personal Travel- Leo Singh" it should display as "Personal Travel- Ali Sufiyan". Is there an OOTB way to accomplish this?

1 ACCEPTED SOLUTION

Sufiyan Memon
Kilo Sage
Kilo Sage

Hi leo,

I have work around it and develop same scenario in my PDI, I success to achieve it. 

1. I created Business rule in Sys_attachment table. It will run always.

find_real_file.png

 

 

2. Then write a following code in Advanced tab.
Note change the Sys_id of Hr_Service as per your HR_Service and also update table name as per your required table.

//Get Case information to make sure that update only for your HR Case.

var getCase = new GlideRecord('sn_hr_core_case_workforce_admin');
getCase.get(current.table_sys_id);
getCase.query();
if((getCase.hr_service == 'c741adf51b8a8890db9afd115b4bcb11') && (current.table_name == 'sn_hr_core_case_workforce_admin')){

//Once we make sure the attachment is in our case, we will update the attachment

var fileName = getCase.pdf_template.name +"-" + getCase.opened_for.getDisplayValue();
current.file_name = fileName;
current.update();
}

Please mark as correct if it resolve your issue or let me know if further explanation needed.

Thanks

 

View solution in original post

7 REPLIES 7

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Leo, sorry for the late reply but I wanted to provide where this value is getting set as it is the best place to adjust versus the additional overhead of a before insert/update business rule on sys_attachment.  I would also suggest you submit this as an Idea via Community Idea portal to make this configurable versus having to modify script to adjust.

There is an HR Script Include named "hr_PdfUtils" that is utilized to create the PDF's attached to the case.  Specifically the createPdfForDocument() function on lines 734-745 sets the name of the PDF based on the PDF's name and adds the logged in user's ID.  I adjusted the script to get the HR Case's subject person and set the attachment name instead.  If for some reason this code was being executed against a record where subject_person wasn't a valid attribute, it will default to the logged in user's name.

To fix this in your instance:

  • Change your scope to Human Resources: Core
  • Navigate to System Definition \ Script Includes and search where name = hr_PdfUtils and open the record
  • Scroll down to line 734 and replace lines 734-745 with the following:
createPdfForDocument : function(tableName, tableSysId, generateDocument){
	var pdfDraft = this._getPdfDraft(tableName, tableSysId);
	var pdfTemplate = this._getPdfTemplate(tableName, tableSysId);

	var pdfName = pdfTemplate.name;
	
	/* --------- Modified OOB code to set attachment to include HR Case's Subject Person ---------
	var user = new GlideRecord('sys_user');
	if (user.get(gs.getUserID())) {
		pdfName = pdfName + "-" + user.getDisplayValue();
	}*/
	
	var tableRec = new GlideRecord(tableName);
	if (tableRec.get(tableSysId)) {
		if (tableRec.isValidField("subject_person")) {
			pdfName = pdfName + "-" + tableRec.subject_person.getDisplayValue();
		} else {
			pdfName = pdfName + "-" + gs.getUserDisplayName();
		}
	}

	var response = this.mergeSignatureImageToPdf(tableName, tableSysId, pdfDraft.getUniqueValue(), pdfName, generateDocument);
	return response;
},
  • Click Update
  • As a result of modifying this script a Restricted Caller Access Privilege record will be invalidated and you will need to re-approve it.
  • Navigate to System Applications \ Application Restricted Caller Access Privileges and search where Status = Invalidated and you should see the following record:

find_real_file.png

  • Change the Status to Allowed

find_real_file.png

  • Both of these adjustments will be logged to your current update set so make sure that both are included in what you promote forward
  • Please note that future upgrades will skip the hr_PdfUtils Script Include since you modified it so make sure that you compare it and merge any enhancements to this script after upgrades.

 

Please mark this post helpful or the correct answer to your question if applicable so others viewing may benefit.

Thanks, Michael!  I glad I found this as I had a similar issue to resolve.  I try to avoid modifying OOB scripts, but I'm going to make an exception in this case.  I'm also taking your advise and submitting an Idea via the portal.

Good stuff!

Community Alums
Not applicable

Hello @Michael Ritchie is the script include and fix also for the new Advanced Forms...Document templates for HTML documents? I tried the script include and wasn't working for the HTML Document template. Thank you for any help as I know it has been some time on this post.