Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
thkolz
Contributor
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!

 
17 Comments