Search code examples
abapsap-query

Converting MATNR via conversion exit fails for custom table


I'm trying to select the latest date of a material movement from MSEG, but the material needs to be in stock and that is sourced from a bespoke table which uses unconverted Material names.

I've tried using the CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' (and INPUT) but I'm not sure how to properly utilize it in a select statement.

IF MSEG-BWART = '101'.

  CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
    EXPORTING
      INPUT  = ZBJSTOCK-ZMAT10
    IMPORTING
      OUTPUT = WA2-MATNR.

  SELECT MAX( BUDAT_MKPF )
  FROM MSEG
  INTO GRDT
  WHERE MATNR = WA2-MATNR.

ENDIF.

Currently, WA2-MATNR seems to come out as blank and therefore is not pulling the data from MSEG.


Solution

  • You shouldn't use conversion exit here. Material number in SAP tables lays in internal (INPUT) format and you are converting it into readable format (OUTPUT) in order to query table. It is obvious you will not find anything.

    Sample:

    MATNR internal format (for OUT exits)

    000000000000025567

    MATNR external format (for IN exits)

    25567

    Conversions cases:

    000000000000025567 -> CONVERSION_EXIT_MATN1_OUTPUT -> 25567 ✔️

    25567                           -> CONVERSION_EXIT_MATN1_OUTPUT -> 25567 ❌ nothing changes

    25567                           -> CONVERSION_EXIT_MATN1_INPUT -> 000000000000025567 ✔️

    000000000000025567 -> CONVERSION_EXIT_MATN1_INPUT -> 000000000000025567 ❌ nng changes

    Most likely, your bespoke table contains faulty material number so exit doesn't return anything. Or material number in format that exit doesn't expect, e.g. 19 characters instead of 18 and so on.

    P.S.

    Just for you info, you can use templates for conversion. It is the same as calling conversion FMs

    SELECT SINGLE matnr FROM mara INTO @DATA(l_matnr) WHERE EXISTS ( SELECT * FROM mseg WHERE matnr = mara~matnr ).
    
    l_matnr = | { l_matnr ALPHA = OUT } |. <<-- templating
    
    SELECT SINGLE matnr, budat_mkpf
      FROM mseg
      INTO @DATA(l_mkpf)
      WHERE matnr = @l_matnr.
    

    In the above sample SELECT will not return anything, but if you comment out the template line it will.