SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Profit of using a field-symbols



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Sep 22, 2007 7:41 pm    Post subject: Profit of using a field-symbols Reply with quote

Author: Richard Harper
http://www.sapfans.com/forums/viewtopic.php?t=33394

A field symbol is something akin to a pointer in C in that once you have assign it the field symbol then 'becomes' the variable that you assign it to.

As to their use, a good example would be a routine that takes an internal table and translates that table to a CSV file or some such.

That's easy if you specify the table in the subroutine. You can then loop round the table and move the individual fields of the table to a variable and then output them.

But.... what if you want to use the code on another table ? You cannot pass the table as a parameter because the definition of the table parameter in the subroutine is different.

Well. You could copy the subroutine and use the second table definition so that's easy enough and not too disgraceful

But hang on..... I've got another job that requires a download to a CSV file.... Oooops - that's copy 3 of essentially the same subroutine.

Using field symbols, you can do all of the above with one subroutine, and have the added bonus that you do not have to change the subroutine for each table you want to download:

Code:
Form Out_Local  Tables t_outtable
                 using pu_file like zfilepath-finalpath
                       pu_sdf  type boolean
              changing pc_status type status_code.
*
     Field-Symbols: <f_field>.
*
     Data: t_csv           like zraw_csv occurs 0 with header line,
           w_csv           like zraw_csv,
           w_lines         type i,
           w_charout(2000) type c,
           w_ftype(1)      type c,
           w_len           type i,
           w_offset        type i.
*
*    Translate the itab to csv format
*
     Loop at t_outtable.
*
*         Take this record and change it to a CSV record.
*
          Do.
              Assign component sy-index
                  of structure t_outtable to <f_field>.
*
*             Last field ?
*
              If sy-subrc <> 0.
                 Exit.
              Else.
                 Write <f_field> to w_charout.
                 Describe field <f_field> type w_ftype.
                 Describe field <f_field> length w_len.
                 If c__numeric cs w_ftype.
                    Perform Format_Number using w_charout
                                       changing w_charout.
                 EndIf.
                 If sy-index = 1.
                    Move w_charout to t_csv-inline.
                    Move w_len to w_offset.
                 Else.
                    If pu_sdf = False.
                       Concatenate t_csv-inline ',' w_charout
                              into t_csv-inline.
                    Else.
                       Move w_charout to t_csv-inline+w_offset.
                       Add w_len to w_offset.
                    EndIf.
                 EndIf.
              EndIf.
          EndDo.

*
*         Write the completed line out...
*
          Append t_csv.
     EndLoop.
     Move pu_file to String_filename.
*
*    Is this an empty file ?
*
     Describe table t_csv lines w_lines.
     If w_lines = 0.
        Move 'NO DATA FOUND' to t_csv-inline.
        Append t_csv.
     EndIf.
     Call Function 'GUI_DOWNLOAD'
       Exporting
         Filename                      = String_Filename
       Tables
         Data_Tab                      = T_CSV
       Exceptions
         File_Write_Error              = c_could_not_create_output
         No_Batch                      = c_no_batch
         Gui_Refuse_Filetransfer       = c_gui_refuse_filetransfer
         Invalid_Type                  = c_invalid_type
         Others                        = c_other_interface_error.
     Move sy-subrc to pc_status.
EndForm.

Another use for field symbols is where you build a list of variables in a table at run time and then wish to access those variables.

This routine is part of docs.zip from the repository. Docs.zip can be downloaded and contains many routines for reporting, creating BDC's and so on. These are resuable routines that do not require any changes what so ever to be used in different programs. The routines access different variables and values in different ways depending on the program calling them.

This routine refreshes the values of any input parameters that are on your input screen without having to press the return key or actually execute the report. Again, the routine knows nothing about the individual parameters on the screen:

Code:
Form Read_Screen_Values.                                 
*                                                       
     Field-Symbols <f_fieldname>.                       
*                                                       
     Data: Begin of t_dynpread occurs 0.                 
                 Include Structure dynpread.             
     Data: End of t_Dynpread.                           
*                                                       
     Zap t_dynpread.                                     
     Loop at Screen.                                     
          If Screen-Group3 = 'PAR'.                     
             Move Screen-Name to t_dynpread-fieldname.   
             Append t_dynpread.                         
          EndIf.                                         
     EndLoop.                                           
     Call Function 'DYNP_VALUES_READ'                   
          Exporting                                     
               Dyname               = syst-cprog         
               Dynumb               = syst-dynnr         
*              Request              = 'A'               
          Tables                                         
               Dynpfields           = t_dynpread         
          Exceptions                                     
               Invalid_Abapworkarea = 0                 
               Invalid_Dynprofield  = 0                 
               Invalid_Dynproname   = 0                 
               Invalid_Dynpronummer = 0                   
               Invalid_Request      = 0                   
               No_Fielddescription  = 0                   
               Invalid_Parameter    = 0                   
               Undefind_Error       = 0                   
               Others               = 0.                   
     Loop at t_dynpread.                                   
          Assign (t_dynpread-fieldname) to <f_fieldname>. 
          Move t_dynpread-fieldvalue to <f_fieldname>.     
     EndLoop.                                             
EndForm.

So, without field symbols, you would have to code this routine for each individual report.

Regards, Rich
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.