Hi, Readers.
Today I would like to talk about how to change attachment name in Document Attachment Details (1173, List). I saw this question on the D365 forum last week. More details: Business Central forum – How to attached a document with custom name
As you might know, on most list pages, cards, and documents, you can attach files on the Attachments tab of the FactBox pane. The number in the tab title indicates how many attached files exist for the card or document.
But here’s a little problem, once we upload an attachment, we can’t change the name of the attachment. This could be solved if we changed the name in advance and then uploaded it, but that’s obviously not very convenient.
So can we change the attachment name? Yes, but it requires a little customization. Actually in table 1173 “Document Attachment”, there is field(5; “File Name”; Text[250]):
Let me do a simple example.
1. Add a new action in Document Attachment Details (1173, List) to change the file name.
2. Create a new StandardDialog page to display some information about the currently selected record and allow the user to enter a new file name.
Enter a new file name and click OK. The new name will be saved.
It will be downloaded with a new name.
Test video:
Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)
page 50110 "Rename Dialog"
{
PageType = StandardDialog;
layout
{
area(Content)
{
group(GroupName)
{
field(AttachmentTableID; AttachmentTableID)
{
ApplicationArea = All;
Caption = 'Table ID';
Editable = false;
}
field(AttachmentNo; AttachmentNo)
{
ApplicationArea = All;
Caption = 'No.';
Editable = false;
}
field(FileName; FileName)
{
ApplicationArea = All;
Caption = 'File Name';
Editable = false;
}
field(ChangeFileName; ChangeFileName)
{
ApplicationArea = All;
Caption = 'New File Name';
}
}
}
}
var
FileName: Text[250];
ChangeFileName: Text[250];
AttachmentTableID: Integer;
AttachmentNo: Code[20];
AttachmentDocumentType: Enum "Attachment Document Type";
AttachmentLineNo: Integer;
AttachmentID: Integer;
procedure AssignValues(NewFileName: Text[250]; NewAttachmentTableID: Integer; NewAttachmentNo: Code[20]; NewAttachmentDocumentType: Enum "Attachment Document Type"; NewAttachmentLineNo: Integer; NewAttachmentID: Integer)
begin
InitialValues();
FileName := NewFileName;
AttachmentTableID := NewAttachmentTableID;
AttachmentNo := NewAttachmentNo;
AttachmentDocumentType := NewAttachmentDocumentType;
AttachmentLineNo := NewAttachmentLineNo;
AttachmentID := NewAttachmentID;
end;
procedure ChangeAttachmentFileName()
var
DocumentAttachment: Record "Document Attachment";
begin
if DocumentAttachment.Get(AttachmentTableID, AttachmentNo, AttachmentDocumentType, AttachmentLineNo, AttachmentID) then begin
DocumentAttachment.Validate("File Name", ChangeFileName);
DocumentAttachment.Modify(true);
end;
end;
local procedure InitialValues()
begin
FileName := '';
ChangeFileName := '';
AttachmentTableID := 0;
AttachmentNo := '';
AttachmentDocumentType := AttachmentDocumentType::Quote;
AttachmentLineNo := 0;
AttachmentID := 0;
end;
}
pageextension 50110 DocumentAttachmentDetailsExt extends "Document Attachment Details"
{
actions
{
addafter(Preview)
{
action(ChangeFileName)
{
Caption = 'Change File Name';
ApplicationArea = All;
Promoted = true;
PromotedCategory = Process;
Image = Change;
trigger OnAction()
var
RenameDialog: Page "Rename Dialog";
begin
RenameDialog.AssignValues(Rec."File Name", Rec."Table ID", Rec."No.", Rec."Document Type", Rec."Line No.", Rec.ID);
if RenameDialog.RunModal() = Action::OK then
RenameDialog.ChangeAttachmentFileName();
end;
}
}
}
}
Let’s discuss this more in depth, is it possible to change the default name? Yes, we can use OnBeforeSaveAttachment event in table 1173 “Document Attachment” which contains the ‘var FileName: Text‘ variable.
For example, automatically add today’s date after the file name.
PS: How to quickly get the extension name, file name without extension, and Media (MIME) types from an uploaded file
Test video:
Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)
codeunit 50110 AttachmentHandle
{
[EventSubscriber(ObjectType::Table, Database::"Document Attachment", OnBeforeSaveAttachment, '', false, false)]
local procedure OnBeforeSaveAttachment(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef; var FileName: Text; var TempBlob: Codeunit "Temp Blob");
var
ExtensionName: Text[250];
FileNameWithoutExtension: Text[30];
FileManagement: Codeunit "File Management";
begin
if FileName <> '' then begin
FileNameWithoutExtension := '';
ExtensionName := '';
FileNameWithoutExtension := FileManagement.GetFileNameWithoutExtension(FileName);
ExtensionName := FileManagement.GetExtension(FileName);
FileNameWithoutExtension := FileNameWithoutExtension + '_' + Format(Today, 0, '<Day,2>-<Month,2>-<Year>');
FileName := FileManagement.CreateFileNameWithExtension(FileNameWithoutExtension, ExtensionName);
end;
end;
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント