Application Development 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: 

BAPI structures: VALUE vs single lines?

Jelena
Active Contributor

This is more of a style question and might not have a single correct question. When we need to use a BAPI, there are typically some structures with a bunch of fields to be filled in. We're looking at two possible ways to write such code, see examples below.

Option 1:

    order_header_in-doc_type = document_type.
    order_header_in-sales_org = sales_organization.
    order_header_in-ref_doc = billing_document.
    order_header_in-refdoc_cat = c_invoice.
<...>

Option 2:

    order_header_in = VALUE#( doc_type = document_type
                              sales_org = sales_organization
                              ref_doc = billing_document
                              refdoc_cat = c_invoice ).

Option 2 appears more readable. However, option 1 has some advantages while working on the code and potentially when maintaining it:
- code completion, e.g. you don't know the exact field name, just type structure name, then dash (-) and you get a list of fields;
- if you find that one BAPI doesn't work and you need to use another one, the field names in BAPI structures can be same or different. When you change the structure type, you'll see right away what fields need to be updated. (This is a rather rare case, I'd say.)

What is your opinion on these options or other suggestions? Thank you!

1 ACCEPTED SOLUTION

larshp
Active Contributor

Regarding style, I typically add a new line add indentation, and adjust the parameters as per https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#align-parameters

Option 3,

    order_header_in = VALUE#( 
      doc_type   = document_type
      sales_org  = sales_organization
      ref_doc    = billing_document
      refdoc_cat = c_invoice ).
6 REPLIES 6

SimoneMilesi
Active Contributor

Hello Jelena!

From my humble POV, your point on code completion isn't a valid one, because (at least in Eclipse) even the VALUE form gives you auto completion with ctrl+space (and you see the list of fields).

And also your second point doesn't sound really convincing! A rapid syntax check gives you the error 🙂

larshp
Active Contributor

Regarding style, I typically add a new line add indentation, and adjust the parameters as per https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#align-parameters

Option 3,

    order_header_in = VALUE#( 
      doc_type   = document_type
      sales_org  = sales_organization
      ref_doc    = billing_document
      refdoc_cat = c_invoice ).

Sandra_Rossi
Active Contributor

Using ADT is also a game changer, as F2 (type info) is used everywhere by the developers (I guess), to show the exact type of the selected variable.

When using VALUE #( ... ) for a structure, the code completion proposes many useless words instead of just the component names, whatever it's ADT or SE80, I use to temporarily type a dummy VALUE #( a = b ... ) so that the code completion after "b" shows only the components.

I also like creating custom OO wrappers for anything standard, especially function modules, so that to benefit from new features to avoid intermediate variables, as far clarity is okay:

zcl_bapi_salesorder=>create(
     EXPORTING
       order_header_in = VALUE #( doc_type  = document_type
                                  sales_org = sales_organization )
     IMPORTING
       return          = DATA(return) ).       

I'm a complete fan of constructor expressions because they guarantee that the components not set are initial, without need to indicate CLEAR.

Concerning the 2nd drawback, "When you change the structure type, you'll see right away what fields need to be updated", it's also valid for constructor expressions, no? Or do you mean it's showing an error for the whole constructor expression, not the line where the component is invalid? With ADT, no issue, all invalid components are immediately highlighted.

Disclaimer: I use an old ADT 2021-09 so I don't know if ADT has been improved since then.

Jelena
Active Contributor

Yes, a syntax error for the whole line is what I meant. I prefer to use SAP GUI for classes not as some form of protest but purely because it works best for my vision and personal preferences.

After some revisions, I came up with the best options and added it in the comment to the original question. Thank you!

0 Kudos

sandra.rossi You are my favourite, best explanations!!

Jelena
Active Contributor

With some help from db48526ccb71414183a64aa46e31e784 I've realized that I can also simply do this, so now I'm freshly convinced that this is bees knees. 🙂 Thank you, everyone, for answers:

DATA(order_header_in) = VALUE bapisdhd1(doc_type = document_type                             
sales_org = sales_organization
ref_doc = billing_document
refdoc_cat = c_invoice ).
SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; }.L0S31 { font-style: italic; color: #808080; }.L0S52 { color: #0000FF; }.L0S55 { color: #800080; }.L0S70 { color: #808080; }