ReadRecord Example

This following example uses the same File Layout definition as the previous example. The file format is CSV. The fields are separated by commas and delimited by double-quotes.

"8001","VAC","09/12/1981","09/26/1981","14","0","","P","Y",""                     
"8001","VAC","03/02/1983","03/07/1983","5","0","","P","Y",""                      
"8001","VAC","08/26/1983","09/10/1983","13","0","","P","Y",""                      
"8105","CNF","02/02/1995","??/??/","0","0","","U","N",""                           
"8516","MAT","06/06/1986","08/01/1986","56","0","","P","Y",""                      
"8516","SCK","08/06/1988","08/07/1988","1","0","","P","Y",""                       
"8516","VAC","07/14/1987","07/28/1987","14","0","","P","Y",""                      
"8553","JUR","12/12/1990","12/17/1990","5","0","Local Jury Duty","P","N",""        
"8553","MAT","02/20/1992","10/01/1992","224","0","Maternity Leave","U","N",""     
"8553","MAT","08/19/1994","03/01/1995","194","0","Maternity-2nd child","U","Y","" 
"8553","PER","04/15/1993","04/19/1993","4","0","","U","N","Personal Day required" 
"8553","SCK","01/28/1987","01/30/1987","2","0","Hong Kong Flu","P","N",""         
"8553","SCK","08/02/1988","08/03/1988","1","0","Sick","P","N",""                  
"8553","SCK","09/12/1995","09/13/1995","1","0","","P","N",""                      
"8641","VAC","06/01/1988","06/15/1988","14","0","","P","Y",""                     
"8641","VAC","07/01/1989","07/15/1989","14","0","","P","Y",""                     
"G001","MAT","07/02/1991","09/28/1991","88","0","3-month Maternity leave","P","Y",⇒
"Maternity will be paid as 80% of Claudia's current salary."

To read in the previous CSV file we use the following PeopleCode. It reads the file into a temporary record. First each line of the file is read into a string. The string is split into an array, with the value of each field in the array becoming an element in the array. The value of each field in the record is assigned a value from the array. After additional processing (for example, converting strings into dates or numbers, verifying data, and so on) the record can be inserted into the database. To insert the final data into the database, this code must be associated with a PeopleCode event that allows database updates, that is, SavePreChange, WorkFlow, SavePostChange, and so on. This code could also be used as part of an Application Engine program.

Local File &MYFILE;
Local Record &REC;
Local array of string &ARRAY;

&MYFILE = GetFile("c:\temp\vendor.txt", "R", "UTF8", %FilePath_Absolute);
&REC = CreateRecord(RECORD.ABS_HIST_TEST);
&ARRAY = CreateArrayRept("", 0);

If &MYFILE.IsOpen Then
   If &MYFILE.SetFileLayout(FILELAYOUT.ABS_HIST) Then
      While &MYFILE.ReadLine(&STRING);
         &ARRAY = Split(&STRING, ",");
         For &I = 1 To &REC.FieldCount
            &REC.GetField(&I).Value = &ARRAY[&I];
         End-For;
      /* do additional processing here for converting values */
         &REC.Insert();
      End-While;
   Else
      /* do error processing - filelayout not correct */
   End-If;
Else
   /* do error processing - file not open */
End-If;

&MYFILE.Close();

Note:

You can't read a file that contains a thousands separator for numeric fields. You must strip out the separator before you try to read in the file.