Friday, September 25, 2020

BAPI PO CREATE Example in SAP ABAP.

BAPI PO CREATE Example in SAP ABAP. 




BAPI PO CREATE.

REPORT zbapi_po_creation.

include zbapi_po_creation_top. " top include for global data declration

include zbapi_po_creation_sel. " sel include for selecation

include zbapi_po_creation_sub. " f01 subrotine programs

*----------------------------------------------*
* I N I T I A L I Z A T I O N                  *
*----------------------------------------------*
INITIALIZATION.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

PERFORM f4_file_name.

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

PERFORM data_process.      " CONVERTING DATA FROM EXCEL TO INTERNAL TABLE
*PERFORM uplode_data.
*----------------------------------------------*
* E N D - O F - S E  L E C T I O N             *
*----------------------------------------------*
END-OF-SELECTION.

CLEAR gcl_app_log.
*****Application Log
gcl_app_log =  zca_cl_application_log=>get_instance( ).

CALL METHOD gcl_app_log->create_app_log_handle(
EXPORTING
  i_object     = 'ZRKPANDEY'         " Object In SLGO
  i_subobject  = 'ZRKSUB'     " Sub Object In SLGO
  i_tcode      = sy-tcode
  i_repid      = sy-cprog
  RECEIVING
  r_log_handle = gs_log_handle ).
  PERFORM uplode_data.
************************* Save and Display Application Log *************************
CALL METHOD gcl_app_log->save_and_display_app_log
EXPORTING
  i_log_handle = gs_log_handle
  i_flg_save   = abap_false.

*Text elements
*----------------------------------------------------------
* T01 select flat file


*Selection texts
*----------------------------------------------------------
* P_FILE D       .
=============================================================================================
Code listing for: ZBAPI_PO_CREATION_TOP
Description: Include ZBAPI_PO_CREATION_TOP
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_PO_CREATION_TOP
*&---------------------------------------------------------------------*
TYPES: BEGIN OF  ty_file,
  vendor      TYPE  elifn,   "   vendro
  purch_org   TYPE  ekorg,   "  puraches orgnization
  pur_group   TYPE  bkgrp,   "  purache groups
  co_code     TYPE  bukrs,   "  comapny code
  po_item     TYPE  ebelp,   " po item
  material    TYPE  matnr,   " material number
  short_text  TYPE  txz01,   " short text
  QUANTITY    type  BSTMG, " po quanity
  NET_PRICE	 TYPE	  BAPICUREXT,    " net price
  plant	     TYPE	  EWERK,        " net price
END OF ty_file.

DATA: po_number LIKE  bapimepoheader-po_number.

***** Internal Table
DATA : gt_final  TYPE STANDARD TABLE OF ty_file,
       gs_final  TYPE                   ty_file.
*       gt_return TYPE STANDARD TABLE OF bapireturn.

DATA : gt_return TYPE STANDARD TABLE OF bapiret2,
       gs_return TYPE bapiret2.


DATA : gt_type  TYPE truxs_t_text_data.


DATA: gt_po_items TYPE TABLE OF   bapimepoitem,
      gt_poitemx      TYPE TABLE OF bapimepoitemx,
      gs_poitemx      TYPE  bapimepoitemx,
      wa_po_items   LIKE bapimepoitem,
      return  TYPE  bapireturn,
      po_header TYPE TABLE OF  bapimepoheader,
      wa_poheaderx TYPE   bapimepoheaderx,
      wa_po_header TYPE  bapimepoheader.

* passing constan for values
DATA : gc_x TYPE c VALUE 'X'.

***** Declaration for application log
DATA: gt_return_log TYPE bapiret2_t.
****************** Class/Objects***************************
DATA: gcl_app_log   TYPE REF TO zca_cl_application_log,
****************** Internal Structure *********************
      gs_log_handle TYPE balloghndl.

"Local table
DATA: gt_return1 TYPE STANDARD TABLE OF bapiret2,
      gs_return1 TYPE bapiret2.

DATA: bapiret2 TYPE STANDARD TABLE OF bapiret2.

============================================================================================
Code listing for: ZBAPI_PO_CREATION_SEL
Description: Include ZBAPI_PO_CREATION_SEL
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_PO_CREATION_SEL
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE TEXT-t01.

  PARAMETER p_file TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK a1.
============================================================================================
Code listing for: ZBAPI_PO_CREATION_SUB
Description: Include ZBAPI_PO_CREATION_SUB
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_PO_CREATION_SUB
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  UPLODE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM uplode_data .
  DATA : lv_log(100) TYPE c.
***** Uploading the data into the database table
  READ TABLE gt_final INTO gs_final INDEX 1.
* header data in po creation
    wa_po_header-vendor     = gs_final-vendor.
    wa_po_header-purch_org  = gs_final-purch_org.
    PERFORM f_converstion_exit.
    wa_po_header-pur_group  = gs_final-pur_group.
    wa_po_header-comp_code    = gs_final-co_code.
* passing x  values in po item
    wa_poheaderx-vendor    = gc_x.
    wa_poheaderx-purch_org = gc_x.
    wa_poheaderx-pur_group = gc_x.
    wa_poheaderx-comp_code   = gc_x.
* item data move
 LOOP AT gt_final INTO gs_final.
    wa_po_items-po_item    = gs_final-po_item.
    wa_po_items-material   = gs_final-material.
    wa_po_items-short_text = gs_final-short_text.
    wa_po_items-quantity  = gs_final-quantity.
    wa_po_items-net_price  = gs_final-net_price.
    wa_po_items-plant      = gs_final-plant.

* passing x value in po item
    gs_poitemx-po_item = gs_final-po_item.
    gs_poitemx-po_itemx   = gc_x.
    gs_poitemx-material   = gc_x.
    gs_poitemx-short_text = gc_x.
    gs_poitemx-quantity  = gc_x.
    gs_poitemx-po_price  = gc_x.
    gs_poitemx-plant      = gc_x.
    APPEND gs_poitemx TO gt_poitemx.
    APPEND wa_po_items TO gt_po_items.
ENDLOOP.
    CALL FUNCTION 'BAPI_PO_CREATE1'
      EXPORTING
        poheader         = wa_po_header
        poheaderx        = wa_poheaderx
      IMPORTING
        exppurchaseorder = po_number
      TABLES
        return           = gt_return1
        poitem           = gt_po_items
        poitemx          = gt_poitemx.
    IF gt_return1[] IS NOT INITIAL.
      LOOP AT gt_return1 INTO gs_return1. "data(gs_return1).
        gs_return-type       = gs_return1-type.
        gs_return-id         = gs_return1-id.
        CONCATENATE po_number gs_return1-message
        INTO lv_log SEPARATED BY ' - '.
        gs_return-message_v1 = lv_log.
        APPEND gs_return TO gt_return.
        CLEAR : gs_return1.
      ENDLOOP.
    ENDIF.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
  CHECK sy-subrc EQ 0.
  CALL METHOD gcl_app_log->add_message_to_app_log
    EXPORTING
      i_log_handle = gs_log_handle
      i_tab_return = gt_return.
ENDFORM.                    " UPLODE_DATA
*&---------------------------------------------------------------------*
*&      Form  F4_FILE_NAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f4_file_name .
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'P_FILE'
    IMPORTING
      file_name  = p_file.

ENDFORM.                    " F4_FILE_NAME
*&---------------------------------------------------------------------*
*&      Form  DATA_PROCESS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM data_process .
****** Uploading the data in the file into internal table

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_field_seperator    = 'X'
      i_line_header        = 'X'
      i_tab_raw_data       = gt_type
      i_filename           = p_file
    TABLES
      i_tab_converted_data = gt_final[]
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc NE 0.
    MESSAGE ID sy-msgid
    TYPE sy-msgty
    NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.                    " DATA_PROCESS
*&---------------------------------------------------------------------*
*&      Form  F_CONVERSTION_EXIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_converstion_exit .
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = gs_final-pur_group
    IMPORTING
      output = gs_final-pur_group.

ENDFORM.                    " F_CONVERSTION_EXIT
===============================================

No comments:

Post a Comment