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: 

Error in MB_MIGO_BADI

former_member470288
Participant
0 Kudos

Hi experts,

I have the following requirements:

When executing MIGO (A01 Goods receipt, movement type 101), there're some materials that have BOM explosion and I need to decrease the stock of the component materials.

I have implemented MB_MIGO_BADI~POST_DOCUMENT with the following code:

DATA: lwa_mseg TYPE LINE OF ty_t_mseg,
lt_stpo TYPE TABLE OF stpo_api02,
lwa_stpo TYPE stpo_api02,
goodsmvt_header TYPE bapi2017_gm_head_01,
goodsmvt_code TYPE bapi2017_gm_code,
goodsmvt_item TYPE TABLE OF bapi2017_gm_item_create,
lwa_goodsmvt_item TYPE bapi2017_gm_item_create,
return TYPE TABLE OF bapiret2,
goodsmvt_headret TYPE bapi2017_gm_head_ret,
materialdocument TYPE bapi2017_gm_head_ret-mat_doc,
matdocumentyear TYPE bapi2017_gm_head_ret-doc_year,
lv_subrc TYPE sy-subrc,
lv_menge TYPE bstmg.

LOOP AT it_mseg INTO lwa_mseg.

CALL FUNCTION 'CSAP_MAT_BOM_READ'
EXPORTING
material = lwa_mseg-matnr
plant = lwa_mseg-werks
bom_usage = '5'
TABLES
t_stpo = lt_stpo
EXCEPTIONS
error = 1
OTHERS = 2.
IF sy-subrc EQ 0 AND lt_stpo[] IS NOT INITIAL.
LOOP AT lt_stpo INTO lwa_stpo.
lv_menge = lwa_mseg-menge * lwa_stpo-comp_qty.
lwa_goodsmvt_item-material = lwa_stpo-component.
lwa_goodsmvt_item-plant = lwa_mseg-werks.
lwa_goodsmvt_item-move_type = '201'.
lwa_goodsmvt_item-entry_qnt = lv_menge.
lwa_goodsmvt_item-mvt_ind = ''.
lwa_goodsmvt_item-stge_loc = lwa_mseg-lgort.
APPEND lwa_goodsmvt_item TO goodsmvt_item.
ENDLOOP.
ENDIF.
ENDLOOP.

IF goodsmvt_item[] IS NOT INITIAL.
goodsmvt_header-pstng_date = sy-datum.
goodsmvt_header-doc_date = sy-datum.
goodsmvt_header-header_txt = 'ATV'.
goodsmvt_code-gm_code = '06'.
* CONCATENATE lwa_mseg-matnr 'ATV' INTO goodsmvt_header-header_txt SEPARATED BY space.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = goodsmvt_header
goodsmvt_code = goodsmvt_code
IMPORTING
goodsmvt_headret = goodsmvt_headret
materialdocument = materialdocument
matdocumentyear = matdocumentyear
TABLES
goodsmvt_item = goodsmvt_item
return = return.
READ TABLE return ASSIGNING FIELD-SYMBOL(<fs_return>)
WITH KEY type = 'E'.
IF sy-subrc EQ 0.
READ TABLE return INTO <fs_return>
WITH KEY id = 'M7'
number = '021'.
IF sy-subrc EQ 0.
MESSAGE e027(zmm) WITH <fs_return>-message_v4+2. "El componente &1 no tiene stock suficiente
ELSE.
MESSAGE e000(zmm) WITH <fs_return>-message.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
ENDIF.

The code runs correctly, but when after executing the BADI there's error M7 260: Required function MB_CREATE_GOODS_MOVEMENT is not being processed.

Can you help me with this? Is there any other way to decrease the stock of the component materials?

Thanks in advance

Carles

1 ACCEPTED SOLUTION

former_member470288
Participant
0 Kudos

Hi,

I've finally decided just to add a validation in the MB_MIGO_BADI where I check if there's enough quantity of each of the component materials. But if I write this validation in the POST_DOCUMENT method, the counter for MIGO goes up each time, even when there's an error.

Where should I put this validation?

Thanks!

Carles

9 REPLIES 9

SimoneMilesi
Active Contributor

Hello, i'm not sure i understand what you are trying to achieve: the good receipt for a Production Order consuming the components?
From the movement (101 - 201) it looks like that... if so, you have to change how you call the BAPI.

PS.

Better you format your code with "CODE" button, so it's easier to read.

DominikTylczyn
Active Contributor

Hello carles.castells

I'm pretty sure that what you are trying to do is to create an additional material document during a material document posting - you are calling BAPI_GOODSMVT_CREATE from within MB_MIGO_BADI~POST_DOCUMENT. You can't do that. BAPI_GOODSMVT_CREATE and MB_* function modules that SAP uses internally to post material documents are implemented in such a way that a commit work has to be executed between posts. You could get a similar error if you tried to post BAPI_GOODSMVT_CREATE twice without commit in between.

You have to separate your additional material document posting to a separate LUW. Another thing you need to implement is error handling in case the main material document posts ok and you additional one fails.

If I were you, I'd move the implementation to a separate RFC enabled function and call it with

CALL FUNCTION func 
   IN BACKGROUND TASK                   
   AS SEPARATE UNIT

See SAP Help on CALL FUNCTION - IN BACKGROUND TASK

I've used this approach several times to automate subsequent postings both in MM and WM - it works.

Another option to implement you requirement, could be a custom output type from a material document. I can post your additional material document in the implementation of the processing routine of the custom output type. It's an elegant solution, however more complex as it requires both configuration and development. It's beyond a scope of simple Q&A.

Last but not least, can't you implement your process with SAP standard functionality? It looks like you explode a BOM and post consumption of the components. That's pretty much like production, isn't it?

Best regards

Dominik Tylczynski

0 Kudos

Hi Dominik,

thanks, we'll do it with a separate RFC enabled function as you indicate.

Thanks to all your helpful answers and have a very Happy New Year!

Carles

nmirandaghn
Participant

Don't by any means use BAPI_TRANSACTION_COMMIT inside this BAdI.

You will have serious troubles in inventory accounts.

Please check SAP Note 968812 - MM/FI Difference for material stocks for more info. especially point 3 and 4

...

(Point 3)
Your system contains active customer enhancements (user exits, customer exits, BAdIs) that cause an additional 'COMMIT WORK' statement to be processed. This is not allowed and results in inconsistencies. See also SAP Notes 1284654, 92550.
SAP Note 1776835 provides an enhanced check logic that can identify such impermissible COMMIT WORK statements and triggers a short dump if required. You must correct the customer-specific source code in any case.

(Point 4)
This case is identical with case 3. However, usually it is not a 'COMMIT WORK' statement that is responsible but a 'ROLLBACK WORK' statement in the customer source code. In particular, method MB_DOCUMENT_BEFORE_UPDATE in BAdI MB_DOCUMENT_BADI creates a ROLLBACK without program termination and an incomplete document because the documents in accounting are created after the BADI. Therefore, a ROLLBACK must always be accompanied by a program termination.
The MM document cannot be regenerated. The accounting documents must be deleted. This can be initiated by a message (which may be subject to charges) on component FO-GL-GL-X. See also Note 636442.

former_member470288
Participant
0 Kudos

Hi,

I've finally decided just to add a validation in the MB_MIGO_BADI where I check if there's enough quantity of each of the component materials. But if I write this validation in the POST_DOCUMENT method, the counter for MIGO goes up each time, even when there's an error.

Where should I put this validation?

Thanks!

Carles

You have "CHECK_ITEM" method to performs checks at item level before the POST or LINE_MODIFY

0 Kudos

Hi Simone,

but in "CHECK_ITEM" I only have I_LINE_ID and ET_BAPIRET2 parameters and they don't have any data, and in "LINE_MODIFY" when the error is shown, it goes again to the "LINE_MODIFY" code.

Did you check in debug which objects you have as available?

You can still create your own implementation objects, fill them in LINE_MODIFY and then perform the checks in your CHECK_ITEM, as it was suggested here

Hi Simone,

it works!! Thanks a lot and have a Happy New Year!

Carles