SavesAS Function & File Management in Business Central On-Prem (ver16)

xhirvanaxhirvana Member Posts: 5
edited 2022-07-29 in NAV Three Tier
I am currently working on a Packing List report that needs to save as pdf automatically in a particular path in our server upon click of an action button in a Sales Order. I am new to development, I have tried following the saveaspdf method in the Microsoft website however when the action button was ticked now, an I/O Error shows even when I already deleted the codes I have done in the Visual Studio Code. Is there any sample code you can advise to achieve the first scenario I have mentioned above? Another twist to this is that, it should only save when the sell-to customer code is equal to Customer 1, Customer 2 & Customer 3...help me please! Will File Management Code Unit help me with this? Thank you in advance!

Answers

  • krikikriki Member, Moderator Posts: 9,112
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ResolusResolus Member Posts: 40
    Not sure if it is still needed, but below should give some inspiration.
    File Management codeunit is the one you need indeed.

    You could also fill up the AllowedCustomers list dynamically, e.g. loop through all customers that have a boolean checked and use the .Add function for them to add their Code to the list.

    Just remember that these files will be saved on the server and there is no automatic way to download them to the client. Since we now work through the web browser, we can no longer access the client file system directly.
    procedure SavePackingList(SalesHeader: Record "Sales Header")
        var
            FileMgt: Codeunit "File Management";
            ReportToRun: Report "AAA Packing List Report";
            JobsSetup: Record "Jobs Setup";
            ServerFileName: Text;
            AllowedCustomers: List of [Code[20]];
        begin
            // Get Setup
            JobsSetup.Get();
            JobsSetup.TestField("AAA Packing List Save Location");
    
            // Check if file needs to be saved
            AllowedCustomers.Add('CUSTOMER1');
            AllowedCustomers.Add('CUSTOMER2');
            AllowedCustomers.Add('CUSTOMER3');
            if (not AllowedCustomers.Contains(SalesHeader."Sell-to Customer No.")) then
                exit;
    
            // Save file
            ServerFileName:= JobsSetup."AAA Packing List Save Location" + '\PackingList_' + SalesHeader."No." + '.pdf';
            ReportToRun.SetTableView(SalesHeader);
            ReportToRun.UseRequestPage(false);
            ReportToRun.SaveAsPdf(ServerFileName);
        end;
    

    I admit, I did not try the code above, but it should work.
    After the report has run, you could also save the created file location to a field on the Sales Header for later use.
Sign In or Register to comment.