Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

BAPI_PO_CHANGE

Former Member
0 Likes
1,898

Hello

I am trying to update the NETPR field of PO with the help of mentioned BAPI  "BAPI_PO_CHANGE". The  changes in the Netprices are not talking place

The Code flow is as below, also i want to restrict the changes for the person who is not having authorisation to change the price

Please help

   REPORT  zpo_update MESSAGE-ID 00.
TYPE-POOLS: abap.

*&---------------------------------------------------------------------*
*& TYPES
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ty_data,
        ebeln TYPE ekpo-ebeln,
        ebelp TYPE ekpo-ebelp,
        netpr TYPE ekpo-netpr,
      END OF ty_data.

TYPES: BEGIN OF ty_ekpo,
        ebeln TYPE ekpo-ebeln,
        ebelp TYPE ekpo-ebelp,
        END OF ty_ekpo.

TYPES: BEGIN OF ty_ekko,
       ebeln TYPE ekko-ebeln,
       END OF ty_ekko.


TYPES: BEGIN OF ty_error,
       ebeln TYPE ebeln,
       ebelp TYPE ebelp,
       error TYPE string,
      END OF ty_error.
*&---------------------------------------------------------------------*
*& DATA
*&---------------------------------------------------------------------*

DATA: gtb_data TYPE TABLE OF  ty_data     INITIAL SIZE 0 WITH HEADER LINE,
      gtb_ekpo TYPE TABLE OF  ty_ekpo     INITIAL SIZE 0 WITH HEADER LINE,
      gtb_ekko TYPE TABLE OF  ty_ekko     INITIAL SIZE 0 WITH HEADER LINE,
      gtb_error TYPE TABLE OF ty_error    INITIAL SIZE 0 WITH HEADER LINE.



DATA : gwa_po_header   TYPE bapimepoheader,
       gwa_po_headerx  TYPE bapimepoheaderx,
       gwa_po_item     TYPE bapimepoitem    OCCURS 0 WITH HEADER LINE,
       gwa_po_itemx    TYPE bapimepoitemx   OCCURS 0 WITH HEADER LINE,
       gwa_bapi_return TYPE bapiret2        OCCURS 0 WITH HEADER LINE.

*&---------------------------------------------------------------------*
*& CONSTANT
*&---------------------------------------------------------------------*

CONSTANTS : gco_mask(20) TYPE c VALUE ',*.txt  ,*.txt.'.
*----------------------------------------------------------------------*
* SELECTION SCREEN
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blck WITH FRAME TITLE text-001.

PARAMETERS:p_file TYPE ibipparms-path OBLIGATORY.

SELECTION-SCREEN END OF BLOCK blck.

*----------------------------------------------------------------------*
* AT SELECTION SCREEN ON VALUE REQUEST
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*** Get the file from which data needs to be uploaded
  PERFORM sub_read_data_file.

*----------------------------------------------------------------------*
* AT SELECTION SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* Subroutine to check for text file
  PERFORM sub_check_file_type.
* Subroutine to check for correct file
  PERFORM sub_validate_file.

*----------------------------------------------------------------------*
* START OF SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
* Subroutine to upload the data file
  PERFORM sub_upload_file.

* Subroutine to check uploaded data.
  PERFORM sub_check_upload_data.

* populate data and call bapi
  PERFORM sub_update_mm.

*&---------------------------------------------------------------------*
*&       ROUTINES                                                      *
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  SUB_READ_DATA_FILE
*&---------------------------------------------------------------------*

FORM sub_read_data_file .
  DATA : lva_file(128) TYPE c.

  lva_file = p_file.

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = ''
      def_path         = lva_file
      mask             = gco_mask
      mode             = 'O'
      title            = 'PO Price Update'                  "#EC *
    IMPORTING
      filename         = lva_file
    EXCEPTIONS
      inv_winsys       = 01
      no_batch         = 02
      selection_cancel = 03
      selection_error  = 04.
  IF sy-subrc NE 0.
*    MESSAGE e001(00) WITH 'File Cannot Be Opened'.
  ELSE.
    p_file = lva_file.
  ENDIF.

ENDFORM.                    " SUB_READ_DATA_FILE
*&---------------------------------------------------------------------*
*&      Form  SUB_CHECK_FILE_TYPE
*&---------------------------------------------------------------------*

FORM sub_check_file_type .

  DATA: lva_length TYPE i.
  DATA: lva_ext(4).


  lva_length = STRLEN( p_file ).
  lva_length = lva_length - 4.

  lva_ext = p_file+lva_length(4).
  TRANSLATE lva_ext TO UPPER CASE.

  IF lva_ext NE '.TXT'.
    MESSAGE e001 WITH 'Please choose a text file'.          "#EC *
  ENDIF.

ENDFORM.                    " SUB_CHECK_FILE_TYPE

*&---------------------------------------------------------------------*
*&      Form  SUB_VALIDATE_FILE
*&---------------------------------------------------------------------*

FORM sub_validate_file .

  DATA : lva_result  TYPE abap_bool.
  DATA : lva_file    TYPE string.

  lva_file = p_file.
  CALL METHOD cl_gui_frontend_services=>file_exist
    EXPORTING
      file                 = lva_file
    RECEIVING
      result               = lva_result
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF sy-subrc NE 0.
    MESSAGE e001 WITH 'Invalid file name'.                  "#EC *
  ELSE.
    CHECK lva_result EQ abap_false.
    MESSAGE e001 WITH 'Invalid file name'.                  "#EC *
  ENDIF.

ENDFORM.                    " SUB_VALIDATE_FILE
*&---------------------------------------------------------------------*
*&      Form  SUB_UPLOAD_FILE
*&---------------------------------------------------------------------*

FORM sub_upload_file .
  DATA : lwa_ekko TYPE ty_ekko.
  DATA : lva_file TYPE string.
  FIELD-SYMBOLS: <fs_mat> TYPE ty_data.

  lva_file = p_file.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = lva_file
      filetype                = 'ASC'
      has_field_separator     = 'X'
    TABLES
      data_tab                = gtb_data
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc NE 0.

    MESSAGE i001 WITH 'Error in uploading file'.            "#EC *
    LEAVE LIST-PROCESSING.

  ELSE.

    MESSAGE s001 WITH 'File uploaded successfully'.         "#EC *


* Check For Data Conversion, i.e to match the data format of the
* incomming material from the file with SAP.

    LOOP AT gtb_data ASSIGNING <fs_mat>.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input        = <fs_mat>-ebeln
        IMPORTING
          output       = <fs_mat>-ebeln
        EXCEPTIONS
          length_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.
    ENDLOOP.
  ENDIF.

ENDFORM.                    " SUB_UPLOAD_FILE


*&---------------------------------------------------------------------*
*&      Form  SUB_CHECK_UPLOAD_DATA
*&---------------------------------------------------------------------*

FORM sub_check_upload_data .

  SELECT ebeln
    FROM ekko
    INTO TABLE gtb_ekko
    FOR ALL ENTRIES IN gtb_data
    WHERE ebeln = gtb_data-ebeln.
  IF sy-subrc NE 0.
    MESSAGE i001 WITH 'No valid PO Number is uploaded'.     "#EC *
    LEAVE LIST-PROCESSING.
  ELSE.
    SORT gtb_ekko BY ebeln.
  ENDIF.


  SELECT ebeln
         ebelp
    FROM ekpo
    INTO TABLE gtb_ekpo
    FOR ALL ENTRIES IN gtb_data
    WHERE ebeln gtb_data-ebeln
    AND ebelp = gtb_data-ebelp.
  IF sy-subrc NE 0.
    MESSAGE i001 WITH 'No valid PO Number and postion number is uploaded'. "#EC *
    LEAVE LIST-PROCESSING.
  ELSE.
    SORT gtb_ekpo BY ebeln ebelp.
  ENDIF.

ENDFORM.                    "SUB_CHECK_UPLOAD_DATA
*&---------------------------------------------------------------------*
*&      Form  SUB_UPDATE_MM
*&---------------------------------------------------------------------*

FORM sub_update_mm .
*  BREAK-POINT.

  DATA : lwa_data  TYPE ty_data.
  DATA : lwa_ekko  TYPE ty_ekko.
  DATA : lwa_ekpo  TYPE ty_ekpo.
  DATA : lwa_error TYPE ty_error.
  FIELD-SYMBOLS  <lfs_data>  TYPE ty_data.

  LOOP AT gtb_data ASSIGNING <lfs_data> .

    gwa_po_header-po_number   = <lfs_data>-ebeln.
    gwa_po_item-po_item       = <lfs_data>-ebelp.
    gwa_po_item-net_price     = <lfs_data>-netpr.
    gwa_po_headerx-po_number  = 'X'.
    gwa_po_itemx-po_item      = 'X'.
    gwa_po_itemx-net_price    = 'X'.

    CALL FUNCTION 'BAPI_PO_CHANGE'
      EXPORTING
        purchaseorder                 = gwa_po_header-po_number
        poheader                      = gwa_po_header
        poheaderx                     = gwa_po_headerx
*       POADDRVENDOR                 =
*       TESTRUN                      =
*       MEMORY_UNCOMPLETE            =
*       MEMORY_COMPLETE              =
*       POEXPIMPHEADER               =
*       POEXPIMPHEADERX              =
*       VERSIONS                     =
*       NO_MESSAGING                 =
*       NO_MESSAGE_REQ               =
*       NO_AUTHORITY                 =
*       NO_PRICE_FROM_PO             =
*       PARK_UNCOMPLETE              =
*       PARK_COMPLETE                =
*     IMPORTING
*       EXPHEADER                    =
*       EXPPOEXPIMPHEADER            =
     TABLES
       return                        = gwa_bapi_return
       poitem                        = gwa_po_item
       poitemx                       = gwa_po_itemx
*       POADDRDELIVERY               =
*       POSCHEDULE                   =
*       POSCHEDULEX                  =
*       POACCOUNT                    =
*       POACCOUNTPROFITSEGMENT       =
*       POACCOUNTX                   =
*       POCONDHEADER                 =
*       POCONDHEADERX                =
*       POCOND                       =
*       POCONDX                      =
*       POLIMITS                     =
*       POCONTRACTLIMITS             =
*       POSERVICES                   =
*       POSRVACCESSVALUES            =
*       POSERVICESTEXT               =
*       EXTENSIONIN                  =
*       EXTENSIONOUT                 =
*       POEXPIMPITEM                 =
*       POEXPIMPITEMX                =
*       POTEXTHEADER                 =
*       POTEXTITEM                   =
*       ALLVERSIONS                  =
*       POPARTNER                    =
*       POCOMPONENTS                 =
*       POCOMPONENTSX                =
*       POSHIPPING                   =
*       POSHIPPINGX                  =
*       POSHIPPINGEXP                =
*       POHISTORY                    =
*       POHISTORY_TOTALS             =
*       POCONFIRMATION               =
*       SERIALNUMBER                 =
*       SERIALNUMBERX                =
*       INVPLANHEADER                =
*       INVPLANHEADERX               =
*       INVPLANITEM                  =
*       INVPLANITEMX                 =
*       POHISTORY_MA                 =
              .

    IF gwa_bapi_return-type = 'E' OR gwa_bapi_return-type = 'X' OR
         gwa_bapi_return-type = 'A'.

      WRITE:'Error Posting Data' , gwa_po_header-po_number , gwa_po_item-po_item , gwa_bapi_return-message. "#EC *
      ULINE.


    ELSE.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' .
      WRITE:'Data Posted For :' ,gwa_po_header-po_number ,gwa_po_item-po_item gwa_bapi_return-message. "#EC *

    ENDIF.
    ULINE.


  ENDLOOP.



ENDFORM.                    " SUB_UPDATE_MM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,729

Hi Anagh,

- Did you Check FM Documentation .

- Read Documentation also Check Example  which give by SAP .

Regard's

Smruti

4 REPLIES 4
Read only

Former Member
0 Likes
1,729

Hi Anagh,

Instead of filling the POHEADER, enter the po number directly and try it.

Read only

VXLozano
Active Contributor
0 Likes
1,729

You are asking the system to change the PO number!

Read about the *X structures: there you must populate ONLY the fields you want to change, not the ones you provide.

I should seek for a BAPI*PO*READ* or BAPI*PO*GET* to retrieve the item, and then just put that BAPI item into yours, change the item-netpr field and KEEPING BLANK the itemx structure, just inform the itemx-netpr field.

---------------------

Your logic should be:

LOOP.

  CALL FUNCTION BAPI_PO_GETDETAIL1 <--- using header-po_number your number

  READ TABLE items WITH KEY your item.

  item-net_price = new_price

  itemX-net_price = 'X'

  CALL FUNCTION BAPI_PO_CHANGE <--- using the same header, keeping the headerX BLANK, and with the items table you got and changed.

ENDLOOP.

Message was edited by: V. Lozano Added logic and fixed header-item

Read only

Former Member
0 Likes
1,729

Hi man, this field(gwa_po_itemx-po_item      = 'X'.) should be field with the po item no.

Read only

Former Member
0 Likes
1,730

Hi Anagh,

- Did you Check FM Documentation .

- Read Documentation also Check Example  which give by SAP .

Regard's

Smruti