Showing posts with label SAP ABAP. Show all posts
Showing posts with label SAP ABAP. Show all posts

Wednesday, January 22, 2020

Downloading a file data from FTP Server to SAP ECC internal table

Downloading a file data from FTP Server to SAP  internal table

SAP provides example reports for connecting to FTP using the function module above. Following report will give you comprehensive example:
  • RSFTP002: Execute FTP Command
  • RSFTP007: Example use of FTP_R3_TO_SERVER and FTP_SERVER_TO_R3


Code : Simply copy and paste the code and run ,if you want to display the record then use alv to display the result in alv re_use_alv display

 

*&---------------------------------------------------------------------*
*& Report  ZHR_ATTENDANCE_UPLOAD_FTP
*&
*&---------------------------------------------------------------------*
*& date : 22/1/2020   technical : Sourav Rai  functional --------
*& Employee attendance upload from FTP for entire group of Companies Emp
*&---------------------------------------------------------------------*
REPORT zhr_attendance_upload_ftp_sr.

************************************************************************
*T Y P E S
************************************************************************
TYPESBEGIN OF stru_ftpdata,
         data1 TYPE char200,
       END OF stru_ftpdata.
TYPES BEGIN OF ty_final,
          emp_code  TYPE char40,
          emp_code1 TYPE char40,
          code      TYPE char100,
          location  TYPE char100,
          timestamp TYPE char40,
          date      TYPE char20,
          time      TYPE char20,
          inout     TYPE char40,
        END OF ty_final.
DATA it_final TYPE TABLE OF ty_final.
DATA wa_final TYPE  ty_final.
DATAit_ftpdata TYPE TABLE OF stru_ftpdata.
DATAw_handle TYPE i.

DATAfilename TYPE char80.

TYPES:
  BEGIN OF x_cmdout,
    line(100TYPE c,
  END OF x_cmdout.

TYPE-POOLS slis.

************************************************************************
*    D A T A
************************************************************************
*data specifications of FTP server

*Handler and Key
DATAw_cmd(40TYPE c,
      w_hdl     TYPE i,
      w_key     TYPE VALUE 26101957,
      w_slen    TYPE i,
      wa_cmdout TYPE x_cmdout,
      it_cmdout TYPE STANDARD TABLE OF x_cmdout.

*Constant declarations
CONSTANTSc_dest     TYPE rfcdes-rfcdest VALUE 'SAPFTP',
           c_host(15TYPE VALUE '199.111.111.11',
           c_ftp(6)   TYPE VALUE 'Mumbai',
           c_sap(16)  TYPE VALUE '/sap/inbound/SAPDIRECTORY'.

************************************************************************
*    P A R A M E T E R S
************************************************************************

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERSp_user(30TYPE LOWER CASE,
            p_pwd(30)  TYPE LOWER CASE,
            p_host(64TYPE LOWER CASE DEFAULT c_host,
            p_ftp      TYPE e_dexcommfilepath LOWER CASE DEFAULT c_ftp,
            p_sap      TYPE esefilepath LOWER CASE DEFAULT c_sap.
SELECTION-SCREEN END OF BLOCK b1.

************************************************************************
*AT SELECTION SCREEN Events
************************************************************************

AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.
    CASE screen-name.
      WHEN 'P_PWD'."Set the password field as invisible
        screen-invisible '1'.
        MODIFY SCREEN.
    ENDCASE.
  ENDLOOP.

************************************************************************
*    S T A R T - O F - S E L E C T I O N
************************************************************************

START-OF-SELECTION.

*Connect to the FTP server.
  PERFORM ftp_connect.

*Find all files in the directory and store inside the Log table
  PERFORM log_files.

*Change the local save directory and download to SAP application server.
  PERFORM move_files.

*Close the connection
  PERFORM close_ftp.

************************************************************************
*END-OF-SELECTION
************************************************************************
END-OF-SELECTION.

*Display report.
  PERFORM disp_res.

*&---------------------------------------------------------------------
*& Form FTP_CONNECT
*&---------------------------------------------------------------------
*Make a connection to the FTP server
*----------------------------------------------------------------------
FORM ftp_connect .

  SET EXTENDED CHECK OFF.
  w_slen strlenp_pwd ).

  CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      source      p_pwd
      sourcelen   w_slen
      key         w_key
    IMPORTING
      destination p_pwd.

  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user            p_user
      password        p_pwd
      host            p_host
      rfc_destination c_dest
    IMPORTING
      handle          w_hdl
    EXCEPTIONS
      not_connected   1
      OTHERS          2.
  IF sy-subrc <> 0.
*Message will arise in case of any issues in connecting to the FTP server.
    MESSAGE text-e01 TYPE 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.

ENDFORM" FTP_CONNECT
*&---------------------------------------------------------------------
*& Form LOG_FILES
*&---------------------------------------------------------------------
*find all the files in the home directory
*----------------------------------------------------------------------
FORM log_files .

*Change directory to the FTP directory for LUPIN
  CONCATENATE 'cd' p_ftp INTO w_cmd SEPARATED BY space.

  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        w_hdl
      command       w_cmd
      compress      'N'
    TABLES
      data          it_cmdout
    EXCEPTIONS
      tcpip_error   1
      command_error 2
      data_error    3
      OTHERS        4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  REFRESH it_cmdout.
  CLEAR w_cmd.
  w_cmd 'ls'.

  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        w_hdl
      command       w_cmd
      compress      'N'
    TABLES
      data          it_cmdout
    EXCEPTIONS
      tcpip_error   1
      command_error 2
      data_error    3
      OTHERS        4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CALL FUNCTION 'FTP_SERVER_TO_R3'
    EXPORTING
      handle         w_hdl
      fname          filename  " i hardcorded the file name as for testing
      character_mode 'X'
* IMPORTING
*     BLOB_LENGTH    =
    TABLES
*     BLOB           =
      text           it_ftpdata
    EXCEPTIONS
      tcpip_error    1
      command_error  2
      data_error     3
      OTHERS         4.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.


  FIELD-SYMBOLS <fs_ftpdata> LIKE LINE OF it_ftpdata .

 
  LOOP AT it_ftpdata ASSIGNING <fs_ftpdata>.

    SPLIT <fs_ftpdata>-data1  AT ',' INTO wa_final-emp_code
                                          wa_final-emp_code1
                                          wa_final-code
                                          wa_final-location
                                          wa_final-timestamp
                                          wa_final-inout.

    Split wa_final-timestamp at ' ' INTO wa_final-date
                                         wa_final-time.

    APPEND wa_final TO it_final.
    CLEAR wa_final.
  ENDLOOP.



  REFRESH it_cmdout.

ENDFORM" LOG_FILES
*&---------------------------------------------------------------------
*& Form MOVE_FILES
*&---------------------------------------------------------------------
FORM move_files .

ENDFORM" MOVE_FILES
*&---------------------------------------------------------------------
*& Form CLOSE_FTP
*&---------------------------------------------------------------------
FORM close_ftp .

  CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
      handle w_hdl.

  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination c_dest
    EXCEPTIONS
      OTHERS      1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM" CLOSE_FTP
*&---------------------------------------------------------------------
*& Form DISP_RES
*&---------------------------------------------------------------------
*Display the output
*----------------------------------------------------------------------
FORM disp_res .

*  DATA : l_layout TYPE slis_layout_alv.
*
*  l_layout-colwidth_optimize = 'X'.
*
**Display table contents of updated files
*  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
*    EXPORTING
*      i_callback_program = sy-repid
*      i_structure_name   = 'ZTBL_IPL_FTP'
*      is_layout          = l_layout
*    TABLES
*      t_outtab           = it_iplftp
*    EXCEPTIONS
*      program_error      = 1
*      OTHERS             = 2.
*  IF sy-subrc <> 0.
*    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
*  ENDIF.

ENDFORM" DISP_RES



Wednesday, January 8, 2020

Transferring Asset from one plant to another plant in same Company Code

     Transferring Asset from one plant to another plant in Company Code:

 We faced an issue that one Asset was in one plant and it was to be transferred to another plant ,the way to transfer asset as non-valuted material and send it via performa invoice and retire from one plant .

The easy solution was to change master data of asset change its cost center and plant and location 
then it will be moved to another plant and then we can easily execute normal depreciation.
then create a manual challan.
applicable for india .

or create plant as customer then sell it.


 

Saturday, December 21, 2019

Deadline Monitoring in SAP Workflow (Working with Latest end)

Deadline Monitoring in SAP Workflow (Working with Latest end)

There  are different deadlines that can be monitored against each workflow step:

  • Requested Start
  • Latest Start
  • Requested End
  • Latest End

    STEP1 : Go  to T-Code SWDD , Click on Create 


    Then Click on USER-DECISION 


    A prompt will appear then enter the text and action required 
     If want to  enable Latest End,
    By default, No deadline monitoring is active  
    Choose the “Work Item Creation” from the list box.

    \


    Enter recipient details to whom the message to be escalated, if the work item is not executed with in 3 minutes after work item creation.



 Save and activate the workflow definition.

Tuesday, December 10, 2019

Number of ways to display the messages in SAP

Number of ways to display the messages in SAP

Number of ways to display the messages.
1). MESSAGE 'abs' type 'I'.
2). MESSAGE I002 (<Message Class>)
            In the message class we have to write the corresponding message for 002.
3). REPORT <Program Name> MESSAGE-ID <Message Class>.
        MESSAGE I002.
            In the message class we have to write the corresponding message for 002.
4). MESSAGE i006 WITH text-003.
                    In the text symbols 003 we have to write the corresponding message.
5). MESSAGE text-001 type 'I'.
                        In the text symbols 001 we have to write the corresponding message.
6). MESSAGE i006 WITH 'Invalid purchase order Number'(003).
       006 message description is sending at runtime, in the message class we mention & & & & for the corresponding 006 number. In the text symbols 003 we are writing the description, it    just for our display purpose.
7). MESSAGE ID '<Message Class>' type 'I' NUMBER 002.
8). MESSAGE i003 (<Message Class>) WITH '<Some text message> '.
9). MESSAGE i003 (<Message Class>) WITH text-003.
                        In the text symbols 003 we have to write the corresponding message.
10). MESSAGE i003 (<Message Class>) with p_user.

Simplest way to use Message Class and Concatenate with Some Variable in SAP Report:
I have used & Symbol to Concatenate variable with Message so that while displaying the message   we can replace & value with some particular value.





Wednesday, November 6, 2019

Hierarchical Alv Tree Based Report (upto 6 levels)

                Hierarchical Alv Tree Based Report (upto 6 levels and still expandable ):
For Hierarchical ALV, we do not have much examples available on sap sdn or google or any other sites.I was  given a task to develop a report in which entire sales hierarchy target vs achievement report  month wise should come.
So that the  management could track which part of thier sale's team is not working well and where they could force and boost the performance , We did not HR module implemented while developing this report , so it was difficult for me to design the entire  system.

Concept Behind : Developer who knows the concept of Linked List, can easily solve this problem,if you look at problem , at start even i was confused how to start i tried each and every site of SAP SDN, SAP TECHNICAL , Google  found there was no  exact solution  to this kind of Required.
I was getting the  few example but the restriction was of 3 levels or upto 9 Column, I had to show 30 Columns Results.
Input Screen :

 1st Level Tree :
 3rd Level Extended:
It can further be extended if required as it just node mapping and node leveling and linking of Node to one another and setting them in a particular level.
if you want code kindly mail @ programmer2117@gmail.com