Loading Purchase Orders Using BAPI_PO_CREATE

Pls check if this code.  I used this to upload po.

*&---------------------------------------------------------------------*
*& Report  ZBAPI_PO_CRATE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZBAPI_PO_CRATE.

data : PO_HEADER like BAPIEKKOC.
data : PO_HEADER_ADD_DATA like BAPIEKKOA.
data : PO_ITEMS like BAPIEKPOC occurs 0 with header
line.
data : PO_ITEM_SCHEDULES like BAPIEKET occurs 0 with
header line.
data : RETURN like BAPIRETURN occurs 0 with header
line.
DATA : BEGIN OF TAB OCCURS 0,
       PO_NUMBER like BAPIEKKOC-PO_NUMBER,
       DOC_DATE(10),
       DOC_TYPE like BAPIEKKOC-DOC_TYPE,
       CO_CODE like BAPIEKKOC-CO_CODE,
       PURCH_ORG like BAPIEKKOC-PURCH_ORG,
       PUR_GROUP like BAPIEKKOC-PUR_GROUP,
       VENDOR like BAPIEKKOC-VENDOR,
       PMNTTRMS like BAPIEKKOA-PMNTTRMS,
       INCOTERMS1 like BAPIEKKOA-INCOTERMS1,
       INCOTERMS2 like BAPIEKKOA-INCOTERMS2,
       PO_ITEM like  BAPIEKPOC-PO_ITEM,
       MATERIAL like BAPIEKPOC-MATERIAL,
       PUR_MAT like BAPIEKPOC-PUR_MAT,
       ITEM_CAT like BAPIEKPOC-ITEM_CAT,
       PLANT like BAPIEKPOC-PLANT,
       NET_PRICE like BAPIEKPOC-NET_PRICE,
       TAX_CODE like BAPIEKPOC-TAX_CODE,
       DELIV_DATE(10),
       QUANTITY like BAPIEKET-QUANTITY,
     end of tab.

data: itab1 like alsmex_tabline occurs 0 with header
line.
DATA: gd_currentrow type i.
data : PURCHASEORDER like ekko-ebeln.
Data: tot_rec type i,     "Total Records
     gd_update type i,   "Main Table Increement
Counter
     gd_lines type i,    "Success Table increement
Counter
     w_textout like t100-text. "VARIABLE TO GET ERROR
LOG

data : begin of it_success occurs 0,
        PURCHASEORDER LIKE ekko-ebeln,  "PROJECT
      end of it_success.

data : begin of it_error occurs 0,
        pono like  ekko-ebeln,
        err_msg(73) TYPE c,    "TO RETREIVE ERROR
MESSAGES
     end of it_error.
data : pono like  ekko-ebeln.

selection-screen begin of block b1 with frame.
skip 3.
parameter:p_infile like rlgrap-filename obligatory.
skip 3.
selection-screen end  of block b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
  PERFORM value_help.

start-of-selection.

  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_infile
      i_begin_col             = '1'
      i_begin_row             = '2' "Do not require
headings
      i_end_col               = '22'
      i_end_row               = '10000'
    TABLES
      intern                  = itab1
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.
  if sy-subrc <> 0.
    message e010(zz) with text-001. "Problem uploading
Excel Spreadsheet
  endif.

*perform open_group.

  sort itab1 by row col.

* Get first row retrieved
  read table itab1 index 1.

* Set first row retrieved to current row
  gd_currentrow = itab1-row.

  loop at itab1.

* Reset values for next row
    if itab1-row ne gd_currentrow.
      append tab .
      clear tab.
      gd_currentrow = itab1-row.
    endif.

    case itab1-col.

      when '0001'. "PGNAM
        TAB-PO_NUMBER = itab1-value.
      when '0002'. "RONAM
        TAB-DOC_DATE = itab1-value.
      when '0003'. "JVLEVEL
        TAB-DOC_TYPE = itab1-value.
      when '0004'. "FTTYPE
        TAB-CO_CODE = itab1-value.
      when '0005'. "LANG
        TAB-PURCH_ORG =  itab1-value.
      when '0006'. "LTEXT
        TAB-PUR_GROUP = itab1-value.
      when '0007'. "LTEXT
        TAB-VENDOR = itab1-value.
      when '0008'. "LTEXT
        TAB-PMNTTRMS = itab1-value.
      when '0009'. "LTEXT
        TAB-INCOTERMS1 = itab1-value.
      when '0010'. "FTTYPE
        TAB-INCOTERMS2 = itab1-value.
      when '0011'. "FTTYPE
        TAB-PO_ITEM = itab1-value.
      when '0012'. "FTTYPE
        TAB-MATERIAL = itab1-value.
      when '0013'. "FTTYPE
        TAB-PUR_MAT = itab1-value.
      when '0014'. "FTTYPE
        TAB-ITEM_CAT = itab1-value.
      when '0015'. "FTTYPE
        shift itab1-value left deleting leading space.
        TAB-PLANT = itab1-value.
      when '0016'. "FTTYPE
        TAB-NET_PRICE = itab1-value.
      when '0017'. "FTTYPE
        TAB-TAX_CODE = itab1-value.
      when '0018'. "FTTYPE
        TAB-DELIV_DATE = itab1-value.
      when '0019'. "FTTYPE
        TAB-QUANTITY = itab1-value.
    endcase.
  endloop.
  append tab.
  clear tab.

  sort tab by PO_NUMBER.
  DELETE TAB WHERE PO_NUMBER = SPACE.
  loop at tab.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = tab-material
      IMPORTING
        OUTPUT = tab-material.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = tab-PUR_MAT
      IMPORTING
        OUTPUT = tab-PUR_MAT.


    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = tab-vendor
      IMPORTING
        OUTPUT = tab-vendor.


    concatenate tab-doc_DATE+4(4) tab-doc_DATE+2(2)
tab-doc_DATE+0(2)
into tab-doc_DATE.
    concatenate tab-DELIV_DATE+4(4)
tab-DELIV_DATE+2(2)
tab-DELIV_DATE+0(2) into tab-DELIV_DATE.

    pono = tab-PO_NUMBER.
    if tab-po_number = pono.
      PO_HEADER-DOC_DATE = tab-DOC_DATE.
      PO_HEADER-DOC_TYPE = tab-DOC_TYPE.
      PO_HEADER-CO_CODE  = tab-CO_CODE.
      PO_HEADER-PURCH_ORG = tab-PURCH_ORG.
      PO_HEADER-PUR_GROUP = tab-PUR_GROUP.
      PO_HEADER-VENDOR = tab-VENDOR.
      PO_HEADER-PO_NUMBER = tab-PO_NUMBER.

      PO_HEADER_ADD_DATA-PMNTTRMS = tab-PMNTTRMS.
      PO_HEADER_ADD_DATA-INCOTERMS1 = tab-INCOTERMS1.
      PO_HEADER_ADD_DATA-INCOTERMS2 = tab-INCOTERMS2.

      PO_ITEMS-PO_ITEM = tab-PO_ITEM.
      PO_ITEMS-MATERIAL = tab-MATERIAL.
      PO_ITEMS-PUR_MAT = tab-PUR_MAT.
      PO_ITEMS-ITEM_CAT = tab-ITEM_CAT.
      PO_ITEMS-PLANT = tab-PLANT.
      PO_ITEMS-NET_PRICE = tab-NET_PRICE.
      PO_ITEMS-TAX_CODE = tab-TAX_CODE.
      append po_items.

      PO_ITEM_SCHEDULES-PO_ITEM = tab-PO_ITEM.
      PO_ITEM_SCHEDULES-DELIV_DATE = tab-DELIV_DATE.
      PO_ITEM_SCHEDULES-QUANTITY =  tab-QUANTITY.
      append PO_ITEM_SCHEDULES.
    endif.
    at end of PO_NUMBER.
      CALL FUNCTION 'BAPI_PO_CREATE'
        EXPORTING
          PO_HEADER                        = PO_HEADER
         PO_HEADER_ADD_DATA               =
PO_HEADER_ADD_DATA
*           HEADER_ADD_DATA_RELEVANT         =
*           PO_ADDRESS                       =
*           SKIP_ITEMS_WITH_ERROR            = 'X'
*           ITEM_ADD_DATA_RELEVANT           =
*           HEADER_TECH_FIELDS               =
       IMPORTING
         PURCHASEORDER                    =
PURCHASEORDER
        TABLES
          PO_ITEMS                         = PO_ITEMS
*           PO_ITEM_ADD_DATA                 =
          PO_ITEM_SCHEDULES                =
PO_ITEM_SCHEDULES
*           PO_ITEM_ACCOUNT_ASSIGNMENT       =
*           PO_ITEM_TEXT                     =
         RETURN                            = RETURN
*           PO_LIMITS                        =
*           PO_CONTRACT_LIMITS               =
*           PO_SERVICES                      =
*           PO_SRV_ACCASS_VALUES             =
*           PO_SERVICES_TEXT                 =
*           PO_BUSINESS_PARTNER              =
*           EXTENSIONIN                      =
*           POADDRDELIVERY                   =
                .

      if PURCHASEORDER <> space.
.
        commit work.
        ADD 1 TO gd_update.
        it_success-PURCHASEORDER = PURCHASEORDER.
        append it_success.
       
clear:PO_HEADER,PO_HEADER_ADD_DATA,PURCHASEORDER.
        refresh : PO_ITEMS,PO_ITEM_SCHEDULES,RETURN.
      ELSE.
        loop at return.
          it_error-pono = tab-PO_NUMBER.
          it_error-err_msg = return-MESSAGE .
          Append it_error.
        ENDLOOP.
       
clear:PO_HEADER,PO_HEADER_ADD_DATA,PURCHASEORDER.
        refresh : PO_ITEMS,PO_ITEM_SCHEDULES,RETURN.
      endif.
    endat.
  endloop.
  DESCRIBE TABLE it_success LINES gd_lines.
  IF gd_lines GT 0.
*     Display result report column headings
    PERFORM display_column_headings.
*     Display result report
    PERFORM DISPLAY_SUCESS.
  ENDIF.

* IF SUCESS FAILS Display Error Report
  DESCRIBE TABLE it_error LINES gd_lines.
  IF gd_lines GT 0.
    PERFORM errorheadings.
    PERFORM errorreport.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  display_column_headings
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_column_headings.
  WRITE:2 ' Success Report '(014) COLOR COL_POSITIVE.
  SKIP.
  WRITE:2 'The following records inserted
successfully:'(013).
  WRITE:/ sy-uline(15).

  FORMAT COLOR COL_HEADING.
  WRITE:/      sy-vline,
           (10) 'Purchase order'(004), sy-vline.

  WRITE:/ sy-uline(15).
ENDFORM.                    "display_column_headings
*Subroutine to display SUCESS REPORT
FORM DISPLAY_SUCESS.
  FORMAT COLOR COL_NORMAL.
  LOOP AT it_success.
    WRITE:/      sy-vline,
        (10)  it_success-PURCHASEORDER, sy-vline.

    CLEAR it_success.
  ENDLOOP.
  WRITE:/ sy-uline(15).
  REFRESH: it_success.
  FORMAT COLOR COL_BACKGROUND.
ENDFORM.                               "
DISPLAY_REPORT
*&---------------------------------------------------------------------*
*&      Form  errorreport
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM errorreport.
  LOOP AT it_error.
    WRITE:/      sy-vline,
            (10) it_error-pono, sy-vline,
             (40) it_error-err_msg, sy-vline.

  ENDLOOP.
  WRITE:/ sy-uline(104).
  REFRESH: it_error.

endform.                    "errorreport

*&---------------------------------------------------------------------*
*&      Form  ERRORHEADINGS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM ERRORHEADINGS.
  SKIP.
  WRITE:2 ' Error Report '(007) COLOR COL_NEGATIVE.
  SKIP.
  WRITE:2 'The following records failed during
update:'(008).
  WRITE:/ sy-uline(104).
  FORMAT COLOR COL_HEADING.
  WRITE:/      sy-vline,
          (10) 'ERROR.'(009), sy-vline.

  WRITE:/ sy-uline(104).
  FORMAT COLOR COL_NORMAL.
ENDFORM.                    "ERRORHEADINGS
**&---------------------------------------------------------------------
**
**&      Form  value_help
**&---------------------------------------------------------------------
**
**       text
**----------------------------------------------------------------------
**
**  -->  p1        text
**  <--  p2        text
**----------------------------------------------------------------------
**
FORM value_help .
  CALL FUNCTION 'DSVAS_DOC_WS_FILENAME_GET_50'
    EXPORTING
      DEF_FILENAME     = ' '
      DEF_PATH         = ' '
      MASK             = ',*.*,*.*.'
      MODE             = 'O'
      TITLE            = ' '
    IMPORTING
      FILENAME         = p_infile
    EXCEPTIONS
      INV_WINSYS       = 1
      NO_BATCH         = 2
      SELECTION_CANCEL = 3
      SELECTION_ERROR  = 4
      OTHERS           = 5.
  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.                    "value_help

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.