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.
About This Walkthrough
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.
- Testing the codeunit.
Prerequisites
To complete this walkthrough, you will need:
- Microsoft Dynamics NAV 2013 installed with a
developer license
Creating the Codeunit
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
-
In Object Designer, choose Codeunit and then choose New.
C/AL Editor opens.
-
Choose the OnRun() function in C/AL Editor.
-
Choose View and then choose C/AL Locals.
The OnRun -C/AL Locals window opens.
-
Choose the Variables tab.
-
Enter the following local variables.
Name DataType Length Name
Text
200
ReturnValue
Boolean
TempFile
File
ToFile
Variant
NewSteam
InStream
-
Choose the next empty line to ensure that the local variables are saved.
-
Close the C/AL Locals window.
-
Enter the following code in the C/AL Editor for the OnRun() method.
Copy Code // Create a temporary file on NAV server, write the content // to the tempoary file, and then create an instream for the // temporary file. TempFile.CREATETEMPFILE(); TempFile.WRITE('abc'); TempFile.CREATEINSTREAM(NewStream); ToFile:='SampleFile.txt'; // Transfer 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(); // Post a message indicating that the file is saved on the client. MESSAGE(FORMAT(ToFile));
-
Save the codeunit, giving it a name of FileTransfer and an ID of 50011.
-
Close the code editor.
Testing the Codeunit
The final step is to test the codeunit.
To test the codeunit
-
In Object Designer, select the codeunit that you created, and then choose the Run button.
The Export File dialog box appears.
-
Choose Save and save to the default location, noting what that location is.
-
Choose OK.
-
A message box opens specifying the location of the saved file. Choose OK.
-
Browse to the specified file location on your client computer.
-
Open the file.
-
Note that the file contains the text 'abc'.