SAP MD04 - Overview Tree Export - sap

I would like to find a way to export the SAP MD04 - overview tree in Excel.
I don't understand what will be the best approach.
Thanks in advance for any suggestion.
Regs
S.

I've done this workaround:
You have to use 2 Functional Module CS_BOM_EXPL_MAT_V2 + MD_STOCK_REQUIREMENTS_LIST_API
In the INFOSET Data Reading Program I've used the CS_BOM_EXPL_MAT_V2 for the BOM Explosion.
REPORT RSAQDVP_TEMPLATE .
data:
STPOX type STPOX ,
it_data type standard table of STPOX .
field-symbols: <struc> type STPOX .
* !! the following comment MUST NOT BE CHANGED !!
*<QUERY_HEAD>
* (select your data here into internal table IT_DATA)
call function 'CS_BOM_EXPL_MAT_V2'
EXPORTING
auskz = 'X'
capid = p_capid
datuv = p_datuv
emeng = u_qty
mehrs = 'X'
mtnrv = p_mtnrv
werks = p_werks
TABLES
stb = it_data.
loop at it_data assigning <struc>.
move-corresponding <struc> to STPOX .
* !! the following comment MUST NOT BE CHANGED !!
*<QUERY_BODY>
endloop.
Than I've added Additional Field calling the MD_STOCK_REQUIREMENTS_LIST_API.
CLEAR MNG01_FM.
call function 'MD_STOCK_REQUIREMENTS_LIST_API'
EXPORTING
matnr = STPOX-IDNRK
werks = p_werks
TABLES
mdezx = it_da2.
READ TABLE it_da2 ASSIGNING <struct2> WITH KEY DELKZ = 'SB'.
if sy-subrc = 0.
MNG01_FM = <struct2>-MNG01.
endif.

Related

Creating documentary batches programmatically with BAPI_GOODSMVT_CREATE?

Summary of the problem
Automatic Documentary Batch handling through custom ABAP code
My employer wishes to perform automatic documentary batch handling on some products from external vendors, and I'm trying to figure out how to set this up through Customizing and ABAP.
It seems to me that Documentary Batches are only meant to be used through MIGO - in any case I'm unable to find a proper solution to assign them programatically, and any hacked-together solution I can come up with, seems insufficient and unstable.
What avenues do I have to solve this issue?
Enhancing BAPI_GOODSMVT_CREATE?
Can I somehow do it through stuff like BAPI_GOODSMVT_CREATE?
Enhancing PPPI Message Destinations?
I also specifically need it to work for consumption messaging through PPPI, and I thought to build on top of the standard Message Destination PI04, FM COCI_CONFIRM_MATERIAL_CONS.
This FM creates a Material Document but does not go through the BAPI_GOODSMVT_CREATE FM.
It does however use MB_CREATE_GOODS_MOVEMENT.
What I've already tried
MIGO Snapshot based Single Use hack solution
I made hack-solution for one area, where I watched which table-updates MIGO performed and with which data (through FM's VB_INSERT_BATCH and VB_BATCH_WHERE_USED_LIST), and then filled out these structures manually.
However, providing all the needed info is not feasible for other implementation areas, as they do not have all the necessary values available, and it doesn't cover unforeseen situations where other parameters might be required.
Reading through BAPI_GOODSMVT_CREATE code
I've tried spying on whether BAPI_GOODSMVT_CREATE performs the same FM's but only found it accessing VB_BATCH_WHERE_USED_LIST.
It seems to be possible to activate this functionality by controlling Memory ID Documentary Batch #1, Documentary Batch #2, Documentary Batch #3 and Documentary Batch #5 (see FM VBDBDM_DATA_POST_IM), but this requires filling out a lot data, including the structure named DOCUBATCH_SCREEN_FIELDS, which again makes it seem like this might not be the correct avenue of approach.
Regardless, this still doesn't allow me to maintain batch through tables MCHA and MCH1.
Hacked together solution based on MIGO snapshot
Here is how my hacked solution looks. Again, this is not a feasible way to go about the problem, as other implementation areas does not have the resulting Material Document immediately available:
FUNCTION zproxy_mdr_goodsreceipt.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IS_GOODSRECEIPT_HEAD) TYPE ZPROXY_GOODSREC_HEAD
*" VALUE(IT_GOODSRECEIPT_ITEM) TYPE ZPROXY_GOODSREC_ITEM_T
*" REFERENCE(I_CREATE_TO_FROM_REQUIREMENTS) TYPE FLAG DEFAULT '-'
*" EXPORTING
*" REFERENCE(E_GOODSMVT_MSG_IDNO) TYPE CHAR23
*" REFERENCE(E_MBLNR) TYPE MBLNR
*" REFERENCE(E_TO_CREATION_SUBRC) TYPE SY-SUBRC
*" REFERENCE(E_LGNUM_ERROR) TYPE LGNUM
*" REFERENCE(E_TBNUM_ERROR) TYPE TBNUM
*" REFERENCE(E_DOCBATCH_SUBRC) TYPE SY-SUBRC
*" REFERENCE(E_DOCBATCH_MSG_IDNO) TYPE CHAR23
*" REFERENCE(E_CLASSNUM) TYPE BAPI1003_KEY-CLASSNUM
*" REFERENCE(E_OBJKEY) TYPE BAPI1003_KEY-OBJECT
*" EXCEPTIONS
*" GOODSMVT_FAILED
*" NO_TRANSFER_REQUIREMENTS
*" TRANSFER_ORDER_CREATION_ERROR
*"----------------------------------------------------------------------
FIELD-SYMBOLS: <return> TYPE bapiret2,
<goods_rec_item> TYPE zproxy_goodsrec_item,
<mseg> TYPE mseg,
<char_char> TYPE bapi1003_alloc_values_char,
<ltap_creat> TYPE LTAP_CREAT.
DATA: ls_header TYPE bapi2017_gm_head_01,
ls_code TYPE bapi2017_gm_code,
ls_item TYPE bapi2017_gm_item_create,
lt_item TYPE STANDARD TABLE OF bapi2017_gm_item_create,
lt_return TYPE STANDARD TABLE OF bapiret2,
ls_headret TYPE bapi2017_gm_head_ret,
l_mblnr LIKE bapi2017_gm_head_ret-mat_doc,
l_docubatch TYPE charg_d,
l_subrc TYPE sy-subrc,
lt_mseg TYPE STANDARD TABLE OF mseg.
CLEAR l_subrc.
* ############################## Create goods movement ##############################
* Build structures
MOVE-CORRESPONDING is_goodsreceipt_head TO ls_header.
ls_code-gm_code = '01'.
LOOP AT it_goodsreceipt_item ASSIGNING <goods_rec_item>.
MOVE-CORRESPONDING <goods_rec_item> TO ls_item.
APPEND ls_item TO lt_item.
ENDLOOP.
* BAPI call
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = ls_header
goodsmvt_code = ls_code
IMPORTING
goodsmvt_headret = ls_headret
materialdocument = l_mblnr
TABLES
goodsmvt_item = lt_item
return = lt_return.
* Check errors
READ TABLE lt_return ASSIGNING <return> WITH KEY type = 'E'.
IF sy-subrc = 0.
e_goodsmvt_msg_idno = <return>-id && <return>-number.
ROLLBACK WORK.
RAISE goodsmvt_failed.
ELSE.
e_mblnr = l_mblnr.
COMMIT WORK AND WAIT. "Wait for TO requirements to be created
ENDIF.
* Only proceede if Material Document has been successfully posted
CHECK l_subrc = 0 AND l_mblnr IS NOT INITIAL.
* ############################## Update with Documentary Batch ###################################
DATA: lt_chvw TYPE STANDARD TABLE OF chvw,
ls_chvw TYPE chvw,
lt_mch1 TYPE STANDARD TABLE OF mch1,
ls_mch1 TYPE mch1,
lt_mcha TYPE STANDARD TABLE OF mcha,
ls_mcha TYPE mcha,
lt_mchb TYPE STANDARD TABLE OF mchb,
lt_mska TYPE STANDARD TABLE OF mska,
lt_mspr TYPE STANDARD TABLE OF mspr,
lt_char_num TYPE STANDARD TABLE OF bapi1003_alloc_values_num,
lt_char_char TYPE STANDARD TABLE OF bapi1003_alloc_values_char,
lt_char_curr TYPE STANDARD TABLE OF bapi1003_alloc_values_curr,
l_objkey TYPE bapi1003_key-object,
l_classnum TYPE bapi1003_key-classnum,
l_atnam TYPE atnam.
REFRESH lt_chvw.
* Get material document items
SELECT *
FROM mseg
INTO TABLE lt_mseg
WHERE mblnr = l_mblnr.
* Perpare docubatch registration data
LOOP AT it_goodsreceipt_item ASSIGNING <goods_rec_item>.
* Generate class num and atnam from plant
CONCATENATE 'PI_' <goods_rec_item>-plant INTO l_classnum.
CONCATENATE 'Z_DOC_BATCH_' <goods_rec_item>-plant INTO l_atnam.
* Get material docubatch usage characteristic
REFRESH: lt_return,
lt_char_num,
lt_char_char,
lt_char_curr.
l_objkey(18) = <goods_rec_item>-material.
CALL FUNCTION 'BAPI_OBJCL_GETDETAIL'
EXPORTING
objectkey = l_objkey
objecttable = 'MARA'
classnum = l_classnum
classtype = '001'
TABLES
allocvaluesnum = lt_char_num
allocvalueschar = lt_char_char
allocvaluescurr = lt_char_curr
return = lt_return.
LOOP AT lt_return ASSIGNING <return> WHERE type = 'E'. "Check for errors
* Couldn't read characteristic, assume no docubatch handling
e_docbatch_subrc = '1'.
e_docbatch_msg_idno = <return>-id && <return>-number.
e_classnum = l_classnum.
e_objkey = l_objkey.
CONTINUE.
ENDLOOP.
READ TABLE lt_char_char ASSIGNING <char_char> WITH KEY charact = l_atnam.
IF sy-subrc <> 0 OR <char_char>-value_neutral = 0.
* No docubatch value
CONTINUE.
ENDIF.
* Get associated material document item
READ TABLE lt_mseg ASSIGNING <mseg>
WITH KEY mblnr = ls_headret-mat_doc
mjahr = ls_headret-doc_year
bwart = <goods_rec_item>-move_type
matnr = <goods_rec_item>-material
werks = <goods_rec_item>-plant
menge = <goods_rec_item>-entry_qnt
meins = <goods_rec_item>-entry_uom
hsdat = <goods_rec_item>-prod_date
kzbew = <goods_rec_item>-mvt_ind
lgort = <goods_rec_item>-stge_loc.
IF sy-subrc <> 0.
* No associated material document item
CONTINUE.
ENDIF.
* Check docubatch type
IF <char_char>-value_neutral <> 0.
* Perform basic docubatch actions (MCHA and MCH1)
* Verify that docubatch nr is assigned
IF <goods_rec_item>-vendrbatch IS INITIAL.
* !!!!!!!!!!!!! Venderbatch not filled even though material is docubatch managed, what to do? !!!!!!!!!!!!!!!
CONTINUE.
ENDIF.
* Prepare data for docubatch registration
CLEAR: ls_mch1,
ls_mcha.
ls_mch1-matnr = <goods_rec_item>-material.
ls_mch1-charg = <goods_rec_item>-vendrbatch.
ls_mch1-ersda = sy-datum.
ls_mch1-ernam = sy-uname.
ls_mch1-ersda_tmstp = sy-datum && sy-uzeit.
ls_mch1-ersda_tz_sys = sy-tzone.
ls_mch1-ersda_tz_usr = sy-zonlo.
MOVE-CORRESPONDING ls_mch1 TO ls_mcha. "Same fields from MCH1 are included in MCHA
ls_mcha-werks = <goods_rec_item>-plant.
APPEND: ls_mch1 TO lt_mch1,
ls_mcha TO lt_mcha.
ENDIF.
IF <char_char>-value_neutral = 2. "Also include batch where-used
* Perpare data for batch where-used registration
CLEAR ls_chvw.
ls_chvw-matnr = <goods_rec_item>-material.
ls_chvw-werks = <goods_rec_item>-plant.
ls_chvw-charg = <goods_rec_item>-vendrbatch.
ls_chvw-ebeln = <goods_rec_item>-po_number.
ls_chvw-ebelp = <goods_rec_item>-po_item.
ls_chvw-mblnr = ls_headret-mat_doc.
ls_chvw-mjahr = ls_headret-doc_year.
ls_chvw-zeile = <mseg>-zeile.
ls_chvw-budat = is_goodsreceipt_head-pstng_date.
ls_chvw-shkzg = 'S'. "??? VALUE ???
ls_chvw-bwart = <goods_rec_item>-move_type.
ls_chvw-kzbew = <goods_rec_item>-mvt_ind. "Goods Movement for Purchase Order
ls_chvw-menge = <goods_rec_item>-entry_qnt.
ls_chvw-meins = <goods_rec_item>-entry_uom.
APPEND ls_chvw TO lt_chvw.
ENDIF.
ENDLOOP.
* Perform batch registration
CALL FUNCTION 'VB_INSERT_BATCH'
TABLES
zmch1 = lt_mch1
zmcha = lt_mcha
zmchb = lt_mchb
zmska = lt_mska
zmspr = lt_mspr
.
* Perform batch where-used registration
CALL FUNCTION 'VB_BATCH_WHERE_USED_LIST'
TABLES
xchvw = lt_chvw.
Why this isn't good enough, and what I need
This performs as a snapshot of MIGO configured with documentary batch handling, but doesn't necessarily cover all cases.
It only works in the context of a Purchase Document, and doesn't cover other cases such as Orders and Sales Orders.
Additionally I only have the necessary date because of the material document being created immediately above, which is not possible for all implementation cases.
I would like to know if there is an intended way to perform Documentary Batch handling from custom code.
Quoting from the documentation:
If you work with RFID or TRM functions, or call IDocs/BAPIs, you can only book in documentary
batches by calling up the RFC-capable function module VBDBDM_DATA_MAINTAIN_RFC
beforehand or incorporating it into the process.
So maybe this function module is the key?
However, it seems you may not be the first to experience this pain. A comment on that documentation reads:
Documentary Batch has a lot of constraints and it seems to be a semifinished product of SAP, since is missing a lot of features of real batches.
Be prepared to make a lot of custom enhancements...
ADDENDUM from community: below is the solution taken from the Original Poster two days after this answer, moved away from his question.
Solution
Example call for Purchase Order Goods Receipt
LOOP AT it_goodsreceipt_item ASSIGNING <goods_rec_item>.
CALL FUNCTION 'VBDBDM_DATA_MAINTAIN_RFC'
EXPORTING
i_matnr = <goods_rec_item>-material
i_werks = <goods_rec_item>-plant
i_quantity = <goods_rec_item>-entry_qnt
i_uom = <goods_rec_item>-entry_uom
i_docubatch_charg = <goods_rec_item>-vendrbatch
* IT_DOCUBATCHES =
i_process_id = '01' "Goods Receipt for External Procurement
* I_REPLACE_EXISTING_DATA =
i_ebeln = <goods_rec_item>-po_number
i_ebelp = <goods_rec_item>-po_item
* I_AUFNR =
* I_AUFPS =
* I_RSNUM =
* I_RSPOS =
* I_RSART =
* I_VBELN =
* I_POSNR =
* IS_DOCUBATCH_COM =
* I_LINE_ID =
* I_LGNUM =
* I_TANUM =
* I_TAPOS =
EXCEPTIONS
parameter_error = 1
process_not_active = 2.
ENDLOOP.
* Follow up by creating Material Document, for example through BAPI_GOODSMVT_CREATE

Number of fields in a structure

I'm new in abap development .
Actually, I'm looking for the Number of fields of a table or structure
any suggestion please
For counting the components of a structure, better use RTTS as we don't want to access the database for result we already have in our environment.
DATA(lwa_struct) = VALUE kna1( ). " your structure e.g. kna1
DATA(lo_descr) = CAST cl_abap_structdescr( cl_abap_datadescr=>describe_by_data( lwa_struct ) ).
DATA(lw_all_fields) = LINES( lo_descr->components ). " => 184
#lausek answered correctly. But for release 730 or lower, you'll need to adapt to something like this:
data ls_mara type mara.
data lo_structdescr type ref to cl_abap_structdescr.
data lv_count type i.
lo_structdescr ?= cl_abap_datadescr=>describe_by_data( ls_mara ).
lv_count = lines( lo_structdescr->components ).
write lv_count.
Another solution from my side.
select count(*) into #data(lv_count) from dd03l
where tabname = 'YOUR STRUCTURE NAME' and AS4LOCAL = 'A'.

How to get the row count via ADBC native SQL?

I can do the following query using Open SQL:
select count(*) into lv_count
from prcd_elements client specified
where waerk = 'USD'
How do I do the same thing with ABAP Database Connectivity (ADBC) native SQL?
I've tried the following but it gives me an exception... I'm not sure what data type I need for lt_table to hold a count, and class CL_SQL_RESULT_SET (returned by execute_query) seems to require a local table.
data lt_table type standard table of prcd_elements.
data(lo_sql) = new cl_sql_statement( ).
data(lo_result) = lo_sql->execute_query(
|select count(*) from prcd_elements where waerk = 'USD'|
).
lo_result->set_param_table( REF #( lt_table ) ).
lo_result->next_package( ).
lo_result->close( ).
Thanks!
With some help from a few mysterious benefactors, I figured this one out. I needed to create an internal table with a single column of type I (Integer), and then extract the count from this internal table.
types:
begin of s_count,
count type i,
end of s_count.
data lt_table type standard table of s_count.
data(lo_sql) = new cl_sql_statement( ).
data(lo_result) = lo_sql->execute_query(
|select count(*) from prcd_elements where waerk = 'USD'|
).
lo_result->set_param_table( REF #( lt_table ) ).
lo_result->next_package( ).
lo_result->close( ).
data(lv_count) = lt_table[ 1 ]-count.
You can also use a variable instead of a internal table;
data lv_count type i.
data(lo_sql) = new cl_sql_statement( ).
lo_sql->execute_query(select count(*) into :lv_count from prcd_elements where waerk = 'USD').

ABAP Display field symbol dynamic in alv

I am pasting this program for example but i will never know the type of the table (here vbap and vbak).
My goals is to display my field symbol without knowing the types.
Is it possible ?
Here is my code :
REPORT ZTEST_FME_FOL.
type-pools slis .
FIELD-SYMBOLS : <mytable> TYPE ANY TABLE.
DATA : lv_alv_table TYPE REF TO cl_salv_table,
lv_funct TYPE REF TO cl_salv_functions,
lv_columns TYPE REF TO cl_salv_columns_table,
lv_column TYPE REF TO CL_SALV_COLUMN_table.
SELECT * from vbap INNER JOIN VBAK ON vbap~vbeln = vbak~vbeln UP TO 10 ROWS INTO TABLE <mytable>.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lv_alv_table
CHANGING
t_table = <mytable> ).
CATCH cx_salv_msg .
ENDTRY.
lv_funct = lv_alv_table->get_functions( ).
lv_funct->set_all( Abap_True ).
lv_columns = lv_alv_table->get_columns( ).
lv_alv_table->display( ).
Thanks in advance !
Depending on what you 're trying to do there's going to be more validation required than what I've done, but in essence this is what you need.
Using (dynamic) joins may be particularly tricky.
report zevw_test_dynamic_alv.
parameters: p_table type string obligatory.
field-symbols: <gt_table> type standard table.
data: gt_data type ref to data.
start-of-selection.
create data gt_data type table of (p_table).
assign gt_data->* to <gt_table>.
select * from (p_table) up to 10 rows
into table <gt_table>.
perform display_results using <gt_table>. "Your ALV stuff will be in here
You may even have to build the fieldcat manually and then use
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = gt_fieldcat[]
importing
ep_table = gt_data.
to get the data reference

Usage of bapi_vendor_find

I'm quite new to ABAP world. I searched on the web over an hour but couldn't find an example of bapi_vendor_find function.
CALL FUNCTION 'BAPI_VENDOR_FIND'
* EXPORTING
* MAX_CNT = 0
* PL_HOLD = ' '
* IMPORTING
* RETURN =
TABLES
selopt_tab = g_tab_selopt
result_tab = g_tab_result
.
How should I prepare the "g_tab_selopt" and "g_tab_selopt". Thanks in advance for any help...
After many tries and fails I've found out the solution.
REPORT zbss00_lieferant_anzeigen.
PARAMETERS: p_ccode TYPE bapivendor_10-comp_code.
TYPES: tab1 TYPE TABLE OF BAPIVENDOR_10.
Data:
g_tab_selopt type table of BAPIVENDOR_10,
g_tab_result type table of BAPIVENDOR_11,
l_vendor_no type BAPIVENDOR_11,
g_selopt type BAPIVENDOR_10.
g_selopt-COMP_CODE = p_ccode.
g_selopt-TABNAME = 'LFB1'.
g_selopt-FIELDNAME = 'MANDT'.
g_selopt-FIELDVALUE = '900'.
APPEND g_selopt TO g_tab_selopt.
CALL FUNCTION 'BAPI_VENDOR_FIND'
* EXPORTING
* MAX_CNT = 0
* PL_HOLD = ' '
* IMPORTING
* RETURN =
TABLES
selopt_tab = g_tab_selopt
result_tab = g_tab_result
.
loop at g_tab_result into l_vendor_no.
write: /, 'Vendor No: ', l_vendor_no-VENDOR_NO.
endloop.
In this example, I've searched for vendors by company code in the table "LFB1" where mandt is equal to 900. I set MANDT 900 because i needed to get all vendors and in my case all vendors have 900 in MANDT column.
This may not be the proper way, but at least it works. If anyone knows a better way to do this, please feel free to write. I'll edit the answer according to that...
Take a look at the online documentation - either of the function module directly or in the transaction BAPI.