Skip to Content

Dear All ,

Its a very common requirement in almost all scenarios wherever SAP MM is there , that we need to calculate or get the Opening and Closing quantities of the Stock of Material(s) in Custom reports ( for eg. Stock Reconciliation Report , Stock Movement Report etc ) , Forms …. for a particular date.

And nothing can provide us with accuracy of MB5B ( Standard Report for Stock on Posting Date ) in this case.

Although there are many approaches for this like , can be calculated by doing operations over all the Material Documents generated for a Material on a particular date , field  MARD-LABST provides with the opening of stock on particular date etc. But getting these detail with the accuracy of MB5B is more desirable , accurate and informative as well.

While viewing Standard Report MB5B’s code (RM07MLBD) you can see an internal table g_t_totals_flat , this is the internal table which is displayed as the output of MB5B for a particular selections made.

So its very obvious that it contains all the necessary data such as Opening Stock Qty , Closing Stock Qty , Plant , Valution Class (Same as Plant ) , Dates ,Unit Of Material etc.

So am using EXPORT IMPORT between MEMORY ID technique to get this data wherever required.

Steps for accomplishing this task are as follows :

1. Exporting the contents of g_t_totals_flat to a particular MEMORY ID from MB5B.

  

For this purpose you will need to Implicitly enhance the standard code of MB5B (RM07MLBD).

For learning how to IMPLICIT ENHANCEMENT you can refer this thread http://wiki.sdn.sap.com/wiki/display/ABAP/How+To+Do+Implicit+Enhancement

Write your code from line number 1995 ( after the loop inside which append g_s_totals_flat to g_t_totals_flat command is executed )

For eg :

Capture 2.JPG

NOTE : Its recommended to write a LEAVE or LEAVE PROGRAM statement followed by EXPORT statement to avoid unwanted execution of the called program after getting the EXPORT done ( Can be written inside SY-SUBRC check ).

Now whenever MB5B will be executed g_t_totals_flat data will be copied to IT_TOTAL and EXPORTED to this MEMORY ID.

2. Importing the contents of IT_TOTAL from a particular MEMORY ID through a Custom Report ( Code ).

For this purpose you will need to use SUBMIT command and thereafter use IMPORT command.

For learning about SUBMIT in detail you can refer this thread http://help.sap.com/abapdocu_70/en/ABAPSUBMIT_LIST_OPTIONS.htm

Write your code like this example :

Capture 3.JPG

Here SUBMIT command with EXPORTING LIST TO MEMORY AND RETURN addition will call MB5B in background and thereby  g_t_totals_flat will be copied to IT_TOTAL and Exported to MEMORY ID.

And after this SUBMIT command we can IMPORT IT_TOTAL from MEMORY ID.

WITH addition in SUBMIT command can be used to provide the various desired selections which we provide to MB5B.

Eg : we set LGBST EQ ‘X’ in the above code to get the below selection.

Capture 4.JPG

Likewise you can add more as per your requirement.

Fields ANFMENGE and ENDMENGE of IT_TOTAL will give you the OPENING and CLOSING quantities repectively.

Rest of the material movement quantities can be calculated and segregated By fetching data from MSEG and processing them using different MOVEMENT TYPES.

I think that performance issue will not be prevailing that much by calling MB5B using SUBMIT , reason being its a very quickly executed report accessing mainly MKPF and MSEG and the output is also using a Basic List.

Hope this will provide with solutions to many.

Thanks and Regards ,

Sijin.

To report this post you need to login first.

27 Comments

You must be Logged on to comment or reply to a post.

  1. RITWIK GHOSH

    Hi Sijin,

    Great job!

    Thank you so much for putting such a valuable article on MB5B (RM07MLBD). It is really Excellent.

    I just want put a small comment here. I achieved the same in one of my earlier requirement w/o doing the enhancement. I was able to get the Opening Balance (ANFMENGE) and Closing Balance (ENDMENGE) by leveraging two function modules ‘LIST_FROM_MEMORY” and ‘LIST_TO_ASCII’. The last FM converts the memory line in text lines and then splitting the text line into fields like Plant, Material Number, Start Date, End Date, Opening Balance, Total Receipt, Total Issue and Closing balance. These fields are separated by Pipe ‘|’.

    (0) 
    1. Sijin Chandran Post author

      Hi RITWIK ,

      Thanks.

      Actually for accomplishing my task I used LIST_FROM_MEMORY and LIST_TO_ASCII but I was not able to copy it to an internal table in a proper format.

      That’s why I went for this option.

      (0) 
      1. S Kumar2

        Dear Concern,

        Thanks For Prompt Reply ,

        Above Logic for Storage location / Batch Stock but i “Required for Valuated Stock T Code MB5B

        Please Do NeedFull

        Regards

        Suresh

        (0) 
  2. Prasanna K

    Hi Sijin ,

    Great job!

    Am sure this will be helpful for many in SCN Forums.

    As I have seen many related threads asking for FMs and other codes for the calculating opening and closing of a material.

    (0) 
      1. S Kumar2

        Dear Concern,

        Thanks For Prompt Reply ,

        Above Logic for Storage location / Batch Stock but i “Required for Valuated Stock T Code MB5B

        Please Do NeedFull

        Regards

        Suresh

        (0) 
  3. A$HI$H $AP

    Dear Sijin

    I think this report will called stock reconciliation because its a great way to run both MB5B and MB51 with column wise but the concept you used with MB5B will give  pin point accuracy and it will be very easy for the management to take decisions.

    In the end I would like to know can we go little deeper I mean storage location wise and Batch Wise.

    Thanking you!

    Ashish Mishra

    (0) 
    1. Sijin Chandran Post author

      Hi Ashish ,

      Yes this can be used for preparing stock reconciliation report.

      Yeah we can get batchwise and storage location wise classification as well.

      You just need to call MB5B (using SUBMIT ) accordingly and likewise loop with MSEG inside the ZREPORT also.

      And it will give you the desired result.

      (0) 
  4. Zakir Appas

    Hi Sijin,

    I am facing a problem in Memory ID. I have followed the steps what you have mention in the document. In RM07MLBD program i m getting the values in the internal table and exporting the same to memory id but when i am going for custom program the memory id is not importing any values. I have written the enhancement point on form alv_flat_list_sums_only as i m getting values.Please have a look on my code below and suggest me on this.

    Custom Program:

    SUBMIT rm07mlbd WITH SELECTION-TABLE k_rsparams

                              WITH FREE SELECTIONS texpr

                              AND RETURN.

       IMPORT it_total_item FROM MEMORY ID ‘ZMB5B_ITEM’.

    **RM07MLBD Enhancement Point

    DATA: IT_TOTAL TYPE STAB_MSEG_LEAN.

      IT_TOTAL[] = G_T_MSEG_LEAN.

      FREE MEMORY ID ‘ZMB5B_ITEM’  .

    * EXPORT IT_TOTAL TO MEMORY id ‘ZMB5B_ITEM’  .

      EXPORT IT_TOTAL TO MEMORY ID ‘ZMB5B_ITEM’.

    thanks and regards,

    Zakir Appas A

    (0) 
    1. Sijin Chandran Post author

      Hi Zakir ,

      Even I had also faced this problem .

      I did the complete execution of my custom zprogram using debugger and it started working fine.

      Try this it may work out , if data is getting exported properly and the problem is with import statement.

      Before that also run MB5B .

      (0) 
      1. Zakir Appas

        Hi Sijin,

        Thanks for your response. I have debugged my custom program and i m not able to sort the issue. First i m running MB5B program and exporting the values in MB5B through the enhancement point and values are properly populating there in enhancement, when i come back to my custom program after the enhancement the values are not importing from memory id.

        (0) 
        1. Sijin Chandran Post author

          Zakir ,

          Check whether your program is able to call MB5B properly or not.

          Keep a breakpoint at your code in MB5B and after that run your zprogram and check.

          (0) 
  5. Zakir Appas

    Sijin,

    Ya my custom program is calling MB5B correctly .I have kept break point in MB5B and checked , its exporting the value properly to the memory id, after execution of MB5B in my custom program import is not getting the values. 

    (0) 
    1. Sijin Chandran Post author

      Zakir ,

      EXPORT IT_TOTAL TO MEMORY ID ‘ZMB5B_ITEM’.

      IMPORT it_total_item FROM MEMORY ID ‘ZMB5B_ITEM’.

      Try with giving both names for internal same ,

      i.e

      EXPORT IT_TOTAL TO MEMORY ID ‘ZMB5B_ITEM’.


      IMPORT IT_TOTAL FROM MEMORY ID ‘ZMB5B_ITEM’.


      (0) 
  6. Sijin Chandran Post author

    It is as simple as that !

    While calling the submit ,

    SUBMIT RM07MLBD WITH MATNR  IN SO_MATNR                     ” Material Number

                        WITH WERKS  IN SO_WERKS                     ” Plant

                        WITH MTART  IN SO_MTART                     ” Material type

    *                   WITH LGORT  IN SO_LGORT                     ” Storage Location

                        WITH DATUM  IN SO_BUDAT                     ” Selection Date

                        WITH LGBST  EQ ‘X’                          ” Storage Loc/ Batch Stock

                        WITH BWBST  EQ ‘ ‘                          ” Valuated Stock

                        WITH SBBST  EQ ‘ ‘                          ” Special Stock

                        WITH PA_SUMFL EQ ‘X’                        ” totals only – non heirarchical representation

                        WITH XCHAR EQ ‘ ‘

                        WITH XNOMCHB EQ ‘ ‘

    *                   WITH XCHAR EQ XCHAR                         ” only matls handled in batches

    *                   WITH XNOMCHB EQ XNOMCHB                     ” also batches w/o stock segment

                        EXPORTING LIST TO MEMORY

                        AND RETURN.

    Instead of setting of the value of LGBST  EQ ‘X’ set BWBST EQ ‘X’

    (0) 
    1. Sijin Chandran Post author

      Hello Abhishek ,

      When I posted this document I was in my earlier company and in that system that code was at line number 1995.

      Now following your statement when I checked this thing in my current organization I found that this code is at line number 1986.

      The first thing which came in my mind was that this could be due to any implicit enhancement carried out for MB5B code , as it will result in reordering of codes.

      But till now I am unable to find any such enhancements with MB5B in my current organization.

      In short am clueless for this .

      (0) 
      1. Manish Kumar

        Sijin and Abhishek, compare your support pack levels.

        Either that, or additional SAP note implementations would be the root cause for varying line number.

        (0) 
  7. Vishwaroop Prathap

    Hello sijin,

    i am getting this dump while importing internal table from memory id

    Runtime Errors :  CONNE_IMPORT_WRONG_OBJECT_TYPE

    Except.              CX_SY_IMPORT_MISMATCH_ERROR

    (0) 

Leave a Reply