Skip to Content

Finding the Condition record for condition type is really challenging.

For particular Condition Type there can be many condition tables.

Each condition table has access sequence and different key combinations.

For example if we go to VK13 transaction we see the available condition records available.

Below are the code for accessing the codition record:

  1. Build the internal table with possible key field names and values as below:

 

  1. Call the Function module SD_COND_T685_SELECT as below:

* get the details about condition type
CALL FUNCTION ‘SD_COND_T685_SELECT’
EXPORTING
cts_kappl = ‘V’        “Application
cts_kschl = ‘ZKRC’     “Condition type
cts_kvewe = ‘A’        “Use
IMPORTING
cts_t685  = lwa_t685.     “Conditions: Types

  1. Use the access sequence returned i.e. lwa_t685-kozgf for selecting records from access details table i.e. T682I table.

* Get the Access sequence details
SELECT * FROM t682i INTO TABLE lit_t682i
WHERE kvewe = lv_kvewe
AND kappl = lv_kappl
AND kozgf = lwa_t685-kozgf.

  1. The condition table names will get by concatenating Use and Condition table

Ex: use is ‘A’ and condtion table is ‘599’ then table to aceess is ‘A599’.

 

 

  1. Loop all the condtion tables and find the key fields for those from DD03L table

Build dynamically the key fields and their values from using DD03L and Keyfields internal table.

 

  1. Get the condition record number from condition tables
  2. Get the price from KONP table for the condition record number .
BEGIN OF t_keyfields,               " Key fields for codition records
     fname        TYPE fieldname,      " Key field name
     fvalue       TYPE char30,         " field value
   END OF t_keyfields.

  DATA:
    lwa_t685      TYPE t685,
    lwa_keyfields TYPE t_keyfields,
    lit_keyfields TYPE TABLE OF t_keyfields,
    lwa_t682i     TYPE t682i,
    lit_t682i     TYPE TABLE OF t682i,
    lwa_dd03l      TYPE dd03l,
    lit_dd03l      TYPE TABLE OF dd03l,
    lv_kappl       TYPE kappl VALUE 'V',
    lv_kschl       TYPE kschl VALUE 'ZGRC',
    lv_kvewe       TYPE kvewe VALUE 'A',
    lv_fvalue      TYPE c LENGTH 52,
    lv_tabname     TYPE tabname,
    lv_keynotfound TYPE c,
    lv_initwhere   TYPE c LENGTH 100,
    lv_wherecond   TYPE c LENGTH 300,
    lv_kbetr       TYPE kbetr,
    lv_knumh       TYPE knumh,
    lv_kdgrp       TYPE kdgrp,
    lwa_sales_hd  TYPE bapisdhd.
*Prepare the key fields
* Application
  lwa_keyfields-fname  = 'KAPPL'.
  lwa_keyfields-fvalue = 'A'.
  APPEND lwa_keyfields TO lit_keyfields.

* Condition Type
  lwa_keyfields-fname  = 'KSCHL'.
  lwa_keyfields-fvalue = 'ZKRC'.
  APPEND lwa_keyfields TO lit_keyfields.

*   Sales Orgnization
  IF lwa_sales_hd-sales_org IS NOT INITIAL.
    lwa_keyfields-fname  = 'VKORG'.
    lwa_keyfields-fvalue = '0010'.
    APPEND lwa_keyfields TO lit_keyfields.
  ENDIF.
*   Sales Division
  IF lwa_sales_hd-distr_chan IS NOT INITIAL.
    lwa_keyfields-fname  = 'VTWEG'.
    lwa_keyfields-fvalue = '00'.
    APPEND lwa_keyfields TO lit_keyfields.
  ENDIF.

* Plant
  IF g_wa_ord_header-plant IS NOT INITIAL.
    lwa_keyfields-fname  = 'WERKS'.
    lwa_keyfields-fvalue = 'AR00'.
    APPEND lwa_keyfields TO lit_keyfields.
  ENDIF.

* Customer / Sold-to-party
  IF g_hd_kunnr IS NOT INITIAL.
    lwa_keyfields-fname  = 'KUNNR'.
    lwa_keyfields-fvalue = '0010101023'.
    APPEND lwa_keyfields TO lit_keyfields.
  ENDIF.

* Material
  IF g_td_matnr IS NOT INITIAL.
    lwa_keyfields-fname  = 'MATNR'.
    lwa_keyfields-fvalue = 'MAT1234'.
    APPEND lwa_keyfields TO lit_keyfields.
  ENDIF.

* get the details about condition type
  CALL FUNCTION 'SD_COND_T685_SELECT'
    EXPORTING 
      cts_kappl = 'V'        "Application
      cts_kschl = 'ZKRC'     "Condition type
      cts_kvewe = 'A'        "Use
    IMPORTING
      cts_t685  = lwa_t685.     "Conditions: Types


* Get the Access sequence details
  SELECT * FROM t682i INTO TABLE lit_t682i
                           WHERE kvewe = lv_kvewe
                             AND kappl = lv_kappl
                             AND kozgf = lwa_t685-kozgf.
  SORT lit_t682i BY kotabnr.
  DELETE ADJACENT DUPLICATES FROM lit_t682i COMPARING kotabnr.

  SORT lit_t682i BY kolnr.

* Build the initial where condition for condition tables
  CONCATENATE `'` lv_kappl `'` INTO lv_fvalue.
  CONCATENATE 'KAPPL' '=' lv_fvalue INTO lv_initwhere
                           SEPARATED BY space.

  CONCATENATE `'` lv_kschl `'` INTO lv_fvalue.
  CONCATENATE lv_initwhere
              'AND'
              'KSCHL'
              '='
              lv_fvalue
         INTO lv_initwhere
  SEPARATED BY space.

* Loop the condition tables
  LOOP AT lit_t682i INTO lwa_t682i.
    CLEAR: lv_tabname,lit_dd03l,lv_keynotfound,lv_wherecond,lv_kbetr.

    CONCATENATE lwa_t682i-kvewe lwa_t682i-kotabnr INTO lv_tabname.

*Key fields of tabname
*get the key field information from DD03L table
    SELECT * FROM dd03l INTO TABLE lit_dd03l
                             WHERE tabname = lv_tabname
                               AND keyflag = gc_x.
* Remove the client field as it is passed default
    DELETE lit_dd03l WHERE fieldname = 'MANDT'.

* Remove the application as we already build above
    READ TABLE lit_dd03l INTO lwa_dd03l WITH KEY fieldname = 'KAPPL'.
    IF sy-subrc EQ 0.
      DELETE lit_dd03l INDEX sy-tabix.
    ELSE.
      CONTINUE.
    ENDIF.

* Remove the Condition type
    READ TABLE lit_dd03l INTO lwa_dd03l WITH KEY fieldname = 'KSCHL'.
    IF sy-subrc EQ 0.
      DELETE lit_dd03l INDEX sy-tabix.
    ELSE.
      CONTINUE.
    ENDIF.

* Remove the valid to field as have given select query
    READ TABLE lit_dd03l INTO lwa_dd03l WITH KEY fieldname = 'DATBI'.
    IF sy-subrc EQ 0.
      DELETE lit_dd03l INDEX sy-tabix.
    ELSE.
      CONTINUE.
    ENDIF.

* Initiate the building of dynamic where condition
    lv_wherecond = lv_initwhere.

    LOOP AT lit_dd03l INTO lwa_dd03l.
*  Concatenate the fields and values for key
      READ TABLE lit_keyfields INTO lwa_keyfields
                           WITH KEY fname = lwa_dd03l-fieldname.
      IF sy-subrc EQ 0.
        CONCATENATE `'` lwa_keyfields-fvalue `'` INTO lv_fvalue.

        CONCATENATE lv_wherecond
                    'AND'
                    lwa_keyfields-fname
                    '='
                    lv_fvalue
               INTO lv_wherecond
       SEPARATED BY space.
      ELSE.
        lv_keynotfound = 'X'.
        EXIT.
      ENDIF.
    ENDLOOP.

* skip the search in table if any of the key field missing
    IF lv_keynotfound IS INITIAL AND sy-subrc EQ 0.

* Retrieve the condition record number
      SELECT SINGLE knumh FROM (lv_tabname)
                          INTO lv_knumh
                         WHERE (lv_wherecond)
                           AND datbi >= sy-datum.
      IF sy-subrc EQ 0.
*    get the Price from Conditions Item table
        SELECT kbetr INTO lv_kbetr UP TO 1 ROWS
                                    FROM konp
                                   WHERE knumh = lv_knumh.
        ENDSELECT.
        IF sy-subrc EQ 0 AND lv_kbetr NE 0.
*      display the price if found and exit the search
          g_td_grc_pric = lv_kbetr.
          EXIT.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.
To report this post you need to login first.

Be the first to leave a comment

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

Leave a Reply