顯示具有 Code 標籤的文章。 顯示所有文章
顯示具有 Code 標籤的文章。 顯示所有文章

2013年2月4日 星期一

[Code] How to catching selection criteria

How to catching selection criteria

Example: 
  DATAselection_table LIKE rsparams OCCURS 0  WITH HEADER LINE.

  CLEAR selection_tableREFRESH selection_table.

* Get selection criteria
  CALL FUNCTION 'RS_REFRESH_FROM_SELECTOPTIONS'

[Code] How to reference whole itab by name

How to reference whole itab by name

Example: clearing all selection criteria
FORM screen_clearing_all .
  DATAselctab         LIKE rsscr    OCCURS 20 WITH HEADER LINE,
        itabname        TYPE c        LENGTH 30.

  FIELD-SYMBOLS<para> TYPE ANY,
                 <sele> TYPE ANY TABLE.

  CLEAR selctabREFRESH selctab.

* Get selection screen

2013年1月31日 星期四

[Code] How to use subquery to get correct record


Suppose ZSD001 is filled as below:
MATNR         DATAB           APPOINTFEE
----------------------------------------
MA12345       2008/12/01      500
MA12345       2009/12/01      1,000
MA12345       2009/12/15      2,000
Example Code:
SELECT SINGLE APPOINTFEE INTO GT_ITAB-APPOINTFEE
    FROM ZSD001
   WHERE MATNR = 'MA12345'
     AND DATAB = ( SELECT MAX( DATAB )
                       FROM ZSD001
                      WHERE MATNR EQ 'MA12345'
                        AND DATAB LE '20091202'
                 ).

* GT_ITAB-APPOINTFEE == 1,000

Another example: (paste from RVAUFSTA)
SELECT OBJNR FROM JSTO AS APPENDING CORRESPONDING FIELDS OF
                     TABLE ONR_TAB WHERE A~OBJNR LIKE SDCON-OBJNR_VB AND
                                         A~OBJNR IN R_S_OBJNR AND
                                         A~OBTYP EQ SDCON-HEADER AND
                                         A~STSMA NE STSMA_INI AND
             EXISTS SELECT OBJNR FROM JEST WHERE OBJNR EQ A~OBJNR AND
                     STAT LIKE 'E%' AND INACT EQ SPACE ORDER BY OBJNR.

2011年9月30日 星期五

[Func] TH_POPUP

跳出通知訊息窗

By Client + User ID :
  DATA: client  LIKE sy-mandt,
        user    LIKE sy-uname,
        message LIKE sm04dic-popupmsg

  CALL FUNCTION 'TH_POPUP'
    EXPORTING
      client               = client
      user                 = user
      message              = message

[BAPI] BAPI_CONTRACT_CREATEFROMDATA

使用 BAPI 建立 Contract

定義
* BAPI Define
  DATA: gw_so_header          LIKE bapisdhd1,
        gw_so_headerx         LIKE bapisdhd1x,
        gw_so_logic_switch    LIKE bapisdls,
        gv_so_doc_in          TYPE bapivbeln-vbeln,
        gv_so_doc_num         TYPE bapivbeln-vbeln,
        gt_so_return2         TYPE bapiret2,
        gt_so_partners        TYPE bapiparnr  OCCURS 0 WITH HEADER LINE,
        gt_so_items_in        TYPE bapisditm  OCCURS 0 WITH HEADER LINE,
        gt_so_items_inx       TYPE bapisditmx OCCURS 0 WITH HEADER LINE,
        gt_so_return1         TYPE bapiret2   OCCURS 0 WITH HEADER LINE,
        gt_so_conditions_in   TYPE bapicond   OCCURS 0 WITH HEADER LINE,
        gt_so_conditions_inx  TYPE bapicondx  OCCURS 0 WITH HEADER LINE.

夥伴功能

2011年9月14日 星期三

[ABAP] Connection Keeper for network idle

當閒置時間過長連線中斷

有時是因為系統參數 rdisp/gui_auto_logout 的關係,有時是因為網路設備的關係,當閒置時間超過設定時,就會中斷連線。

如果可以的話,修改系統參數、網路設備設定,是最直接有效的。

如果不行的話,可以參考底下的程式。執行時輸入訊息傳輸的間隔秒數,要少於設定斷線的閒置秒數。


2011年6月14日 星期二

[Code] READ_TEXT

SO Header
DATA  BEGIN OF ITLINE OCCURS 0.
      INCLUDE STRUCTURE TLINE.
DATA  END OF ITLINE.
DATA  G_VGBEL LIKE THEAD-TDNAME.

G_VGBEL = VBAK-VBELN.

CALL FUNCTION 'READ_TEXT'
  EXPORTING
    ID       = '0001'
    LANGUAGE = 'M'
    NAME     = G_VGBEL
    OBJECT   = 'VBBK'
  TABLES
    LINES    = ITLINE
  EXCEPTIONS
    ID       = 1
    OTHERS   = 2.
SO Item
DATA  BEGIN OF ITLINE OCCURS 0.
      INCLUDE STRUCTURE TLINE.
DATA  END OF ITLINE.

DATA  G_VGBEL LIKE THEAD-TDNAME.

CONCATENATE VBAP-VBELN VBAP-POSNR G_VGBEL.

CALL FUNCTION 'READ_TEXT'
  EXPORTING
    ID       = '0001'
    LANGUAGE = 'M'
    NAME     = G_VGBEL
    OBJECT   = 'VBBP'
  TABLES
    LINES    = ITLINE
  EXCEPTIONS
    ID       = 1
    OTHERS   = 2.

[Func] Converting Material Quantities To Different Unit Of Measure

單位轉換
Example code:
IF GT_ITAB-VRKME EQ GT_ITAB-KMEIN OR
   GT_ITAB-KMEIN EQ SPACE.
  LV_LFIMG = GT_ITAB-LFIMG.
ELSE.
  CALL FUNCTION 'MATERIAL_UNIT_CONVERSION'
    EXPORTING
      INPUT    = GT_ITAB-LFIMG
      KZMEINH  = 'X'
      MATNR    = GT_ITAB-MATNR
      MEINH    = GT_ITAB-VRKME
      MEINS    = GT_ITAB-KMEIN
      TYPE_UMR = '3'
    IMPORTING
      OUTPUT   = LV_LFIMG.
ENDIF.

[Code] Bills of material in the BAPI for create SO


使用 BAPI 建立含 BOM 的銷售訂單,重點在於先使用 BAPI_SALESORDER_SIMULATE 算出所有的 item no, 另外就是呼叫 BAPI_SALESORDER_CREATEFROMDAT2 時要使用 Exporting 參數 int_number_assignment = 'X'

[Code] Lock Mechanism


Lock:
CALL FUNCTION 'ENQUEUE_E_TRDIR'
  EXPORTING
    name           = 'PROGRAM_RUNNING'
    _scope         = '1'
  EXCEPTIONS
    foreign_lock   = 1
    system_failure = 2
    OTHERS         = 3.
IF sy-subrc <> 0.
  CALL FUNCTION 'DEQUEUE_ALL'.
  MESSAGE e000(zsd01) WITH 'Err: Program running!'.
ENDIF.
Unlock:
CALL FUNCTION 'DEQUEUE_ALL'.

Reference Manual:

2011年6月10日 星期五

[Code] YourIMG

Download Your IMG to Excel

IMG 的設定主要是存放在 Table, Maintenance View, Cluster View 裡,YourIMG 只針對主要的這三種格式下載。在 4.7 版中還有部份的 IMG 是使用獨立撰寫的程式去作設定的,這一類的資料就無法一併下載了。

2011年5月28日 星期六

[ABAP] Use OLE to Manipulate EXCEL

透過 SAP GUI 的功能直接操作 EXCEL

首先定義 Object :
TYPE-POOLS: ole2.
* OLE objects Declarations DATA: w_excel TYPE ole2_object, w_workbooks TYPE ole2_object, w_workbook TYPE ole2_object, w_worksheets TYPE ole2_object, w_worksheet TYPE ole2_object, w_columns TYPE ole2_object, w_column_ent TYPE ole2_object, w_cell TYPE ole2_object, w_int TYPE ole2_object, w_range TYPE ole2_object, w_font TYPE ole2_object, w_window TYPE ole2_object.
接著開啟 Excel :

2011年5月25日 星期三

When you use ABAP to split a BIG5 string , how to make sure that you have no cut a word of BIG5 in half ?

當你用 ABAP 在分割或刪減包含有 BIG5 的字串時,如何確認你沒有把一個 BIG5 的字切成一半?

首先我們要知道 BIG5 的編號規則,BIG5 是 2 bytes code ,分為 High Byte 和 Low Byte:
*      ===========   ===========
*       High Byte     Low Byte
*      ===========   ===========
*       0xA1-0xF9     0x40-0x7E
*                        or
*                     0xA1-0xFE
*      ===========   ===========
依照其編碼規則我們可以發現 high byte 和 low byte 有重疊的部份就是 0xA1-0xF9 的區間,底下的程式假設將一個長字串縮減成 30 個字元,然後判斷最後一個字元是否有將 BIG5 的文字切成一半:

2011年5月11日 星期三

[Func] Func Group - TXXL


EXCEL - list viewer interface

Function Module:
  • XXL_SIMPLE_API: 單個情況 Excel 清單查看器的省略接口
  • XXL_FULL_API: Excel 清單瀏覽器的完整界面

XXL_SIMPLE_API

Example code:
  1. XXLSTEST
  2. DATA_Export
*&---------------------------------------------------------------------*
*&      Form  data_export
*&---------------------------------------------------------------------*
*       資料匯出至 Excel
*----------------------------------------------------------------------*
FORM DATA_EXPORT .

DATA: XMPL_HEADER LIKE  GXXLT_P-TEXT
                  VALUE '交貨明細表'.

DATA BEGIN OF XMPLT_O OCCURS 1.
        INCLUDE STRUCTURE GXXLT_O.
DATA END OF XMPLT_O.

DATA BEGIN OF XMPLT_P OCCURS 1.
        INCLUDE STRUCTURE GXXLT_P.
DATA END OF XMPLT_P.

DATA BEGIN OF XMPLT_V OCCURS 1.
        INCLUDE STRUCTURE GXXLT_V.
DATA END OF XMPLT_V.

DATA: BEGIN OF XMPLT_D OCCURS 60,
        VBELN    LIKE LIPS-VBELN,
        POSNR    LIKE LIPS-POSNR,
        KUNNR    LIKE LIKP-KUNNR,
        NAME1    LIKE KNA1-NAME1,
        NAME1_SH LIKE KNA1-NAME1,
        MATNR    LIKE LIPS-MATNR,
        VGBEL    LIKE LIPS-VGBEL,
        KDMAT    LIKE LIPS-KDMAT,
        LFIMG    LIKE LIPS-LFIMG,
        VRKME    LIKE LIPS-VRKME,
        LFDAT    LIKE LIKP-LFDAT,
      END OF XMPLT_D.

* 標題列
CLEAR XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '交貨'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '項目'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '收貨人'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '客戶名稱'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '出貨對象名稱'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '物料'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '採購文件'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '客戶物料號碼'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '交貨數量'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = 'SU'.
APPEND XMPLT_V.
XMPLT_V-COL_NO     = XMPLT_V-COL_NO + 1.
XMPLT_V-COL_NAME   = '發貨日期'.
APPEND XMPLT_V.

* 資料轉移
LOOP AT GT_ITAB.
  MOVE-CORRESPONDING GT_ITAB TO XMPLT_D.
  APPEND XMPLT_D.
ENDLOOP.
SORT XMPLT_D BY VBELN POSNR.
*
CALL FUNCTION 'XXL_SIMPLE_API'
   EXPORTING
        N_KEY_COLS         = 4
        FILENAME           = 'XMPL0002'
        HEADER             = XMPL_HEADER
   TABLES
        DATA               = XMPLT_D
        ONLINE_TEXT        = XMPLT_O
        PRINT_TEXT         = XMPLT_P
        COL_TEXT           = XMPLT_V.

ENDFORM.                    " data_export

XXL_FULL_API

Example code: XXLFTEST
CALL FUNCTION 'XXL_FULL_API'
   EXPORTING
        N_VRT_KEYS         = 3     " Of the 9 columns in the list
        N_ATT_COLS         = 6     " \ object, 3 are key columns
        N_HRZ_KEYS         = 3     " Header above the non-key-columns
                                   " \ forms a three-level-hierarchy
        DATA_STARTING_AT   = 5     " Only 50 rows out of 60 are
        DATA_ENDING_AT     = 54    " \ really exported
        FILENAME           = 'XMPL0001'
        HEADER_1           = XMPL_HEADER1
        HEADER_2           = XMPL_HEADER2
        SEMA_TYPE          = 'X'    " C-Columns are converted if SEMA
                                    " \ definitions require this
        NO_DIALOG          = SPACE  " XXL itself prompts for FILENAME
   TABLES
        VKEY               = XMPLT_V
        HKEY               = XMPLT_H
        DATA               = XMPLT_D
        SEMA               = XMPLT_S
        ONLINE_TEXT        = XMPLT_O
        PRINT_TEXT         = XMPLT_P
   EXCEPTIONS
        FILE_OPEN_ERROR    = 71
        FILE_WRITE_ERROR   = 72
        INV_WINSYS         = 73
        INV_XXL            = 74
        CANCELLED_BY_USER  = 75
        OTHERS             = 99.

[Func] PUT SIGN IN FRONT


Function: CLOI_PUT_SIGN_IN_FRONT ,將負號放到數字前面
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
  CHANGING
    VALUE         = var_char
注意 :如果原先負號的後面還有其他的字元(例如 %),經過處理後會遺漏掉。

[Code] Put Data into Range Object with Macro


Put data into ranges table.
首先是先定義好資料:
TABLES: tvfk.
RANGES: gr_fkart FOR vbrk-fkart.
最重要的 萬用巨集
* -------------
* Define Macro
* -------------
DEFINE put_ranges.
  &1-sign   = &2.
  &1-option = &3.
  &1-low    = &4.
  &1-high   = &5.
  APPEND &1.
END-OF-DEFINITION.
或者是這個「假猴」的 :p
DEFINE put_ranges.
  FIELD-SYMBOLS <PT> LIKE &1.
  ASSIGN &1 TO <PT>.
  <PT>-sign   = &2.
  <PT>-option = &3.
  <PT>-low    = &4.
  <PT>-high   = &5.
  APPEND <PT> TO &1.
END-OF-DEFINITION.
然後是運用的方式:
SELECT fkarts INTO tvfk-fkarts
    FROM tvfk
   WHERE kappl = 'V3'.
  put_ranges gr_fkart 'E' 'EQ' tvfk-fkarts ''.
ENDSELECT.

2011年4月8日 星期五

Change document

customer master object: (Block customer):
TABLE       CDHDR/CDPOS
OBJECTCLAS  = DEBI
OBJECTID    = <Customer number>
TCODE       = VD05

OBJECTCLAS

可以由底下 Table 取得:
SWECDOBTYP  指派更改文件 / 工作流程物件類型
TCDOB       變更文件建立的物件
FUNCTION C14O_CH_DOC_OBJ_NAME_GET:
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"       IMPORTING
*"             VALUE(I_TABLENAME) TYPE  ESP1_TAB_NAME
*"       EXPORTING
*"             VALUE(E_OBJ_NAME) LIKE  TCDOB-OBJECT
*"       EXCEPTIONS
*"              ILLEGAL_INPUT<
*"              CHNG_DOC_OBJ_NOT_FOUND
*"----------------------------------------------------------------------
CDNAMES_GET
Transport Chnge doc.obj. via shadow tab.

訂單文件修改查詢

範例程式:RVSCD100:
CALL FUNCTION 'CHANGEDOCUMENT_READ_HEADERS'
  EXPORTING
    DATE_OF_CHANGE    = CDHDR-UDATE
    OBJECTCLASS       = CDHDR-OBJECTCLAS
    OBJECTID          = CDHDR-OBJECTID
    TIME_OF_CHANGE    = CDHDR-UTIME
    USERNAME          = CDHDR-USERNAME
  TABLES
    I_CDHDR           = ICDHDR
  EXCEPTIONS
    NO_POSITION_FOUND = 1
    OTHERS            = 2.

CALL FUNCTION 'CHANGEDOCUMENT_READ_POSITIONS'
  EXPORTING
    CHANGENUMBER      = ICDHDR-CHANGENR
    TABLEKEY          = CDPOS-TABKEY
    TABLENAME         = CDPOS-TABNAME
  IMPORTING
    HEADER            = CDHDR
  TABLES
    EDITPOS           = ICDSHW
  EXCEPTIONS
    NO_POSITION_FOUND = 1
    OTHERS            = 2.

[Func] SD_CONDITION_TIME_INTERVAL_GET

依照篩選條件取回日期區間內生效的價格條件
FUNCTION SD_CONDITION_TIME_INTERVAL_GET :
CALL FUNCTION 'SD_CONDITION_TIME_INTERVAL_GET'
    EXPORTING
      VALUE(KOMG_I)             LIKE  KOMG STRUCTURE  KOMG OPTIONAL
      VALUE(KOMK_I)             LIKE  KOMK STRUCTURE  KOMK OPTIONAL
      VALUE(KOMP_I)             LIKE  KOMP STRUCTURE  KOMP OPTIONAL
      VALUE(DATVO_I)            LIKE  A000-DATAB
      VALUE(DATBI_I)            LIKE  A000-DATBI
      VALUE(KVEWE_I)            LIKE  T681V-KVEWE DEFAULT 'A'
      VALUE(KAPPL_I)            LIKE  T681A-KAPPL DEFAULT 'V '
      VALUE(KOMK_KOMP_FIELDS_I) LIKE  SY-MARKY DEFAULT ' '
    TABLES
      PERIODS_TAB               STRUCTURE  WPPERIOD
      KSCHL_TAB                 STRUCTURE  WPKSCHL
    EXCEPTIONS
      NO_CONDITIONS.
Example:
KOMG-VKORG = P_VKORG.
      KOMG-VTWEG = P_VTWEG.
      KOMG-MATNR = IMATNR-MATNR.
      KOMG-KUNNR = IKUNNR-KUNNR.
      CALL FUNCTION 'SD_CONDITION_TIME_INTERVAL_GET'
        EXPORTING
          KOMG_I      = KOMG
          DATVO_I     = S_DATAB-LOW
          DATBI_I     = S_DATAB-HIGH
        TABLES
          PERIODS_TAB = GT_PREDATE
          KSCHL_TAB   = GT_TKSCH
        EXCEPTIONS
          OTHERS      = 1.
      READ TABLE GT_PREDATE INDEX 1.
      CHECK SY-SUBRC = 0.
      LOOP AT GT_PREDATE.
        GT_ITAB-MATNR   = IMATNR-MATNR.
        GT_ITAB-KUNNR   = IKUNNR-KUNNR.
        GT_ITAB-NAME1   = IKUNNR-NAME1.
        GT_ITAB-KOTABNR = GT_PREDATE-KOTABNR.
        GT_ITAB-VTWEG   = P_VTWEG.
        GT_ITAB-KSCHL   = P_KSCHL.
        GT_ITAB-DATAB   = GT_PREDATE-DATAB.
        GT_ITAB-DATBI   = GT_PREDATE-DATBI.
        SELECT SINGLE H~ERNAM H~ERDAT P~KBETR
            INTO (GT_ITAB-ERNAM, GT_ITAB-ERDAT,GT_ITAB-KBETR)
            FROM KONH AS H
            JOIN KONP AS P
              ON P~KNUMH = H~KNUMH
           WHERE H~KNUMH = GT_PREDATE-KNUMH.
        APPEND GT_ITAB.
        LT_DD02T-KOTABNR = GT_PREDATE-KOTABNR.
        APPEND LT_DD02T.
      ENDLOOP.