Automatically Populate Select-Options In Selection Screen

Jimbo's picture

This example comes from an LSMW object that facilitated the cleanup of duplicate Customer Masters from a client's production system by monitoring those blocked for sales, those that were hidden and those with a popup message instructing users to transact with the customer through the survivor Customer Master to see when all of the associated transactional documents had been settled. The LSMW object initially took a list copy-pasted from the spreadsheet used first to analyze the Customer Masters and then to keep track of those that had been blocked and sunset.

To save time and eliminate the risk of human error, the process was automated by pulling the list of Customer Masters from the KNA1 table based on the notes in the Train Station (BAHNS) field and the various blocking and deletion flags. The Customer Masters that have the popup message were then found and added to the list.

A Select-Options variable is little more than an internal table used as a parameter with some Domain filtering where the parameter is based on some field in SAP. As such, it can be manipulated in ABAP with a small snippet of code.

Start by declaring the Select-Option in the GLOBAL_DATA section of the LSMW Field Mapping and Conversion Rules. It has to be of a type from SAP, so that is declared here with an internal table that will be used to find Customer Masters with the special popup message.

data: lKUNNR like KNA1-KUNNR.
data: it_STXH type standard table of STXH with header line,
      it_TLINE type standard table of TLINE with header line.
select-options: s_KUNNR for lKUNNR.

During the INITIALIZATION, a simple SELECT statement finds Customer Masters that have been blocked or hidden. The BAHNS field is almost never used, so it is a great place to store discreet keys without risk.

The fields of the s_KUNNR internal table are populated here in order to make a list of individual Customer Masters. The SIGN field is populated with an "I" to indicate that this is an include and the OPTION field is populated with "EQ" so the system knows to pull all records that equal this value. Finally, the LOW field is populated with the Customer Master number and the record is appended to the internal table.

initialization.
  select KUNNR from KNA1 into lKUNNR
   where ( LOEVM eq 'X' or SPERR eq 'X' or AUFSD ne '' )
     and BAHNS ne ''.
    s_KUNNR-SIGN = 'I'. s_KUNNR-OPTION = 'EQ'. s_KUNNR-LOW = lKUNNR.
    append s_KUNNR.
  endselect.

Now the code populates and then loops through the it_STXH internal table to find occurrences of Customer Masters with popup texts. Where the Customer Master has the key phrase added to duplicates identified by the "Master Data Team", it too is added to the internal table just like above.

  "Let's include those with the sunsetting popup message...
  select * from STXH into table it_STXH
   where TDID eq 'ZAR' and TDOBJECT eq 'KNVV'.
  loop at it_STXH.
    CALL FUNCTION 'READ_TEXT'
      EXPORTING
        CLIENT                  = SY-MANDT
        ID                      = it_STXH-TDID
        LANGUAGE                = it_STXH-TDSPRAS
        NAME                    = it_STXH-TDNAME
        OBJECT                  = it_STXH-TDOBJECT
      TABLES
        LINES                   = it_TLINE
      EXCEPTIONS
        ID                      = 1
        OTHERS                  = 8.
    loop at it_TLINE.
      if it_TLINE-TDLINE cs 'Master Data Team'. "Our popup!!!!
        s_KUNNR-SIGN = 'I'. s_KUNNR-OPTION = 'EQ'.
        s_KUNNR-LOW = it_STXH-TDNAME+0(10). "Customer Master Number.
        append s_KUNNR.
      endif.
    endloop.
  endloop.

Finally, the data is sorted and cleansed of duplicate records. This takes only a few microseconds and is great form.

  sort s_KUNNR.
  delete adjacent duplicates from s_KUNNR.

This snippet only shows how to populate the internal table with records that are included if they equal a value, but it can be used to exclude results and use the other operands like LT, GT, LE, GE, NE. A little experimentation will go a long way.

Load Characteristic Values with a BAPI

Programming Language: 
ABAP