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.
-
Associating the codeunit with an action on a Role Center page.
-
Testing the codeunit.
Note |
---|
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
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, click Codeunit and then click New.
C/AL Editor opens.
-
Click the OnRun() function in C/AL Editor.
-
Click View and then click C/AL Locals.
The OnRun - C/AL Locals window opens.
-
Click the Variables tab.
-
Enter the following local variables.
Name DataType Length Name
Text
200
ReturnValue
Boolean
TempFile
File
ToFile
Variant
NewSteam
InStream
-
Click 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 // 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));
-
Save the codeunit, giving it a name of FileTransfer and an ID of 50011.
-
Close the code editor.
Associating the Codeunit with an Action
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
-
In Object Designer, click Page, select page 9006 Order Processor Role Center, and then click Design.
Page Designer opens.
-
Click the first empty line on the page, click View, and then click Actions.
Action Designer opens.
-
Click the first empty line in Action Designer and in the Caption column for that line, enter File Transfer. Set Type to Action.
-
With the File Transfer line selected, press SHIFT+F4 to open the Properties window.
-
Set the RunObject property value to c50011. When this action is selected on the Role Center, codeunit 50011 will run.
-
Close the Properties window.
-
Close Action Designer.
-
Close Page Designer and save the change to page 9006.
Testing the Codeunit
The final step is to test the codeunit.
To test the codeunit
-
Open the RoleTailored client.
-
On the Sales Order Processor role center, click Actions, and then click File Transfer.
The Export File dialog box appears.
-
Click Save and save to the default location, noting what that location is.
-
Click OK.
-
A message box opens specifying the location of the saved file. Click OK.
-
Browse to the specified file location on your client computer.
-
Open the file.
-
Note that the file contains the text 'abc'.
See Also
© 2010 Microsoft Corporation. All rights reserved.