Wednesday, December 4, 2013

CREATE XML FILE and SEND TO FTP SERVER


REPORT ZXML_FILE .

type-pools: ixml.
tables: vbak.

types: begin of ty_vbak,
       vbeln type vbak-vbeln,
       auart type vbak-auart,
      end of ty_vbak.

data: ty_ftp type standard table of zftpinfo,
      wa_ftp type zftpi,
      l_slen type i,
      lv_user(30) type c,
      lv_pwd(30) type c,
      c_key type i value 26101957.

data: begin of t_title occurs 0,
      title(4000) type c,
      end of t_title.
data: temp_file(200) type c.
data: to_file type char30000.
data: c_gw type string.
data: mi_handle type i.

data: t_vbak type ty_vbak occurs 0 with header line.

selection-screen begin of block b1 with frame title text-001.
select-options: so_num for vbak-vbeln.
selection-screen end of block b1.

*START OF SELECTION
start-of-selection.

select
  vbeln auart
  from vbak into corresponding fields of table t_vbak
  where vbeln in so_num.

"" FOR GETTING FTP SEVER CONNECTION INFO FROM MAINTAING TABLE
select * from zftpinfo client specified
    into corresponding fields of table ty_ftp
    where mandt = sy-mandt
    and   zcountry = 'BD'.

  clear wa_ftp.
  read table ty_ftp into wa_ftp index 1.

*----CONNECTING TO FTP SERVER.
  perform ftp_connect using wa_ftp-zuser wa_ftp-zpasswd wa_ftp-zhost.

*----CHECKING SUCCESSFUL FTP CONNECTION
  check sy-subrc = 0.

    if t_vbak[] is not initial.
    perform c_xml. " FOR CREATING XML FILE
    endif.

form c_xml.

  clear t_title[].
 "" SERVER DESTINATION FILE PATH EX: /.../.../.../
  concatenate 'SERVER FILE PATH' 'SOINFO' '.xml'  into to_file.

*----BUILDING THE XML FILE
  t_title-title = '<?xml version="1.0" encoding="UTF-8"?>'.
  append t_title.
  t_title-title = '<n1:SalesOderInfo xmlns:n1="SalesOderInfo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'.
  append t_title.

  loop at t_vbak.
    concatenate '<SONUM>' t_vbak-vbeln '</SONUM>' into temp_file.
    t_title-title = temp_file.
    append t_title.
    clear temp_file.

    concatenate '<DOCTYPE>' t_vbak-auart '</DOCTYPE>' into temp_file.
    t_title-title = temp_file.
    append t_title.
    clear temp_file.

    endloop.

    t_title-title = '</n1:SalesOderInfo>'.
    append t_title.

  perform r3_ftp using to_file.

endform.



form r3_ftp using to_file.
  call function 'FTP_R3_TO_SERVER'
    exporting
      handle         = mi_handle
      fname          = to_file          "file path of destination system
      character_mode = 'X'
    tables
      text           = t_title
    exceptions
      tcpip_error    = 1
      command_error  = 2
      data_error     = 3
      others         = 4.
endform.

form ftp_connect using lv_user lv_pwd lv_host.

  l_slen = strlen( lv_pwd ).

  call function 'HTTP_SCRAMBLE'
    exporting
      source      = lv_pwd
      sourcelen   = l_slen
      key         = c_key
    importing
      destination = lv_pwd.

  call function 'FTP_CONNECT'
    exporting
      user            = lv_user "Your SAP-UNIX FTP user name (case sensitive)
      password        = lv_pwd  "Your SAP-UNIX server host name (case sensitive)
      host            = lv_host
      rfc_destination = 'SAPFTPA'
    importing
      handle          = mi_handle
    exceptions
      not_connected   = 1
      others          = 2.

endform.

Sunday, December 1, 2013

Simple Code to consume Web service using SAP ABAP

REPORT  zpw_webservice.

*&---------------------------------------------------------------------*
*&      Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS : p_cnt TYPE t005t-landx .

*&---------------------------------------------------------------------*
*&      Types and Data
*&---------------------------------------------------------------------*
DATA:  http_client    TYPE REF TO if_http_client ,
       http_url       TYPE string                ,
       p_content      TYPE string                .

*&---------------------------------------------------------------------*
*&      Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .

* Build the url string based on input
  CONCATENATE 'http://www.webservicex.net/globalweather.asmx'
  '/GetCitiesByCountry?CountryName='
  p_cnt
  INTO http_url .

* Creation of new IF_HTTP_Client object
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = http_url
    IMPORTING
      client             = http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.

  http_client->request->set_header_field( name  = '~request_method'
  value = 'GET' ).
* Send the request
  http_client->send( ).

* Reterive the result
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.

  p_content = http_client->response->get_cdata( ).
  REPLACE  ALL OCCURRENCES OF '&lt;' IN p_content WITH '<' .
  REPLACE  ALL OCCURRENCES OF '&gt;' IN p_content WITH '>' .

*&---------------------------------------------------------------------*
*&      Processing string
*&---------------------------------------------------------------------*

  DATA : moff  TYPE syst-tabix ,
         moff1 TYPE syst-tabix ,
         len   TYPE syst-tabix .

  DO .
    FIND '<City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
    IF sy-subrc = 0 .
      moff = moff + 6 .
      FIND '</City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
      len = moff1 - moff .
      WRITE : / p_content+moff(len) .
    ELSE.
      EXIT.
    ENDIF.
  ENDDO .

In this code processing of XML string is done using FIND statement. SAP has introduced command CALL TRANSFORMATION since Basis release 6.10 for XML processing. This command CALL TRANSFORMATION process the whole XML string based on defined transformation and return it in variable/internal table. Transformations can be defined using transaction SE80. Ok I am leaving this topic for next blog ;)



 

Simple Code to consume Web service using SAP ABAP

REPORT  zpw_webservice.
 
*&---------------------------------------------------------------------*
*&      Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS : p_cnt TYPE t005t-landx .
 
*&---------------------------------------------------------------------*
*&      Types and Data
*&---------------------------------------------------------------------*
DATA:  http_client    TYPE REF TO if_http_client ,
       http_url       TYPE string                ,
       p_content      TYPE string                .
 
*&---------------------------------------------------------------------*
*&      Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .
 
* Build the url string based on input
  '/GetCitiesByCountry?CountryName='
  p_cnt
  INTO http_url .
 
* Creation of new IF_HTTP_Client object
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = http_url
    IMPORTING
      client             = http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
 
  http_client->request->set_header_field( name  = '~request_method'
  value = 'GET' ).
* Send the request
  http_client->send( ).
 
* Reterive the result
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.
 
  p_content = http_client->response->get_cdata( ).
  REPLACE  ALL OCCURRENCES OF '&lt;' IN p_content WITH '<' .
  REPLACE  ALL OCCURRENCES OF '&gt;' IN p_content WITH '>' .
 
*&---------------------------------------------------------------------*
*&      Processing string
*&---------------------------------------------------------------------*
 
  DATA : moff  TYPE syst-tabix ,
         moff1 TYPE syst-tabix ,
         len   TYPE syst-tabix .
 
  DO .
    FIND '<City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
    IF sy-subrc = 0 .
      moff = moff + 6 .
      FIND '</City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
      len = moff1 - moff .
      WRITE : / p_content+moff(len) .
    ELSE.
      EXIT.
    ENDIF.
  ENDDO .

In this code processing of XML string is done using FIND statement. SAP has introduced command CALL TRANSFORMATION since Basis release 6.10 for XML processing. This command CALL TRANSFORMATION process the whole XML string based on defined transformation and return it in variable/internal table. Transformations can be defined using transaction SE80. Ok I am leaving this topic for next blog ;)