WriteRecord method: File class

Syntax

WriteRecord(record)

Description

The WriteRecord method is a file layout method. It writes the contents of the record object record, to the output file. (Remember, a record object contains only one row of data from an SQL table.)

You can use this method to build a transaction in the output file, one file record at a time, without having to instantiate and populate a rowset object. You can apply this method to any record whose structure and name matches that of a record defined in the current file layout.

Note:

You must execute the SetFileId method from the file object before using WriteRecord, otherwise it returns False.

See SetFileLayout method: File class.

Note:

When you're writing text to an XML file, special HTML characters, such as ampersands, lesser than or greater than symbols, and so on, are automatically converted to valid XML character string, such as &amp.

Parameters

Parameter Description

record

Specify the name of an existing record object to be written. You can use Rowset class methods such as GetField and built-in functions such as GetRecord to populate the record with data before writing it to the file.

Returns

A Boolean value: True if the method succeeds, False otherwise. This value is optional.

Example

The following example appends all the data in a record to an existing file:

Local File &MYFILE;

&MYFILE = GetFile("record.txt", "A", "UTF8");

If &MYFILE.IsOpen Then
   If &MYFILE.SetFileLayout(FILELAYOUT.VOL_TEST) Then
      &LN = CreateRecord(RECORD.VOLNTER_ORG_TBL);
      &SQL2 = CreateSQL("%Selectall(:1)", &LN);
      While &SQL2.Fetch(&LN) 
         &MYFILE.WriteRecord(&LN);
      End-While;
   Else
      /* do error processing - filelayout not correct */
   End-If;
Else
   /* do error processing -; file not open */
End-If;

&MYFILE.Close();

Considerations Using XML With File Definition Tags

File Definition Tags have no effect when importing data. However, generally, during export, the File Definition Tag is added at the start and end of the data to create a valid XML file.

The one exception is when the file to which the data is being written during export has been opened in append mode. At that time, the file definition tag is not taken into consideration.

Considerations Using XML with File Definitions

If your file layout is defined as XML, WriteRecord doesn't add the closing tag for the record. You must write it yourself using the WriteLine method. This is because the code has no way of knowing when you want to write children records following the record just written out.

The following code shows an example of using WriteLine:

Local File &MYFILE; 

&MYFILE = GetFile("XMLrecord.txt", "A", "UTF8"); 

If &MYFILE.IsOpen Then 
   If &MYFILE.SetFileLayout(FILELAYOUT.RECORDLAYOUT) Then 
      &LN = CreateRecord(RECORD.QA_INVEST_LN); 
      &SQL2 = CreateSQL("%Selectall(:1)", &LN); 
      While &SQL2.Fetch(&LN) 
         &MYFILE.WriteRecord(&LN); 
         WriteLine("</QA_INVEST_LN>"); /* Add the closing tag */ 
      End-While; 
   Else 
   /* do error processing - filelayout not correct */ 
   End-If; 
Else 
/* do error processing - file not open */ 
End-If; 

&MYFILE.Close();