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.


  1. Function getHeader(&rec) Returns string
  2. Local string &hdr;
  3. For &i = 1 To &rec.FieldCount
  4. SQLExec("select label_id from psrecfield where recname = :1 and fieldname = :2", &rec.name, &rec.GetField(&i).name, &labelId);
  5. &label = &rec.GetField(&i).GetLongLabel(&labelId);
  6. If None(&label) Then
  7. &label = &rec.GetField(&i).GetLongLabel(&rec.GetField(&i).name);
  8. If None(&label) Then
  9. &label = &rec.GetField(&i).name;
  10. End-If;
  11. End-If;
  12. &header = &header | """" | &label | """";
  13. If &i < &rec.FieldCount Then
  14. &hdr = &hdr | ",";
  15. End-If;
  16. End-For;
  17. Return &hdr;
  18. End-Function;
  19. /* main */
  20. Local Record &recRpt = CreateRecord(Record.MCM_ADSDD_RPT);
  21. Local File &ExportFile;
  22. &ExportFile = GetFile("c:\temp\myfile.csv", "W", %FilePath_Absolute);
  23. If &ExportFile.IsOpen Then
  24. &ExportFile.WriteLine(getHeader(&recRpt));
  25. While ...
  26. 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