Walkthrough: Transferring a File from Microsoft Dynamics NAV Server to the Client

This walkthrough demonstrates how to develop a codeunit that creates a file on Microsoft Dynamics NAV Server, opens the file, writes some data to it, and then downloads that file to a RoleTailored client.

This walkthrough illustrates the following tasks:

  • Creating a codeunit that creates a text file, opens the file, writes some text to the file, and then downloads that file to the client.

  • Associating the codeunit with an action on a Role Center page.

  • Testing the codeunit.

NoteNote

The codeunit that you develop is intended for use in the RoleTailored client and does not run in the Classic client.

Prerequisites

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV 2009 installed with a developer license

The first step is to create the codeunit. In the codeunit, the toFile variable is initially assigned a value of samplefile.txt. If the user specifies another file name, then this new value and the file path are returned in toFile.

To create the codeunit

  1. In Object Designer, click Codeunit and then click New.

    C/AL Editor opens.

  2. Click the OnRun() function in C/AL Editor.

  3. Click View and then click C/AL Locals.

    The OnRun - C/AL Locals window opens.

  4. Click the Variables tab.

  5. Enter the following local variables.

    Name DataType Length

    Name

    Text

    200

    ReturnValue

    Boolean

    TempFile

    File

    ToFile

    Variant

    NewSteam

    InStream

  6. Click the next empty line to ensure that the local variables are saved.

  7. Close the C/AL Locals window.

  8. Enter the following code in the C/AL Editor for the OnRun() method.

    // ISSERVICETIER checks whether the code is running on the RoleTailored client.
    // If so, create a temporary file on NAV server, write the content // to the tempoary file, and then create an instream for the
    // temporary file.
    
    IF ISSERVICETIER THEN BEGIN
        TempFile.CREATETEMPFILE();
        TempFile.WRITE('abc');
        TempFile.CREATEINSTREAM(NewStream);
        ToFile:='SampleFile.txt';
    
        // Tranfer the content from the temporary file on the NAV server to a file on the RoleTailored client.
    
        ReturnValue:=DOWNLOADFROMSTREAM(
                  NewStream,
                  'Save File to RoleTailored Client',
                  '',
                  'Text File *.txt| *.txt',
                  ToFile);
    
        // Close the temporary file and delete it from NAV server.
    
        TempFile.CLOSE();
    END
    
    // If running on the Classic client
    
    ELSE BEGIN
        ToFile:='SampleFile.txt';
        TempFile.CREATE(ToFile);
        TempFile.WRITE('abc');
        ToFile:=TempFile.NAME;
        TempFile.CLOSE();
    END;
    
    // Post a message indicating that the file is saved on the client.
    
    MESSAGE(FORMAT(ToFile));
    
  9. Save the codeunit, giving it a name of FileTransfer and an ID of 50011.

  10. Close the code editor.

Now that you have the completed codeunit, you need to associate it with an action in the RoleTailored client so it can be tested. In this case, you will add a new action to the Sales Order Processor role center and name it File Transfer. When selected, this action will call the codeunit.

To associate the codeunit with an action

  1. In Object Designer, click Page, select page 9006 Order Processor Role Center, and then click Design.

    Page Designer opens.

  2. Click the first empty line on the page, click View, and then click Actions.

    Action Designer opens.

  3. Click the first empty line in Action Designer and in the Caption column for that line, enter File Transfer. Set Type to Action.

  4. With the File Transfer line selected, press SHIFT+F4 to open the Properties window.

  5. Set the RunObject property value to c50011. When this action is selected on the Role Center, codeunit 50011 will run.

  6. Close the Properties window.

  7. Close Action Designer.

  8. Close Page Designer and save the change to page 9006.

The final step is to test the codeunit.

To test the codeunit

  1. Open the RoleTailored client.

  2. On the Sales Order Processor role center, click Actions, and then click File Transfer.

    The Export File dialog box appears.

  3. Click Save and save to the default location, noting what that location is.

  4. Click OK.

  5. A message box opens specifying the location of the saved file. Click OK.

  6. Browse to the specified file location on your client computer.

  7. Open the file.

  8. Note that the file contains the text 'abc'.

Community Additions

ADD
Show: