This website is not affiliated with, sponsored by, or approved by SAP AG.

0003 - User Defined Types

Moderators: thx4allthefish, Snowy, ilya, Rich

0003 - User Defined Types

Postby Rich » Thu Mar 26, 2009 11:48 am

User Defined Types.

Typing is a powerful way of creating programs that are easily maintainable, easy to read and understand.

User defined types are created generally in a top include are are introduced with the 'TYPES' keyword. A type statement defines the characteristics of a variable:

Code: Select all
Types: Uint  type i,                       " Integer
       UChar type c,                       " Char
       UKnnr type Kunnr,                   " Customer code
       UVkrg type VkOrg,                   " Sales Org
       UVtWg type VtWeg,                   " DC
       USprt type Spart,                   " Division
       UVrsg type Versg,                   " Stats group,
       UVsBd type Vsbed,                   " Ship conditions
       UdWrk type dWerk,                   " Del plant
       UKtgd type Ktgrd,                   " Account Assignment
       UZtrm type Zterm,                   " Payment terms,
       UAknt type Akont,                   " Recon account
       UNatt type Natxt,                   " Text message
       UFleN type FileName,                " File name
*     
       Begin of Upload_Record,             " Inbound data
             Kunnr type UKnnr,             " Customer code
             Vkorg type UVkrg,             " Sales Organisation
             Vtweg type UVtwg,             " Distribution channel
             Spart type USprt,             " Division
             Blank type Uchar,             " Blank column
             Versg type UVrsg,             " Customer Stats Group
             VsBed type UVsbd,             " Shipping Conditions
             Vwerk type UdWrk,             " Delivering Plant
             Ktgrd type UKtgd,             " Account Assignment Group
             ZTerm type UzTrm,             " Payment Terms
             Akont type UAknt,             " Reconciliation Account
        End of Upload_Record,
*
        Begin of Results_Record.
              Include type Upload_Record.
Types:        Text    type UNatt,          " BDC Error message
        End of Results_Record,
*
        Upload_Table type standard table of Upload_Record
                     Initial size 0,
        Error_Table  type standard table of Results_Record
                     Initial size 0.
*


As can be seen in the example above, types can be used to construct simple data types, structures, tables and so forth.

Once a type has been defined, it can be used to declare variables through out the program, including both parameter and form variables:

Code: Select all
Form Write_Error_File using pu_infile    type UFleN
                            t_error_list type Error_Table.
*
     Data: t_Outtab        type Upload_Table,
           w_Outtab        type Upload_Record,
           w_Results       type Results_Record,
           w_Outfile       type FileName,        " Output file
           w_Sfile         type string,
           w_Max_Lines     type UInt,            " Progress indication
           w_Cur_Line      type UInt,
           w_Msg           type UNatt.


This then provides an additional level of code checking because the parameters to the form are typed and it prevents you from passing any old parameter to the form.

Types can be made system wide by incorporating the data definitions into the ABAP dictionary using transaction SE11.

Also, note that I am also redefining SAP dictionary types. Why should I do this ??

One of the main reasons is maintainability.

Within the data dictionary, if a data element is changed, the change propagates through every program that references that data type - as expected if you are using a data dictionary.

In the same manner, by redefining Data dictionary types and then using that user defined type throughout the program, should the type need to be changed for some reason, then it only needs to be changed in one place.

Consider this subroutine:

Code: Select all
Form Get_Data using pu_file type FileName
                    t_Data  type Upload_Table.
*
     Field-Symbols: <f_Fields>.
*
     Data: w_FileName type String,
           w_Msg      type Natxt.
*
     Concatenate 'Uploading '(012)
                 pu_File
            into w_Msg
       separated by ' '.
     Perform Show_Progress using w_Msg 0 0 0.
*
     Move pu_file to w_FileName.
     Zap t_Data.
     Call Function 'GUI_UPLOAD'
       Exporting
         Filename                      = w_FileName
         Filetype                      = 'ASC'
         Has_Field_Separator           = 'X'
       Tables
         Data_Tab                      = t_Data
       Exceptions
         File_Open_Error               = 1
         File_Read_Error               = 2
         No_Batch                      = 3
         Gui_Refuse_Filetransfer       = 4
         Invalid_Type                  = 5
         No_Authority                  = 6
         Unknown_Error                 = 7
         Bad_Data_Format               = 8
         Header_Not_Allowed            = 9
         Separator_Not_Allowed         = 10
         Header_Too_Long               = 11
         Unknown_Dp_Error              = 12
         Access_Denied                 = 13
         Dp_Out_Of_Memory              = 14
         Disk_Full                     = 15
         Dp_Timeout                    = 16
         Others                        = 17.
     If sy-subrc <> 0.
        Message E000 with 'Data could not be loaded'(002).
        Zap t_Data.
     Else.
*
*       Remove blank records....
*
        Loop at t_Data Assigning <f_Fields>.
             If <F_Fields> = ''.
                Delete t_data.
             EndIf.
        EndLoop.
     EndIf.
EndForm.


All it does is to load a file from a presentation server. Using the definition of Upload table above, it will upload a file consisting of 11 fields in the correct manner.

However, change the definition of Upload_Record to:

Code: Select all
Types: Begin of Upload_Record,             " Inbound data
             Lifnr type ULfnr,             " Vendor code
             Bukrs type UBkrs,             " Company code
             Ktokk type UKtkk,             " Vendor Account Group
             Akont type UAknt,             " Recon Account
             Zuawa type UZuawa,            " Sort key
             Zterm type UZtrm,             " payment terms
             RepRf type URpRf,             " Double entry check
             Zwels type UZwels,            " Payment methods
             D(10) type c,                 " Unused date
        End of Upload_Record,


Then without any change to the code a file containing Vendor information is uploaded. How do I implement this type of code ? By modularising my code and using includes.

Regards

Rich

Image
Abap KC
SFMDR
Rich
 
Posts: 6919
Joined: Thu Oct 31, 2002 4:47 pm
Location: Geneva

Return to ABAPers

Who is online

Users browsing this forum: No registered users and 0 guests




This website is not affiliated with, sponsored by, or approved by SAP AG.