BAPI_ADDRESSORG_CHANGE does not work correctly

104 Views Asked by At

BAPI_ADDRESSORG_CHANGE does not work correctly, I'm trying to update the address and also notes (remark) from XD02 with this BAPI but it does not work, I don't know what I'm doing wrong maybe you guys could help me with this issue.

I tried with this:

* Assign the following object type and object key to a main customer address

    objtype    = 'KNA1'.
    obj_id     = lv_kunnr.

    CALL FUNCTION 'BAPI_ADDRESSORG_GETDETAIL' " ---> Chequear BAPI, nos elimina el dato wa_excel_data-kunnr
      EXPORTING
        obj_type       = objtype
        obj_id         = obj_id
        context        = context
      IMPORTING
        address_number = lv_adrnr
      TABLES
        bapiadsmtp     = gt_bapiadsmtp
        return         = return.

    IF sy-subrc EQ 0.

      READ TABLE gt_tables_for_excel INTO wa_tables_for_excel WITH KEY "addrnumber = lv_adrnr                
                                                                       smtp_addr  = wa_excel_data-smtp_addr.

      IF sy-subrc EQ 0.
        wa_bapiadsmtp-e_mail      = wa_excel_data-smtp_addr.
        wa_bapiadsmtp-consnumber  = 001.
        wa_bapiadsmtx-e_mail      = 'X'.
        wa_bapiadsmtx-updateflag  = 'U'.
      ELSE.
        wa_bapiadsmtp-std_no      = 'X'.
        wa_bapiadsmtp-e_mail      = wa_excel_data-smtp_addr.
        wa_bapiadsmtp-consnumber  = 001.
        wa_bapiadsmtx-updateflag  = 'I'.
      ENDIF.

      APPEND wa_bapiadsmtp TO gt_bapiadsmtp.
      APPEND wa_bapiadsmtx TO gt_bapiadsmtx.

      CLEAR: wa_bapiadsmtp, wa_bapiadsmtx, wa_bapicomrem, wa_bapicomrex.

      CALL FUNCTION 'BAPI_ADDRESSORG_CHANGE'
        EXPORTING
          obj_type       = objtype
          obj_id         = obj_id
          obj_id_ext     = obj_id_ext
          save_address   = 'X'
          context        = context
        IMPORTING
          address_number = lv_adrnr
        TABLES
          bapiadsmtp     = gt_bapiadsmtp
          bapiadsmt_x    = gt_bapiadsmtx
          return         = return.

      IF sy-subrc = 0.
        WRITE : / 'ADRT update  :', wa_excel_data-name1.
      ELSE.
        WRITE : / 'ADRT couldn't update : ', wa_excel_data-name1.
      ENDIF.
    ENDIF.

Some lines of code are commented since I tried everything I could think or found on the docs.

1

There are 1 best solutions below

2
Kisbandi On

I see that you are looking at sy-subrc after BAPI, but BAPI does not return EXCEPTIONS. This BAPI returns a return table containing the error messages. It is worth checking to see if there are any errors in this. If so, you can go from there to see what exactly needs to be fixed.

Otherwise, I currently see several sources of errors that could lead to poblems. Alternatively, it is not clear.

  • For example, you can check the return of BAPI_ADDRESSORG_GETDETAIL with sy-subrc, but then (since it always takes 0) you take the new element as INSERT element within it, but you cannot know if the table contains any data at all.
  • Why do you query the table gt_bapiadsmtp with the BAPI_ADDRESSORG_GETDETAIL BAPI, but then it is not used? If you want to UPDATE a line, you should modify it on what already exists.
  • Does BAPI not work for UPDATE and INSERT either?
  • For INSERT, there is a missing wa_bapiadsmtx-e_mail = 'X'.
  • Are you sure you need save_address = 'X'?
  • If the return returns without error it may be necessary to call a CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

Suddenly these questions/solutions came to mind. But I will try to help if you can look them up or clarify.

Updated: the following code works for me.

I checked the corresponding address in the ADRC6 table.

For INSERT, it depends on the number of CONSNUMBER elements to be included, so it is a good idea to select them with the BAPI_ADDRESSORG_GETDETAIL function beforehand. Or use simple 001.

For UPDATE, the record must exist (this can also be checked).

The error messages determine which value you will be able to edit, here you may get errors if you have different time zones or incomplete data. These need to be handled. But BAPI_TRANSACTION_COMMIT is definitely missing for you.

objtype    = 'KNA1'.
obj_id     = lv_kunnr.

CALL FUNCTION 'BAPI_ADDRESSORG_GETDETAIL'
  EXPORTING
    obj_type       = objtype
    obj_id         = obj_id
  IMPORTING
    address_number = lv_adrnr
  TABLES
    bapiadsmtp     = gt_bapiadsmtp
    return         = return.

  READ TABLE gt_bapiadsmtp INTO wa_bapiadsmtp_tmp WITH KEY consnumber = 001.
  IF sy-subrc = 0.
    wa_bapiadsmtp-std_no      = 'X'.
    wa_bapiadsmtp-e_mail      = '[email protected]'. "wa_excel_data-smtp_addr
    wa_bapiadsmtp-consnumber  = 001.
    wa_bapiadsmtx-e_mail      = 'X'.
    wa_bapiadsmtx-updateflag  = 'U'.
  ELSE.
    wa_bapiadsmtp-std_no      = 'X'.
    wa_bapiadsmtp-e_mail      = '[email protected]'. "wa_excel_data-smtp_addr
    wa_bapiadsmtp-consnumber  = 001.
    wa_bapiadsmtx-e_mail      = 'X'.
    wa_bapiadsmtx-updateflag  = 'I'.
  ENDIF.
  CLEAR: gt_bapiadsmtp.

  APPEND wa_bapiadsmtp TO gt_bapiadsmtp.
  APPEND wa_bapiadsmtx TO gt_bapiadsmtx.
  CLEAR: wa_bapiadsmtp, wa_bapiadsmtx, wa_bapiadsmtp_tmp.

  CALL FUNCTION 'BAPI_ADDRESSORG_CHANGE'
    EXPORTING
      obj_type       = objtype
      obj_id         = obj_id
    IMPORTING
      address_number = lv_adrnr
    TABLES
      bapiadsmtp     = gt_bapiadsmtp
      bapiadsmt_x    = gt_bapiadsmtx
      return         = return.

  IF NOT line_exists( return[ type = 'E' ] ).
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ENDIF.