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

Handle null value as input to BAPI-X structure dynamically.

koolspy_ultimate
Active Contributor
0 Likes
1,366

Hi All,

Could you please suggest how to input null value / 0 / Space to bapix structures.

To be more accurate please find the below example.

BAPI: BAPI_MATERIAL_SAVEREPLICA

Field XCHPF (It accepts only "X" or "").

currently I am update the plantdatax-xchpf as following:

lw-xchpf = 'X'.
append lw to lt_plant.

If lw-xchpf is not initial.
 lw_plantx-xchpf = 'X'.
endif.
append lw_plantx to lt_plantx.

The above is working fine. (Note: I am trying to make the solution dynamic)

Now I have a scenario where I need to update the value to null.

lw-xchpf = ''.
append lw to lt_plant.

If lw-xchpf is not initial. "------This is causing Issue
 lw_plantx-xchpf = 'X'.
endif.
append lw_plantx to lt_plantx.

Thanks in Advance.

3 REPLIES 3
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,112

What issue do you have? ("if lw-xchpf is not initial ... causing Issue" is not a symptom)

You say "null value / 0 / Space", it's rather imprecise, can you explain what you mean? (why different terms?)

Read only

MateuszAdamus
Active Contributor
1,112

Hello koolspy.ultimate

You could write a method (or a procedure) which uses ABAP RTTI for this. Example:

CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
      mark_x
        IMPORTING
          iv_filled_only TYPE flag
          is_structure   TYPE any
        CHANGING
          cs_structure_x TYPE any.
ENDCLASS.

CLASS lcl_test IMPLEMENTATION.
  METHOD mark_x.
    DATA:
      lo_type_table     TYPE REF TO cl_abap_tabledescr,
      lo_type_structure TYPE REF TO cl_abap_structdescr.

    DATA(lo_type) = cl_abap_typedescr=>describe_by_data( p_data = is_structure ).
    CASE lo_type->kind.
      WHEN cl_abap_typedescr=>kind_struct.
        lo_type_structure ?= lo_type.
    ENDCASE.
    CHECK lo_type_structure IS BOUND.

    DATA(lt_components) = lo_type_structure->get_components( ).
    LOOP AT lt_components REFERENCE INTO DATA(ld_component).
      ASSIGN COMPONENT ld_component->name OF STRUCTURE cs_structure_x TO FIELD-SYMBOL(<lv_field_x>).
      CHECK sy-subrc = 0.

      IF iv_filled_only = abap_true.
        ASSIGN COMPONENT ld_component->name OF STRUCTURE is_structure TO FIELD-SYMBOL(<lv_field>).
        CHECK sy-subrc = 0
          AND <lv_field> IS NOT INITIAL.
      ENDIF.

      <lv_field> = abap_true.
    ENDLOOP.
  ENDMETHOD.
ENDCLASS.

" then, the usage
IF something = abap_true OR something_else = abap_true.
  lv_filled_only = abap_true.
ELSE.
  lv_filled_only = abap_false.
ENDIF.

lcl_test=>mark_x(
  EXPORTING iv_filled_only = lv_filled_only is_structure = ls_values
  CHANGING cs_structure_x = ls_values_x ).<br>

The LS_VALUES structure from the example does not have to be the same as the BAPI's values structure. It can be a structure of any type. The important thing is that at least some of its fields have the same name as the BAPI's structure. These fields will have their X filled by the MARK_X method.

Kind regards,

Mateusz

Read only

koolspy_ultimate
Active Contributor
0 Likes
1,112

Hi Sandra Rossi,

I mean how to pass empty value as input considering the if condition for updating the bapix structure.