Exporting data from a C/AL function

The next code example demonstrates how to use this feature. We will call the Export Lease Contracts XMLport from a page to export the selected contract, without opening the request page.

The ExportContract function shown in the code block exports the contract record by directing the XMLport output into a file stream. The SETTABLEVIEW function is used to limit the export to records satisfying the filtering criteria applied to the LeaseContractHeader  variable. Before calling SETTABLEVIEW, we set a filter on the contract number to select a single record; in practice it, can be any viable table filter. This function should be declared on page 50505 Lease Contract, and will be called on the same page:

LOCAL PROCEDURE ExportContract(ContractNo : Code[20]) : Boolean;
VAR
LeaseContractHeader : Record 50500;
FileManagement : Codeunit 419;
ExportLeaseContracts : XMLport 50508;
FileStream : OutStream;
FileName : Text;
OutFile : File;
BEGIN
FileName := FileManagement.SaveFileDialog('Export contract','','xml|*.xml');
IF FileName = '' THEN
ERROR('');

IF EXISTS(FileName) THEN
ERASE(FileName);

OutFile.CREATE(FileName);
OutFile.CREATEOUTSTREAM(FileStream);

LeaseContractHeader.SETRANGE("No.",ContractNo);
ExportLeaseContracts.SETTABLEVIEW(LeaseContractHeader);
ExportLeaseContracts.SETDESTINATION(FileStream);
EXIT(ExportLeaseContracts.EXPORT);
END;

To receive the filename, the function opens a dialog, SaveFileDialogIf the selected file already exists, it is deleted and recreated again. We don't ask the user to confirm the overwriting of the file, since this confirmation request is raised by the SaveFileDialog  function in File Management. The dialog will not return the filename if the user selects an existing file and then does not confirm the choice.

To break function execution when the user cancels the action, we use a little trick: we call the ERROR  function with the empty string in the parameter. The ERROR('')  syntax will stop the function execution silently, without showing any error messages.

To invoke the export function, create an action button on page 50505 Lease Contract. There is already an action container, Documents, on this page, which we created in Chapter 4, Designing User Interface. New actions can be added to the same container. Create the action with the parameters shown in the following table:

Type Name Caption
Action ExportContract Export Contract

 

This action will require two global text constants:

Name ConstValue
ContractExportedMsg Contract was successfully exported
CouldNotWriteFileErr Could not write file

 

The OnAction trigger of the action button we just created calls the ExportContract function and displays a message, depending on the export status, as shown in the next code block:

OnAction=BEGIN
IF ExportContract("No.") THEN
MESSAGE(ContractExportedMsg)
ELSE
MESSAGE(CouldNotWriteFileErr);
END;

With the new action, you can run the XML export, hiding the native XMLport interface and controlling its functionality from your C/AL function. The resulting code is more complicated than the simple XMLport.RUN, but it gives you detailed control over object execution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.61.49