Thursday, January 30, 2014

CSV Headers for File Layout

I couldn't find a nice way to build a CSV header using the file layout.  You seem to always need a record to send to the file layout in order to write a different segment.  So I found this solution that uses the record field names to write the header.


Function getHeader(&rec) Returns string
   Local string &hdr;
   For &i = 1 To &rec.FieldCount
      SQLExec("select label_id from psrecfield where recname = :1 and fieldname = :2", &rec.name, &rec.GetField(&i).name, &labelId);
      &label = &rec.GetField(&i).GetLongLabel(&labelId);
      If None(&label) Then
         &label = &rec.GetField(&i).GetLongLabel(&rec.GetField(&i).name);
         If None(&label) Then
            &label = &rec.GetField(&i).name;
         End-If;
      End-If;
      &header = &header | """" | &label | """";
      If &i < &rec.FieldCount Then
         &hdr = &hdr | ",";
      End-If;
   End-For;
   Return &hdr;
End-Function;
/* main */
Local Record &recRpt = CreateRecord(Record.MCM_ADSDD_RPT);
Local File &ExportFile;
&ExportFile = GetFile("c:\temp\myfile.csv", "W", %FilePath_Absolute);
If &ExportFile.IsOpen Then
   &ExportFile.WriteLine(getHeader(&recRpt));
   While ...
End-If;

This will first check if there is a specific label set on the record version of the field. If that value is blank it will attempt to get the label with the same name as the field itself. If there still is no value just use the field name.

No comments:

Post a Comment