File handling in InputScripts

There are a number of useful things that you can do with files in an InputScript:
  • you can read data from a file and enter them into R/3
  • you can extract data from R/3 via the transactions and use the generated files in Excel or similar tools
  • you could generate an html file and then display it with R/3 using the GuiXT Viewer

This method works well for any number of records in the file.  For performance reasons it might be preferable to choose other approaches (batch input, ABAP programs) for a very large amount of data. We suggest you measure the runtime first if several thousand records or more have to be entered.

Overview
We use the following InputAssistant features (see the keyword documentation for further details):
  • Open and close a file: OpenFile, CloseFile
  • Read values from a file into variables: ReadFile
  • Write a new record to a file: AppendFile
  • Handling of variables: Set, if
  • Flow control within the InputScript: Screen, Enter, goto, label, Return
Example 1

Let’s assume that we have a text file with material numbers. We want to produce a second file containing the material number together with the division, material group and authorization group for each material.

First we need a place where we can start the script, for example the basic R/3 menu:

MENUS00.E0040.txt:
Pushbutton (toolbar) “Generate file” “/NMM03″ Process=CreateMaterialFile.txt”

The InputScript CreateMaterialFile.txt looks like this:

// Create material file (Example for R/3 Rel. 4.5)
Parameter mat_file1 “C:\GuiXT\Input.txt”
Parameter mat_file2 “C:\GuiXT\Output.txt”

// Start
Screen SAPLMGMM.0060
OpenFile “&[mat_file1]“
OpenFile “&[mat_file2]” “-Output”

label Read_File
ReadFile “&[mat_file1]” mat_matnr

if not V[mat_matnr]
   CloseFile “&[mat_file1]“
   CloseFile “&[mat_file2]“
   Return “Material file generated” “&[mat_file2]“
endif

Set F[Material] “&V[mat_matnr]“
Enter

// Select view
Screen SAPLMGMM.0070
Set C[Basic Data 1] “X”
Enter

// Basic data 1
Screen SAPLMGMM.4000
Set V[mat_division] “&F[Division]“
Set V[mat_matgroup] “&F[Material group]“
Set V[mat_autgroup] “&F[Authorization group]“

AppendFile “&[mat_file2]” mat_matnr mat_division mat_matgroup mat_autgroup

Enter “/NMM03″

// MM03 Start again, goto beginning of script
Screen SAPLMGMM.0060
goto Read_File

The runtime for 100 material numbers was 67 seconds in our test system (central system on a PC with 266 MHz Pentium and 256 MB RAM) .

 

Example 2

Let’s assume that we have a text file with data representing GL documents.  We want to post these documents in R/3 using transaction FB01. If an error message occurs during one of these R/3 transactions, we put the error message into a log file, together with some data .

MENUS00.E0040.txt:
Pushbutton (toolbar) “GL upload” “/NFB01″ “Process=FB01_GL.txt”

The InputScript FB01_GL.txt appears as follows:

// File names
Parameter FB01FILE “C:\GL Test\FB01_data.txt”
Parameter FB01ERR  “C:\GL Test\FB01_err.txt”

// Start file processing
OpenFile “&[FB01FILE]” Delimiter=”;”
OpenFile “&[FB01ERR]” “-Output”

Set V[FB01_errors] 0
Set V[FB01_records] 0

Screen SAPMF05A.0100

label start

// Error in previous record?
if V[_lasterror]

  // Increase error count
  Set V[FB01_errors] &[F
B01_errors] + 1

  // Write error log
  AppendFile “&[FB01ERR]” s_date s_text s_pstky1 s_account1 s_amount1 s_pstky2 s_account2 s_amount2 _lasterror

  // Reset error indicator (!). Automatic reset only at start of InputScript processing 
  Set V[_lasterror]
endif 

ReadFile “-StripQuotationMarks” “&[FB01FILE]” s_date s_text s_pstky1 s_account1 s_amount1 s_pstky2 s_accoun
t2 s_amount2 

// Another record to process?
if V[s_date]

  // Increase record count
  Set V[FB01_records] &[FB01_records] + 1

  Set F[Document date] &[s_date]
  Set F[Doc.header text] &[s_text]
  Set F[Company Code] “0001″
  Set F[Type] “SA”
  Set F[Currency/rate] “USD”

  Set F[Pstky] &[s_bschl1]
  Set F[Account] &[s_account]

  Enter OnError=”/NFB01″

else

  // Processing complete, close files
  CloseFile “&[FB01FILE]“ 
  CloseFile “&[FB01ERR]“ 

  if V[FB01_errors=0]
    Return “Processing done, no errors, &[FB01_records] documents in total”
  else

    // Display error log
    View “&[FB01ERR]“ 

    Return “Processing done: &[FB01_errors] errors, &[FB01_records] documents in total”
  endif
endif 

Screen SAPMF05A.0300
Set F[Amount] &[s_amount1]
Set F[PstKy]  &[s_pstky2]
Set F[GL/ account]  &[s_account2]

Enter OnError=”/NFB01″

Screen SAPMF05A.0300
Set F[Amount] &[s_amount2]
Enter “/11″ OnError=”/NFB01″

Screen SAPMF05A.0100
goto start 

The input file FB01_data.txt could appear as follows:

01122000;”GL text1″;40;113100;100;50;113101;100
02122000;”GL text2″;40;113100;200;50;113777;200
15122000;”GL text3″;40;113100;300;50;113101;300
32122000;”GL text4″;40;113100;400;50;113101;400
14122000;”GL text5″;40;113100;500;50;113101;500
01122000;”GL text6″;40;113100;100;50;13101M;100
02122000;”GL text7″;40;113100;200;50;113777;200
15122000;”GL text8″;40;113100;300;50;113101;300
32122000;”GL text9″;40;113100;400;50;113101;400

File handling in InputScripts