2022 Nov 04 4:04 PM
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!
2022 Nov 04 5:05 PM
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 ).
2022 Nov 04 4:40 PM
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 🙂
2022 Nov 04 5:05 PM
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 ).
2022 Nov 04 5:32 PM
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.
2022 Nov 04 9:38 PM
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!
2022 Nov 05 3:01 AM
2022 Nov 04 9:34 PM
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;
}
2024 Aug 12 1:02 PM - edited 2024 Aug 12 3:40 PM
@Sandra_Rossi wrote: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.
That is indeed a good tipp!
OTH, I wonder why the code completion fails completely above and below, too?
If one is using # at this point I tend to understand this behavior, but when using e.g.
DATA(l_ag) = VALUE #( gt_ihpa[ parvw = zif_partners=>role-ag_orderer ] OPTIONAL ).
and doing code completion after gt_ihpa[ I still get nonsense suggestions although it should be clear which members should be available, right?
You tip works perfectly well here, too, btw!