Skip to Content
Technical Articles
Author's profile photo Thorsten Kolz

Create Business Partner via API class CL_MD_BP_MAINTAIN

Hi everybody,

In S/4 HANA systems SAP recommends to use the API class CL_MD_BP_MAINTAIN to maintain business partners instead of using the old BAPIs.
The usage of the class differs quite a lot from the existing BAPIs, because there’s only one importing parameter I_DATA for the main method CL_MD_ BP_MAINTAIN=>MAINTAIN.
With BAPIs there are normally several structures/tables to be filled for the business objects.

As I couldn’t find any blog related to this topic, I thought it would be a good idea to create one.
I’ve written a small report that creates a simple business partner with a vendor role.
The data is hard-coded, but it should give you an idea what is expected by the method.
It’s very likely that you need to adapt the report in order to make it work in your system.

Before the actual creation of the Business Partner, data should be validated by method CL_MD_BP_MAINTAIN=>VALIDATE_SINGLE.

In order to write the changes to the database, a COMMIT must be performed after the successful call of CL_MD_ BP_MAINTAIN=>MAINTAIN.

*&---------------------------------------------------------------------*
*& Report zbp_create_simple
*&---------------------------------------------------------------------*
*& Create basic BP with vendor role via
*& Central API class CL_MD_BP_MAINTAIN
*&---------------------------------------------------------------------*
REPORT zbp_create_simple.

CONSTANTS:
  c_task_insert   TYPE bus_ei_object_task VALUE 'I'.

DATA:
  s_bp            TYPE cvis_ei_extern,
  t_bp            TYPE cvis_ei_extern_t,
  t_address       TYPE bus_ei_bupa_address_t,
  t_role          TYPE bus_ei_bupa_roles_t,
  t_ident_numbers TYPE bus_ei_bupa_identification_t,
  t_taxnumbers    TYPE bus_ei_bupa_taxnumber_t,
  t_return        TYPE bapiretm,
  v_bu_partner    TYPE bu_partner,
  v_error         TYPE abap_bool.

PARAMETERS:
  p_test TYPE abap_bool AS CHECKBOX.

START-OF-SELECTION.

*------------------------------------------------------------------------------
* Create GUID for new BP
*------------------------------------------------------------------------------
  TRY.
      DATA(v_guid) = cl_system_uuid=>if_system_uuid_static~create_uuid_c32( ).
    CATCH cx_uuid_error INTO DATA(r_uuid_exc).
      MESSAGE r_uuid_exc->get_text( ) TYPE 'E'.
  ENDTRY.

*------------------------------------------------------------------------------
* Header and common central data
*------------------------------------------------------------------------------
  s_bp-partner-header-object_task = c_task_insert. "'I' for new BP
  s_bp-partner-header-object_instance-bpartnerguid = v_guid.

* Category: 1 for Person, 2 for Organization, 3 for Group
  s_bp-partner-central_data-common-data-bp_control-category = '2'.
* The grouping depends on the system settings
  s_bp-partner-central_data-common-data-bp_control-grouping = 'ZCRE'.

  s_bp-partner-central_data-common-data-bp_centraldata-searchterm1 = 'SAP SE'.
  s_bp-partner-central_data-common-data-bp_organization-name1  = 'SAP SE'.
  s_bp-partner-central_data-common-data-bp_organization-name2  = 'Test BP'.
* Mark as changed
  s_bp-partner-central_data-common-datax-bp_organization-name1 = abap_true.
  s_bp-partner-central_data-common-datax-bp_organization-name2 = abap_true.

*------------------------------------------------------------------------------
* VAT number (needed for BPs located in the EU)
*
* Number is normally validated by function module VAT_REGISTRATION_NUMBER_CHECK
* Tax types are stored in table TFKTAXNUMTYPE
*------------------------------------------------------------------------------
  APPEND INITIAL LINE TO t_taxnumbers ASSIGNING FIELD-SYMBOL(<fs_taxnumbers>).
  <fs_taxnumbers>-task               = c_task_insert.
  <fs_taxnumbers>-data_key-taxtype   = 'DE0'.
  <fs_taxnumbers>-data_key-taxnumber = 'DE143454214'. "SAP SE VAT reg. number
  s_bp-partner-central_data-taxnumber-taxnumbers = t_taxnumbers.

*------------------------------------------------------------------------------
* Address data
*------------------------------------------------------------------------------
  APPEND INITIAL LINE TO t_address ASSIGNING FIELD-SYMBOL(<fs_address>).
  <fs_address>-task = c_task_insert.
* Operations are store in table TB008S
  <fs_address>-data_key-operation           = 'XXDFLT'. "Standard operation
  <fs_address>-data-postal-data-city        = 'Walldorf'.
  <fs_address>-data-postal-data-postl_cod1  = '69190'.
  <fs_address>-data-postal-data-street      = 'SAP street'.
  <fs_address>-data-postal-data-country     = 'DE'.
  <fs_address>-data-postal-data-langu       = 'E'.

* Mark as changed
  <fs_address>-data-postal-datax-city       = abap_true. 
  <fs_address>-data-postal-datax-postl_cod1 = abap_true.
  <fs_address>-data-postal-datax-street     = abap_true.
  <fs_address>-data-postal-datax-country    = abap_true.
  <fs_address>-data-postal-datax-region     = abap_true.
  <fs_address>-data-postal-datax-langu      = abap_true.

* Add address to main structure
  s_bp-partner-central_data-address-addresses = t_address.

*------------------------------------------------------------------------------
* Roles
*------------------------------------------------------------------------------
  APPEND INITIAL LINE TO t_role ASSIGNING FIELD-SYMBOL(<fs_role>).
  <fs_role>-task              = c_task_insert.
  <fs_role>-data_key          = 'FLVN01'. "Role key - Vendor
*<fs_role>-data_key          = 'FLCU01'. "Role key - customer

* Add role to main structure
  s_bp-partner-central_data-role-roles = t_role.

*------------------------------------------------------------------------------
* Validate data
*------------------------------------------------------------------------------
  cl_md_bp_maintain=>validate_single(
    EXPORTING
      i_data        = s_bp
    IMPORTING
      et_return_map = DATA(t_return_map)
  ).

  IF line_exists( t_return_map[ type = 'E' ] ) OR
     line_exists( t_return_map[ type = 'A' ] ).
    LOOP AT t_return_map INTO DATA(s_return_map).
      WRITE:/ s_return_map-message.
    ENDLOOP.
    EXIT.
  ENDIF.

*------------------------------------------------------------------------------
* Call API
*------------------------------------------------------------------------------
* Add single BP to IMPORTING table
  INSERT s_bp INTO TABLE t_bp.

  cl_md_bp_maintain=>maintain(
    EXPORTING
      i_data     = t_bp
      i_test_run = p_test
    IMPORTING
      e_return   = t_return
  ).

* Check result
  LOOP AT t_return INTO DATA(s_return).
    LOOP AT s_return-object_msg INTO DATA(s_msg).
      IF s_msg-type = 'E' OR s_msg-type = 'A'.
*       Error occurred
        v_error = abap_true.
      ENDIF.
      WRITE:/ s_msg-message.
    ENDLOOP.
  ENDLOOP.
  IF v_error IS INITIAL.
    CASE p_test.
      WHEN abap_true.
*       Test mode
        WRITE:/ |BP data is ok.|.
      WHEN abap_false.
*       Non-test mode => Perform COMMIT
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
*       Get number of new BP (it's not returned by the API)
        IMPORT lv_partner TO v_bu_partner FROM MEMORY ID 'BUP_MEMORY_PARTNER'.
        WRITE:/ |Business Partner { v_bu_partner } has been created.|.
    ENDCASE.
  ENDIF.

 

When running this report, you can only activate/deactivate the test mode.

If this is enabled, then no BP will be created. But you will see in general if the creation works or not.
Without the enabled test mode, the report returns the number of the created Business partner.

The new Business Partner can be displayed in transaction BP.

It is created with the following two roles:

  1. Business Partner Role

  2. MM Vendor Role

I hope that this blog helps you to use the API class CL_MD_ BP_MAINTAIN.
I’ve tried to keep this example as simple as possible.

 

Please feel free to share feedback or thoughts in a comment below!

 

Assigned Tags

      16 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hi Thorsten,

      It’s really informative blog.

      Thank you,

      Syam

      Author's profile photo Thorsten Kolz
      Thorsten Kolz
      Blog Post Author

      Thanks!
      I'm glad if it helps people.

      Author's profile photo xin liu
      xin liu

      nice

      thanks

      Author's profile photo Thorsten Kolz
      Thorsten Kolz
      Blog Post Author

      You're welcome!

      Author's profile photo DOZCO BASIS
      DOZCO BASIS

      Hi Thorsten Kolz,

      Nice information provided.After creating role i'm passing role data to create.Unfortunately role is creating without any data.What could be the reason.

      Author's profile photo shasank gupta
      shasank gupta

      I am in urgent need to know, how is it possible to create BAPI with 2 tax numbers ? how should I populate

      t_taxnumbers    TYPE bus_ei_bupa_taxnumber_t
      Author's profile photo Thorsten Kolz
      Thorsten Kolz
      Blog Post Author

      Just add another line to the t_taxnumbers table.

      APPEND INITIAL LINE TO t_taxnumbers ASSIGNING FIELD-SYMBOL(<fs_taxnumbers>).
      <fs_taxnumbers>-task               = c_task_insert.
      <fs_taxnumbers>-data_key-taxtype   = 'DE0'.
      <fs_taxnumbers>-data_key-taxnumber = 'DE143454214'. "SAP SE VAT reg. number
      
      APPEND INITIAL LINE TO t_taxnumbers ASSIGNING <fs_taxnumbers>.
      <fs_taxnumbers>-task               = c_task_insert.
      <fs_taxnumbers>-data_key-taxtype   = 'DE0'.
      <fs_taxnumbers>-data_key-taxnumber = 'XY143454214'. "Just a dummy number
      
      s_bp-partner-central_data-taxnumber-taxnumbers = t_taxnumbers.
      Author's profile photo Oleg Tocheniuk
      Oleg Tocheniuk

      What about payment data, structure DATA-PARTNER-CENTRAL_DATA-BANKDETAIL-BANKDETAILS? I am trying to set the start date of BANKDETAILVALIDTO to the beginning of the year 20230101 but the system changes it to the current date

      Author's profile photo Sourabh Kumar Singh
      Sourabh Kumar Singh

      Use BAPI "BUPA_BANKDETAIL_ADD or CHANGE "

      Author's profile photo Stephen Herlick
      Stephen Herlick

      Hi Thorsten

      You have opened a "can of worms" here.  I have been using the class for a couple of years now.  Whenever I have make a change or add a new feature to using it, like today to add information to the comment section of a BP address, I have to step through the code to figure out exactly how SAP wants the data.

      It would be nice if SAP thoroughly documented this class with examples for 80 to 90% of the functionality.  It is a frustrating to use this class.

      Furthermore, it doesn't appear that the BANKDETAILS can be used update banking data so had to use the old BAPIs.

      Regards,

       

      Stephen

      Author's profile photo Yogesh Odhwani
      Yogesh Odhwani

      Thanks for sharing detail, I could able to adopt in our development.

      Author's profile photo Vaishnavi p
      Vaishnavi p

      Hi,

      I want to get the business partner number and vendor number to be the same. How can i do that? Kindly let me know .

      Thanks in advance

      Author's profile photo Nicolas Fajardo
      Nicolas Fajardo

      Hi, how to use this to create several business partner reading th info from an internal table, I already do it but I have some errors. Thank you for your answer.

      Author's profile photo madjid khanevadegi
      madjid khanevadegi

      Hello, Thorsten Kolz
      thank you for sharing to this post,

      i receiving this error when calling the method cl_md_bp_maintain=>validate_single.
      ( Function module "BBP_IFC_INBOUND" not found.
      this exception has occurred in class "CX_SY_DYN_CALL_ILLEGAL_FUNC" )

      Has anyone else run into this and found a solution?

       

      best regards

       

      Author's profile photo TECNOCOM SOPORTE
      TECNOCOM SOPORTE

      Hello Thorsten Kolz,

      How can i assign a bp number instead of it was proposed by system at creating. I tried it not passing it the bpartnerguid only the field bpartner. Something like this:

      ls_data-partner-header-object_instance-bpartner '0000042882'.

      It doesn't work.

      Thank you.

       

      Best regards

      Author's profile photo Thorsten Kolz
      Thorsten Kolz
      Blog Post Author

      I think you need to read the field PARTNER_GUID from table BUT000 first and pass it to ls_data-partner-header-object_instance-bpartnerguid