Skip to main content

Notifications

Business Central forum
Suggested answer

Edit PDF file name for posted sales invoice attachments

Posted on by 65

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?

Categories:
  • Suggested answer
    Mohana Yadav Profile Picture
    Mohana Yadav 58,874 Super User 2024 Season 2 on at
    RE: Edit PDF file name for posted sales invoice attachments

    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;

    }

    pastedimage1672833190354v1.png

  • Fisher A Profile Picture
    Fisher A 25 on at
    RE: Edit PDF file name for posted sales invoice attachments

    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...

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 69,639 Super User 2024 Season 2 on at
    RE: Edit PDF file name for posted sales invoice attachments

    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)

    https://yzhums.com/21202/

    Thanks.

    ZHU

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,217 Moderator on at
    RE: Edit PDF file name for posted sales invoice attachments

    hi

    try this event

    [EventSubscriber(ObjectType::table, 1173, 'OnAfterInsertEvent', '', false, false)]
        local procedure ChangeName(var Rec: Record "Document Attachment")
        begin
            if rec."Table ID" = 112 then begin  // this is my condition
                rec."File Name" := 'NewName'; //-> in your case should be: rec. "File Name" := ConvertStr(rec.'File Name','/','-') ;
                rec.Modify()
            end;
        end;

    obviously try to condition the change according to your document and your conditions

    pastedimage1672770564539v1.png

    check my answer if it helped you, thanks

    DANiele

  • Fisher A Profile Picture
    Fisher A 25 on at
    RE: Edit PDF file name for posted sales invoice attachments

    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..

  • Alzack18 Profile Picture
    Alzack18 65 on at
    RE: Edit PDF file name for posted sales invoice attachments

    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;
    
    }

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Edit PDF file name for posted sales invoice attachments

    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.

  • Alzack18 Profile Picture
    Alzack18 65 on at
    RE: Edit PDF file name for posted sales invoice attachments

    Thank you for response Amit Sharma.

    This doesn't solve my problem, but it's helpful.

  • Alzack18 Profile Picture
    Alzack18 65 on at
    RE: Edit PDF file name for posted sales invoice attachments

    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.

  • Suggested answer
    Amit Baru Profile Picture
    Amit Baru 3,025 on at
    RE: Edit PDF file name for posted sales invoice attachments

    Hi,

    I tried to replace all '/' characters with '-' using--> 

    DELCHR Function (Code, Text) function.

    for removing special character from string.

    Regards

    Amit Sharma

    www.erpconsultors.com

    https://www.linkedin.com/in/amit-sharma-94542440/

    Press Yes if info is useful.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans