Hi
I have problem with PDF attachment file name for posted sales invoices.
My invoices are in format: INV/YEAR/NUMER like INV/2022/001.
If I go to "Actions > Attach as PDF" my pdf name will be "001.pdf", instead format "ReportName DocumentType DocumentNo" like "123456 Invoice INV/2022/001". Problem is with characters '/'
in document no.
I tried to replace all '/' characters with '-' using ConvertStr(), but I couldn't find any good event to subscribe. Every event I found pass fileName or DocumentNo as normal parameter without prefix var, to edit it as reference.
Code is in table 77 "Report Selections" in procedure "SaveDocumentAttachmentFromRecRef".
I tried to use events:
1.table 77 "Report Selections" > "OnBeforeSaveDocumentAttachmentFromRecRef"
2. table 1173 "DocumentAttachment" > "OnBeforeSaveAttachment"
but all pass parameter without prefix 'var'.
Is there any solution to change PDF fileName in code?
It's a bit tricky as there are no publishers with FileName passing as reference (Var)
So, I have used SingleInstance Codeunit to store the FileName using OnBeforeSaveAttachment publisher
Got the FileName back after the insert
[EventSubscriber(ObjectType::Table, Database::"Document Attachment", 'OnBeforeSaveAttachment', '', false, false)]
local procedure OnBeforeSaveAttachment(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef; FileName: Text; var TempBlob: Codeunit "Temp Blob");
var
SingleInstaceCU: Codeunit SingleInstanceCU;
begin
SingleInstaceCU.SetFileName(FileName);
end;
[EventSubscriber(ObjectType::Table, Database::"Document Attachment", 'OnAfterInsertEvent', '', false, false)]
local procedure OnAfterInsertEventDocumentAttachment(var Rec: Record "Document Attachment"; RunTrigger: Boolean)
var
SingleInstaceCU: Codeunit SingleInstanceCU;
FileName: Text[250];
begin
if Rec."Table ID" = 36 then begin
FileName := SingleInstaceCU.GetFileName();
Rec.Validate("File Name", CopyStr(FileName, 1, StrLen(FileName) - 4));
if Rec.Modify() then;
end;
end;
codeunit 90104 SingleInstanceCU
{
SingleInstance = true;
var
gFileName: Text;
procedure SetFileName(ParFileName: Text)
begin
gFileName := ParFileName;
end;
procedure GetFileName(): Text
begin
exit(gFileName);
end;
}
yes, it works with this event, but i need to have file name changes before it is saved to BC.
What i am trying to achieve is when user tries to add an attachment, i want to save it to Azure blob storage and in case if there is an error on sending it to Azure storage, user gets an error and has to try adding an attachment again.
in file name i want to save the full path to Azure blob storage so taht is why i need it to be updated...
Hi, just adding some info.
Hope the following can give you some hints.
How to directly export Report from page (Custom Filename For Report Export)
Thanks.
ZHU
hi
try this event
obviously try to condition the change according to your document and your conditions
check my answer if it helped you, thanks
DANiele
Anything i change on the event OnBeforeInsertAttachment, does not get updated on record...
i change the name of the file and it still gets inserted with an old name..
Thank for your response Inge M. Bruvik. My code looks like this. Just like I wrote in previous comment, this solution doesn't work because DocumentAttachemnt."File Name" contains already cutted file name like "001" instead of "123456 Invoice 001".
All start in page 132 in action "AttachAsPDF".
codeunit 50149 AttachPDFFileNameValidation { [EventSubscriber(ObjectType::Table, Database::"Document Attachment", 'OnBeforeInsertAttachment', '', true, true)] local procedure OnBeforeInsertAttachment(var DocumentAttachment: Record "Document Attachment") begin DocumentAttachment."File Name" := ConvertStr(DocumentAttachment."File Name", '/', '-'); end; }
Maybe you can show us the complete code you have for the trigger you try to use.
It is often easier to help when we can see the whole picture through the compete code.
Thank you for response Amit Sharma.
This doesn't solve my problem, but it's helpful.
Thank you for response Nitin Verma. This solution won't work because at this moment DocumentAttachment."File Name" have already cutted file name.
I need to write this line:
DocumentAttachment."File Name" := ConvertStr(DocumentAttachment."File Name", '/', '-');
If line should have name like this: "123456 Invoice INV/2022/001" at this scope field DocumentAttachment."File Name" contains " only "001" value.
Hi,
I tried to replace all '/' characters with '-' using-->
for removing special character from string.
Regards
Amit Sharma
https://www.linkedin.com/in/amit-sharma-94542440/
Press Yes if info is useful.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,524 Super User 2024 Season 2
Martin Dráb 228,493 Most Valuable Professional
nmaenpaa 101,148