Header Ads

  • Recent Posts

    Two dimensional arrays and writing data into multiple files from one Application Engine.

    How to open multiple files in a PeopleSoft Application Engine program and write data into them based on certain defined conditions?
    For example, if there is a program that processes every row of employee and certain employee details are to be written to a file. However, if the data is to be sorted and written based on every department and a unique file has to be created for every company and if this sorting fetches companies in random order (and intermittently) then how to identify which row of data has to go in which file. I have got the below code to do this by making use of two-dimensional arrays in PeopleSoft.

    How to make use of two dimensional arrays in Peoplecode?
    How to get(fetch) and put data in a two dimensional PeopleSoft array?
    In my view, structure and usage of multidimensional arrays in peoplecode are slightly different from SQR and other programming languages.

    Here is the code snippet which explains how to have multiple files open in an Application engine and write data into each file based on a predefined condition by making use of two dimensional arrays in PeopleSoft:
    Component array of array of string &Files;
    Component string &Counter;
    Component number &no_of_rows;
    Component boolean &File_Created, &New_File;
    rem Both these boolean variables must be set to False initially as the program runs and then the value must be changed through the below code;
    /* &Variable_Field - field value based on which file names should be created, for example if the App Engine should create different files for each company then use company as this variable field. */
    &Filename = &Variable_Field | %Date | ".txt"; 
    &PathFileOut = &FolderName | &Path_Separator | &Filename;
    If &File_Created = False Then
    /* This is going to be the first file created by the program and this if block will get executed only for the first row of data encountered by the program */
       &New_File = True;
    /* Open in write mode as each file is created for first time */
       &OUT_FILE = GetFile(&PathFileOut, "w", %FilePath_Absolute); 
       &Counter = "1";
    /* Let us create a two dimensional array with the file name in position 'i' and no of records written to each file in position 'j' of the array. */
       &Files = CreateArray(CreateArray(&PathFileOut, &Counter));
       &Files [1][1] = &PathFileOut;
       &Files [1][2] = &Counter;
    /* The below initialization makes sure that the above block of code gets executed just only once */
       &File_Created = True;
       &I = 2;
    Else
    /* This piece of code will get executed for every row after the first row of data encountered by the program; We check if the output file name is already present in the array (which means that we are validating if we have created the file in this session or else */
       Local number &Arr_Posn = &Files.Find(&PathFileOut);
       If &Arr_Posn = 0 Then
       /* Since file name is NOT FOUND in the array we create a new file in WRITE mode */
          &New_File = True;
          &OUT_FILE = GetFile(&PathFileOut, "w", %FilePath_Absolute);
          &Counter = "1";
          &Files.Unshift(&PathFileOut);
          &Files [1][2] = &Counter;
       Else
    /* Since file name is FOUND in the array we get the file in APPEND mode and have the &New_File variable set to false */
          &New_File = False;
          &OUT_FILE = GetFile(&PathFileOut, "a", %FilePath_Absolute);
          /* Reach the 'i' position in which the file name is found in the array; then read the 'j' value and increment it by 1 which indicates the total no of records processed in each file. */
          Local any &Ctr = &Files [&Arr_Posn][2];
          &Ctr = Value(&Ctr) + 1;
          &Ctr = String(&Ctr);
          &Files [&Arr_Posn][2] = &Ctr;
          &Counter = &Ctr;
       End-If;
    End-If;


    /* Have the below piece of code in appropriate place to print header for every file created */
    If &New_File = True Then
    /* code to print header */
    End-if;
    &no_of_rows = &no_of_rows + 1;


    /* Finally close all the files created, this code should get executed only once and before the end of the App Engine Program, please make sure to declare all the required variables used here. */
    If  &File_Created = True then
       &no_of_Files = &Files.Len;
       &i = &no_of_Files;
       While &i > 0
          &PathFileOut = &Files.Shift();
          &Out_file_to_close = GetFile(&PathFileOut, "R", %FilePath_Absolute);
          &Out_file_to_close.close();
          &i = &i - 1;
       End-While;
    End-If;

    Note:
    In case the program opens several files, users may encounter a message like, exceeded the max number of files that can be opened. In such cases, it is better to close each file after writing every row of data into the file as any way we are reopening it with the use of arrays as specified above.

    No comments

    Please refrain for marketing messages and unnecessary back links.

    Post Top Ad

    Post Bottom Ad