Monday, September 12, 2011

ABAP Field Symbol Usage


Here is a short sample which illustrates the usage of field symbols with work areas (headers) . Even though we don't know the number or the name of elements in a work area , we can dynamically get them like below. Field symbols are indispensable for complicated applications or reports. Also they could provide a solution for performance issues in some cases. Beside the usage below there are much more ways to use it. For more information just check out the documentation in sdn.




FIELD-SYMBOLS  TYPE ANY ,
                 TYPE ANY .

DATA t_table TYPE TABLE OF pa0001 WITH HEADER LINE.

SELECT FROM pa0001 INTO TABLE t_table UP TO 100 ROWS.

LOOP AT t_table ASSIGNING  .
WRITE /.
  " Loop at fields
  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE  TO .
    IF sy-subrc EQ 0.
      WRITE .
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.

ENDLOOP.

Basic ABAP HTTP Request Sample


For various needs using http requests could be a life saver. Here is a simple code snippet.




  data : lv_value type string.

  data : http_client type ref to if_http_client .
  data : lv_url type string.
  data : return type string.

    data : lv_err_string type string ,
           lv_ret_code type sy-subrc .


* Build Url

  concatenate 'http://www.example.com/?param='
               lv_value
               into lv_url.

* Create Client 
  call method cl_http_client=>create_by_url
    exporting
      url    = lv_url
    importing
      client = http_client.

* Send 
  call method http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2.

* Receive
  call method http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  if sy-subrc ne 0.
    http_client->response->get_status(
      importing
        code   = lv_ret_code
        reason = lv_err_string
           ).
    message lv_err_string type 'I'.
  endif.

* Now we have the response , parse , display  
* do what you like 
  return = http_client->response->get_cdata( ).