GRC
HR
SCM
CRM
BI


Article

 

Using Field Symbols in ABAP

by Steven Shi | SAPinsider

April 1, 2001

by Steven Shi, Storability SAPinsider - 2001 (Volume 2), April (Issue 2)
 

Someone once commented that the three great virtues of a programmer are laziness, impatience, and hubris.

     What this really means, though, is that good programmers are always searching for ways to write reusable code so that others can benefit from their labor.

     ABAP, as a modern procedure language, has many features that allow developers to write compact and reusable code. One of the less-applied features is the use of field symbols. Field symbols are used to dynamically assign references to a variable at runtime, as opposed to the more conventional way in which variables are defined at compile time.

     In this article, I'll demonstrate the benefits of field symbols, using a program I wrote for a real-world application. For a more general understanding of field symbols, you can consult the SAP documentation.

The Problem: Cumbersome Code for Populating Delimiter Fields

Often, when writing an interface program, you need to extract SAP data into a comma-delimited flat file. Normally, to do this, you would simply add delimiters to a structure before transferring data to an operating system file.

     For example, I wrote a program to extract material master data into a comma-delimited file. The first thing I did was to define the structure of that comma-delimited file, shown in Listing 1.



	DATA: BEGIN OF MARAFILE
         MATNR	like MARA-MATNR,
         MARAFILE-DELIMITER1	TYPE C,
         WERKS	like MARC-WERKS,
         MARAFILE-DELIMITER2	TYPE C,
         MTART	like MARA-MTART,
         MARAFILE-DELIMITER3	TYPE C,
         MATKL	like MARA-MATKL,
         MARAFILE-DELIMITER4	TYPE C,
         .......         "More fields like those above
         MARAFILE-DELIMITER27	TYPE C,
         END OF FLATFILE.
		
Listing 1 Defining the Structure of a Comma-Delimited File

     In an interface program, you would populate the main structure fields with data from SAP. Before writing the structure out to a file, you would populate the remaining delimiter fields with codes like those shown in Listing 2.



DATA:  C_DELIMITER VALUE ','.

   MOVE:    C_DELIMITER TO MARAFILE-DELIMITER1,
            C_DELIMITER TO MARAFILE-DELIMITER2,
            C_DELIMITER TO MARAFILE-DELIMITER3,
            C_DELIMITER TO MARAFILE-DELIMITER4,
            ....
            C_DELIMITER TO MARAFILE-DELIMITER27.
	    
Listing 2 One Approach to Populating Delimited Fields

     This is fine as long as you don't have a large structure. But what if you do? Or what if you have to write hundreds of structures, as I had to do? Worse yet, what if you need to delete or add a couple of fields in your program and change the numbering of your delimiters in your structure? You then would end up rewriting the block in Listing 2 everywhere it exists in your program.

     Wouldn't it be better to dynamically populate all these delimiters with one statement? You can do that! The answer lies in the use of field symbols.

The Solution: Field Symbols

Listing 3 is a routine to populate your structures with delimiters - one that you can directly copy into your program.



*------------------------------------------------------------------------------- *
*    FORM POPULATE_DELIMITER                                                     * 
*------------------------------------------------------------------------------- *
* Populate a structure with delimiters.                                          *                           
* Variables in the structure must follow a predefined                            *
* naming convention of a string followed by a numeric value.                     *
* Parameters:   STRING_NAME - The character portion of the variable name         *
*               V_TIMES - number of times you want the routine to append         *
*               a number to the end of the STRING_NAME to form the variable name *
*------------------------------------------------------------------------------- *

FORM POPULATE_DELIMITER USING STRING_NAME VALUE(V_TIMES).

 FIELD-SYMBOLS:  .   " A place holder for dynamically assigned variables
 
 DATA:	COUNTER(3) TYPE C,	     "Note:	The maximum number of allowed loops is
				                 "       999, as reflected in the data type.
		FIELD_NAME(40) TYPE C,   "The place holder for the concatenated field name.
				                 "The maximum length is 40.
		C_DELIMITER VALUE ','.	 "The delimiter you want to use.
  
   CLEAR COUNTER.
   DO.
    COUNTER = COUNTER + 1.
    " Keep incrementing the counter until it exceeds the value passed in by the 
    " calling program.
    IF COUNTER > V_TIMES.
          EXIT.
    ENDIF.
    CONCATENATE STRING_NAME COUNTER INTO FIELD_NAME .
    CONDENSE FIELD_NAME .
    ASSIGN (FIELD_NAME) TO .
     IF SY-SUBRC = 0.	"Important! Run time check to make sure this 
				"variable does exist.
				"Otherwise the program may produce a shortdump.
      MOVE: C_DELIMITER TO .
     ENDIF.
   ENDDO.

 ENDFORM.                               " POPULATE_DELIMITER
Listing 3 The FORM POPULATE_DELIMITER Routine

     Now, when you go back to the interface program, this is all you need to write to populate the delimiters:


PERFORM POPULATE_ DELIMITER 
   USING 'MARIFILE-
   DELIMITER' 40.

     Notice that you don't have to code the number of loops (in our example, 40) using exactly the same number as the number of delimiters in your structure (27 in Listing 1). Since the subroutine will examine each variable at runtime, you can pass a larger number, which gives you the flexibility to modify your structure and increase the number of delimiters, or skip a few without having to modify the PERFORM statement.

     Who knew that laziness, impatience, and hubris could be so productive?

Steven Shi is a Senior Software Engineer for Storability in Southborough, Massachusetts. Storability is the market leader in building and managing high-end enterprise storage area networks (SANs). Steve is tasked with building a first-of-its-kind application to enable real-time management of distributed networks and storage devices. In exchange for doing real work, he gets to socialize with the world's best engineers in the fields of optical networking, enterprise storage, and object-oriented programming.

Steve has extensive experience in designing and implementing modern n-tiered information systems. His background ranges from mainframe computing to client/server technology, and from SAP systems to Internet applications. Before joining Storability, he was a Lead Engineer for EC Cubed, an Internet application provider, and led a team of software engineers building an e-commerce application on the Java platform. Prior to EC Cubed, Steve was a functional and technical consultant for SAP America and Andersen Consulting.

He has consulted his clients in the areas of SAP Materials Management, Sales and Distribution, Quality Management, and of course, ABAP development, and has been a speaker at ASUG and an author for the SAP Professional Journal.

Steve can be reached at steve_shi@mail.com.

Did you find this article helpful? Get access to the latest updates and resources from SAPinsider with a free subscription.

Get the SAPinsider subscription now »»

An email has been sent to:






More from SAPinsider



COMMENTS

Please log in to post a comment.

No comments have been submitted on this article. Be the first to comment!


SAPinsider
FAQ