Skip to Content
avatar image
-3
Former Member

Function Module with optional parameter

Hi Gurus,
I have a requirement where i have to fetch the records from MARC table based on matnr ( importing parameter - Optional ) and werks as table parameter .
When i am writing the select query on MARC , if the material is blank and plant is having data , still the select query is failing.
I want to get even if the material is blank , the select query should get all the materials under that plant.
Kindly help in this regard.
Thanks in Advance.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Dec 21, 2016 at 12:47 PM

    Firstly, you can check if a parameter is provided to a function module by using the 'IS SUPPLIED' clause. For exporting parameters its' IS REQUIRED'.
    I would code your select something like:

    If it_Werks Is Not Initial.
       If I_Matnr Is Supplied.
          Select [field-list]
            Into Table et_Results
            From Marc
             For All Entries in it_Werks
           Where Matnr = I_Matnr And
                 Werks = it_Werks-Werks.
       Else.  
          Select [field-list]
            Into Table et_Results
            From Marc
             For All Entries in it_Werks
           Where Werks = it_Werks-Werks.
       EndIf.
    EndIf.


    Regards.

    Rich

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 03, 2017 at 08:10 PM

    If you have a material as a single value parameter then it does not seem logical to make it optional and return the plant-ful of materials when it's blank. It is not like parameters work in the reports and it wouldn't be intuitive to someone using such function IMHO. If you are going in this direction then maybe it'd make sense to pass to the function a table that could contain 1 or more material numbers or be blank. This would work like SELECT-OPTIONS and you could even use a range table type for more complex criteria. In this way at least you'll have more robust function.

    There are also some memory and security considerations, of course.

    Overall this is a rather simple ABAP task, next time you might want to try something yourself first before posting a question on SCN. At least you'd be able to post more specific question based on your experiments.

    Add comment
    10|10000 characters needed characters exceeded

  • Dec 22, 2016 at 07:26 PM

    If the material is blank in your query, it is looking for a MARC entry with no material - I don't think that is going to happen. To keep the select statement and therefore its error handling together in a single piece, you could combine the recommendations of Igor and Richard. Use a range for the material instead of the SELECT-OPTIONS. Then use the "IS SUPPLIED" as suggested by Richard to add records to the range.

    In this sample code, some of it (non-relevant to the question at hand) is specific to ABAP 7.4 and above, but can easily be replaced by other older ABAP commands.

        DATA:
          it_matnr TYPE RANGE OF matnr,
          it_werks TYPE RANGE OF werks_d,
          lr_matnr LIKE LINE OF it_matnr,
          lr_werks LIKE LINE OF it_werks.
    
    
        IF i_matnr IS SUPPLIED.
          it_matnr = VALUE #( ( sign = 'I' option = 'EQ' low = i_matnr ) ).
        ENDIF.
    
    
        LOOP AT i_werks_tab INTO DATA(l_werks).
          lr_werks = VALUE #( sign = 'I' option = 'EQ' low = l_werks ).
          APPEND lr_werks TO it_werks.
        ENDLOOP.
    
    
        SELECT * FROM marc
          WHERE matnr IN @it_matnr
          AND werks IN @it_werks
          INTO TABLE @DATA(et_results).
    Add comment
    10|10000 characters needed characters exceeded

  • Dec 21, 2016 at 12:05 PM

    Hello,

    I suppose that you are using '=' in your query. Try the query with 'in' clause and you should get the results. Use 'select-options' instead of 'parameters'.

    Example:

    SELECT-OPTIONS: p_matnr FOR matnr NO INTERVALS.
    "some code
    select matnr werks
    into (l_matnr, l_werks)
    from MARC
    where matnr in p_matnr
    "some code
    endselect.
    "some code

    Regards,

    Igor

    Add comment
    10|10000 characters needed characters exceeded