2014 Sep 26 2:22 PM
Hi,
I have a BOM in which there are infinite loops, it means that a material can contains itself in the following level or in a deeper level.
For example,
(1, 2), (1, 3), (2, 4), (4, 1)
I have seen how recursive works in abap but I don't know how to control infinite loops.
Can anybody help me?
Thanks in advance.
Regards.
2014 Sep 26 2:44 PM
Hi David
You should give us more details, I mean how you think to arrange your LOOP
Max
Hi,
I have a BOM in which there are infinite loops, it means that a material can contains itself in the following level or in a deeper level.
For example,
(1, 2), (1, 3), (2, 4), (4, 1)
I have seen how recursive works in abap but I don't know how to control infinite loops.
Can anybody help me?
Thanks in advance.
Regards.
2014 Sep 26 2:44 PM
Hi David
You should give us more details, I mean how you think to arrange your LOOP
Max
2014 Sep 26 2:56 PM
Hi Max,
I'll try to explain in more detail what I'm looking for.
The loop shoul stop when:
1. I find a raw material (my raw materials begin with m or s)
2. I find a material which hasn't components
3. I find a material that is in the origin of the loop. For example, if I want the list of material of 1 (in the example) the loop shoul stop in the material 4 because it coints the 1 component.
2014 Sep 26 3:46 PM
Perhaps something like this could work:
DATA: BEGIN OF w_bom,
matnr LIKE mast-matnr,
werks LIKE mast-werks,
stlnr LIKE mast-stlnr,
items LIKE STANDARD TABLE OF stpo,
END OF w_bom.
DATA: t_bom LIKE STANDARD TABLE OF w_bom.
PARAMETERS: p_matnr LIKE mast-matnr,
p_werks LIKE mast-werks.
PERFORM get_bom USING p_matnr p_werks.
FORM get_bom USING p1_matnr
p1_werks.
DATA: l_mast TYPE mast.
DATA: l_stpo TYPE stpo.
* Check if BOM material was loaded
READ TABLE t_bom TRANSPORTING NO FIELDS WITH KEY matnr = p1_matnr
werks = p1_werks.
CHECK sy-subrc <> 0.
* Check no raw material
CHECK p1_matnr(1) <> 'M' AND p1_matnr <> 'S'.
* Check BOM
SELECT * INTO l_mast
FROM mast
WHERE matnr = p1_matnr
AND werks = p1_werks.
ENDSELECT.
CHECK sy-subrc = 0.
CLEAR w_bom.
FREE w_bom-items.
*
w_bom-matnr = l_mast-matnr.
w_bom-werks = l_mast-werks.
w_bom-stlnr = l_mast-stlnr.
SELECT * INTO TABLE w_bom-items
FROM stpo
WHERE stlty = 'M'
AND stlnr = l_mast-stlnr.
CHECK sy-subrc = 0.
APPEND w_bom TO t_bom.
* Check next level
LOOP AT w_bom-items INTO l_stpo.
PERFORM get_bom USING l_stpo-idnrk l_mast-werks.
ENDLOOP.
ENDFORM.
The routine GET_BOM should call itself until the conditions to stop the search are satisfied, I can't try this code but you can try it or something like that.
2014 Sep 26 4:04 PM
Thanks Max for the example. I'll try the example with my BOM.
I haven't still run the example but, looking the code what I don't see is, where do you control the if the current material had appeared before in the loop?
I will use one of my materials with infinite loop to see what happens.
Thanks again.
2014 Sep 26 4:19 PM
* Check if BOM material was loaded
READ TABLE t_bom TRANSPORTING NO FIELDS
WITH KEY matnr =p1_matnr
werks = p1_werks.
CHECK sy-subrc <> 0.
The main table T_BOM (or something like that) is to store the bom unread
If the current material is a bom read in previous level, the search will be stopped else continue
w_bom-matnr = l_mast-matnr.
w_bom-werks = l_mast-werks.
w_bom-stlnr = l_mast-stlnr.
SELECT * INTO TABLE w_bom-items
FROM stpo
WHERE stlty = 'M'
AND stlnr = l_mast-stlnr.
CHECK sy-subrc = 0.
APPEND w_bom TO t_bom.
I mean it needs a support where to store the results of previous levels
Ma
2014 Sep 26 4:07 PM
Hi,
Looks like chemical industry.
Try this:
Create internal table of matnr lets call it it_comp_1 .
Logic:
loop on root materials
for each:
- Clear it_comp_1 .
- Add the root matnr to it_comp_1 .
- When you traverse the tree fill check if the it_comp_1 contain the component if yes return without going any deeper.
- if not add the component to it_comp_1 and go deeper.
For your knowledge there are standard functions that can be used,
Do a google using search operators : site:scn.sap.com bom explosion functions
We use this: CS_BOM_EXPL_MAT_V2
Regards.
2014 Sep 26 4:13 PM