Showing posts with label ABAP. Show all posts
Showing posts with label ABAP. Show all posts

Sunday, June 20, 2010

How to insert text element in SAP Smartforms

You can insert text element in SAP smartforms as following:
  • Select window that you want to insert graphic object and right click. System will list object that you can create.
    In the example, I have window name ’HEADER2’ and I want to insert text element in this window.
  • On popup menu, you will select Create -> Text

  • System will default value of text name and description but you can change them and select text type as ’Text Element’.
  • You can input text that you want to show by typing in editor on General Attributes tab.

    In the example, I want to show text ’Testing text element’ on window ’HEADER2’.
  • The output is:

การกำหนดให้ window ขึ้นเฉพาะในหน้าสุดท้ายใน smartforms

หากเราต้องการให้ windows นั้นออกเฉพาะหน้าสุท้ายของ document ให้เรากำหนดที่ Conditions -> And Additional Event -> เลือกที่ Only After End of Main Window
เพียงแค่นี้ Window นั้นก็ขึ้นเฉพาะหน้าสุดท้ายแล้ว

Monday, May 24, 2010

No Vendor Specified

บางครั้งเมื่อเราเรียกคำสั่ง MESSAGE ใน ABAP แล้วเกิด Message Pop up ขึ้นมาว่า "No Vendor Specified" สาเหตุเป็นเพราะว่าใน SAP System ที่เราใช้อยู่นั้นใน Message Class = '00' => Message Number = '000' ไม่ได้ที่การ Maintain ค่าไว้ ซึ่งมันควรจะเป็น &1&2&3&4&5&6&7&8 นั่นจึงเป็นสาเหตุว่าเมื่อเราเรียกคำสั่ง MESSAGE เช่น
MESSAGE 'No data for selection' TYPE 'S'

จึงเกิดข้อความดังกล่าวขึ้น

วิธีนี้ถ้าเราไม่ต้องการแก้ไข Message Class = '00' ก็ให้ใช้คำสั่ง MESSAGE โดยกำหนด Class ที่ต้องการใช้ไปเลยเช่น
MESSAGE ID 'PN' TYPE 'S' NUMBER 016 WITH 'No data for selection'.

Thursday, April 8, 2010

How to Upload and Download Source Code of ABAP/4

Upload Source Code from PC File to ABAP Editor
You can do it by select Utilities -> More utilities -> Upload/download -> Upload and then enter your pc file that want to upload in your ABAP editor. Finally, select action ’Copy’.

Download Source Code from ABAP Editor to PC File
You can do it by select Utilities -> More utilities -> Upload/download -> Download and then enter your pc file that want to download from your ABAP editor. Finally, select action ’Copy’.

How to Insert or Delete Comment Block in ABAP Editor

Insert Comment
Select the statement line as block by hi-light and select menu Utilities -> Block/buffer -> Insert comment * in table control mode) or use hot key Ctrl + < in textedit control mode.

Delete Comment
Select the statement line as block by hi-light and select menu Utilities -> Block/buffer -> Delete comment * in table control mode) or use hot key Ctrl + > in textedit control mode.

Wednesday, April 7, 2010

How to Assign Dynamic Fields using Filed-Symbols in ABAP/4

You can create field name in run time and assign the created name in your field-symbols by statement "ASSIGN ... TO ...".

Example

DATA: name(20),

lv_check(1).

DATA: BEGIN OF itab OCCURS 0,
fld1(4),
fld2(4),

END OF itab.

FIELD-SYMBOLS .

...

...

LOOP AT itab.

IF lv_check = ’X’.

name = ’ITAB-FLD1’.

ELSE.

name = ’ITAB-FLD2’.

ENDIF.

ASSIGN (name) TO .

...

...

ENDLOOP.

Color in ABAP/4

Name Code Color

COL_BACKGROUND 0 depends on GUI

COL_HEADING 1 grayish-blue
COL_NORMAL 2 bright gray
COL_TOTAL 3 yellow
COL_KEY 4 bluish green
COL_POSITIVE 5 green
COL_NEGATIVE 6 red
COL_GROUP 7 violet

FORMAT Options of Format Statement in ABAP/4

Option          Description
COLOR n Specific background colors
INTENSIFIED Intensified the background color
INVERSE Swap background and foreground color
HOTSPOT Effect when mouse pointer and click
INPUT Generate input field
RESET Reset formats. The format will be backed to default format.

System Field When Generate ABAP/4 Report (Often to use)

Field     Description
COLNO the current column
LINNO the current line
LINCT Number of list lines
LINSZ Line size of list
SROWS Number of lines on screen
SCOLS Number of column on screen
TITLE Program title
PAGNO Current page in list

How to Find the Number of Entries of Internal Table in ABAP/4

We can find out how many entries of internal table by apply statement "DESCRIBE".

Example

DATA: it_tab TYPE TABLE OF BKPF WITH HEADER LINE,
tot_lines TYPE i.
...
...
DESCRIBE TABLE it_tab LINES tot_lines.

Or you can apply statement ’LOOP AT’ and count it.

Example

LOOP AT it_tab.
tot_lines = tot_lines + 1.
ENDLOOP.

Operator for comparing strings in ABAP/4

Operator     Description
CO Contains Only
CN Contains Not Only
CA Contains Any
NA Contains Not Any
CS Contains String
NS Contains No String
CP Matches Pattern
NP Not Match Pattern

How to Read Table Dynamically in ABAP/4

About statement "READ TABLE", we can generate code for read key dynamic as below:

DATA: BEGIN OF itab OCCURS 0,
fld1(2),

fld2(2),

fld3(10),

fld4 TYPE i,

END OF itab,

wa_tab LIKE itab,

gv_key1(4) TYPE C VALUE ’FLD1’,

gv_key2(4) TYPE C VALUE ’FLD2’.

...

...

...

READ TABLE itab INTO wa_tab
WITH KEY (gv_key1) = ’AA’
(gv_key2) = ’BB’.

How to Generate Value Request by ABAP/4

We can use function module "F4IF_INT_TABLE_VALUE_REQUEST" for generate value request by ABAP report.

Example


*** Screen Flow ***

PROCESS ON VALUE-REQUEST.

FIELD scn_kokrs MODULE create_value_request.

*** ABAP Editor ***

MODULE create_value_request INPUT.

DATA: BEGIN OF itab_request OCCURS 0,

kokrs LIKE TKA01-KOKRS,

bezei LIKE TKA01-BEZEI,

END OF itab_request.


SELECT KOKRS BEZEI
FROM TKA01
INTO TABLE itab_request.

CALL FUNCTION ’F4IF_INT_TABLE_VALUE_REQUEST’
EXPORTING
retfield = ’KOKRS’
value_org = ’S’
TABLES
value_tab = itab_request
EXCEPTIONS
parameter_error = 1
no_values_found = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.


ENDIF.

ENDMODULE.

ABAP/4 Example Code: ALV List by FM 'REUSE_ALV_LIST_DISPLAY'

Example

TYPE-POOLS: SLIS.

DATA: it_spfli TYPE TABLE OF spfli WITH HEADER LINE,
it_cat TYPE SLIS_T_FIELDCAT_ALV,
wa_cat TYPE slis_fieldcat_alv.

START-OF-SELECTION.
SELECT * FROM spfli
INTO TABLE it_spfli.

PERFORM create_field_catalog.

CALL FUNCTION ’REUSE_ALV_LIST_DISPLAY’
EXPORTING
* I_INTERFACE_CHECK = ’ ’
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE = ’ ’
* I_CALLBACK_PROGRAM = ’ ’
* I_CALLBACK_PF_STATUS_SET = ’ ’
* I_CALLBACK_USER_COMMAND = ’ ’
* I_STRUCTURE_NAME =
* IS_LAYOUT =
IT_FIELDCAT = it_cat[]
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = ’X’
* I_SAVE = ’ ’
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* IR_SALV_LIST_ADAPTER =
* IT_EXCEPT_QINFO =
* I_SUPPRESS_EMPTY_DATA = ABAP_FALSE
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
T_OUTTAB = it_spfli
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*&---------------------------------------------------------------------*
*& Form create_field_catalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_field_catalog .
IF it_cat[] is initial.
clear wa_cat.
wa_cat-col_pos = 1.
wa_cat-fieldname = ’CARRID’.
wa_cat-datatype = ’CHAR’.
wa_cat-inttype = ’C’.
wa_cat-intlen = 3.
wa_cat-seltext_l = ’Airline Code’.
wa_cat-seltext_m = ’Airline Code’.
wa_cat-seltext_s = ’Airline Code’.
append wa_cat to it_cat.

clear wa_cat.
wa_cat-col_pos = 2.
wa_cat-fieldname = ’CONNID’.
wa_cat-datatype = ’NUMC’.
wa_cat-inttype = ’N’.
wa_cat-intlen = 4.
wa_cat-seltext_l = ’Flight Conn. No’.
wa_cat-seltext_m = ’Flight Conn. No’.
wa_cat-seltext_s = ’Flight Conn. No’.
append wa_cat to it_cat.

clear wa_cat.
wa_cat-col_pos = 3.
wa_cat-fieldname = ’CITYFROM’.
wa_cat-datatype = ’CHAR’.
wa_cat-inttype = ’C’.
wa_cat-intlen = 20.
wa_cat-seltext_l = ’Dep. city’.
wa_cat-seltext_m = ’Dep. city’.
wa_cat-seltext_s = ’Dep. city’.
append wa_cat to it_cat.

clear wa_cat.
wa_cat-col_pos = 4.
wa_cat-fieldname = ’AIRPFROM’.
wa_cat-datatype = ’CHAR’.
wa_cat-inttype = ’C’.
wa_cat-intlen = 3.
wa_cat-seltext_l = ’Dep. airport’.
wa_cat-seltext_m = ’Dep. airport’.
wa_cat-seltext_s = ’Dep. airport’.
append wa_cat to it_cat.

clear wa_cat.
wa_cat-col_pos = 5.
wa_cat-fieldname = ’CITYTO’.
wa_cat-datatype = ’CHAR’.
wa_cat-inttype = ’C’.
wa_cat-intlen = 20.
wa_cat-seltext_l = ’Arrival city’.
wa_cat-seltext_m = ’Arrival city’.
wa_cat-seltext_s = ’Arrival city’.
append wa_cat to it_cat.

clear wa_cat.
wa_cat-col_pos = 6.
wa_cat-fieldname = ’AIRPTO’.
wa_cat-datatype = ’CHAR’.
wa_cat-inttype = ’C’.
wa_cat-intlen = 3.
wa_cat-seltext_l = ’Dest. airport’.
wa_cat-seltext_m = ’Dest. airport’.
wa_cat-seltext_s = ’Dest. airport’.
append wa_cat to it_cat.

ENDIF.
ENDFORM. " create_field_catalog


The output is:

Sale and Distribution (SD) Table in SAP

Table Description

VBAK Sale Order Header
VBAP Sale Order Line Items
LIKP Delivery Order Header
LIPS Delivery Order Line Items
VEKP Shipping Unit Header
VEPO Shipping Unit Line Items
EIKP Foreign Trade: Header
EIPO Foreign Trade: Line Items
VBRK Billing Header
VBRP Billing Line Items
VBUK Sale Document Status Header
VBUP Sale Document Status Line Items
VBFA Document Flow
VBPA Sale Document Partner
KONV Pricing Condition

Tuesday, April 6, 2010

ดึงข้อมูลของ TCode: STAT โดยใช้ ABAP

Transaction STAT เป็น Transaction เพื่อใช้สำหรับวิเคราะห์สถิติการใช้งานโปรแกรมของผู้ใช้งาน โดยข้อมูลที่เก็บนั้นไม่ได้เก็บอยู่ในรูปของตาราง แต่เก็บอยู่ในรูปของไฟล์ ซึ่งถ้าเราต้องการดึงข้อมูลของ Transaction นี้ออกมา เราสามารถทำได้โดยการเลือกใช้ Function Module: SAPWL_STATREC_READ_FILE ดังตัวอย่าง

CALL FUNCTION 'SAPWL_STATREC_READ_FILE'
EXPORTING
READ_CLIENT = CLIENT
READ_END_DATE = ENDDATE
READ_END_TIME = ENDTIME
READ_START_DATE = STARTDATE
READ_START_TIME = STARTTIME
TABLES
V2_NORMAL_RECORDS = V2_NORMAL_RECORDS.

จากตัวอย่างจะเห็นว่าเราดึงข้อมูลใน STAT ไฟล์โดยกำหนดข้อมูลเกี่ยวกับ Client, End Date, End Time, Start Date และ Start Time เป็นเงื่อนไขในการเลือกข้อมูล และหลังจากเรียกฟังก์ชั่นนี้ทำงาน ข้อมูลจะถูกกลับมาที่ Internal Table: V2_NORMAL_RECORDS

Monday, April 5, 2010

วิธีการแก้ไข User Status ในใบสั่งผลิต โดยใช้ ABAP

ในบางครั้งระบบ PP ที่อยู่ใน SAP ได้มีการ Config User Status ขึ้นมาเพื่อเป็นข้อมูลเพิ่มเติมที่ User สามารถระบุได้ ดังงนั้นในบทความนี้เราจะใช้ Function Module "I_CHANGE_STATUS" เพื่อแก้ไขข้อมูลนี้ โดยฟังก์ชั่นนี้ต้องการเพียง Object Number ของใบสั่งผลิต, รหัสสถานะที่ Active อยู่ และรหัสสถานะที่ต้องการให้ Active
ตัวอย่างการใช้ Function มีดังนี้

CALL FUNCTION 'I_CHANGE_STATUS'
EXPORTING
OBJNR = PA_OBJNR "Object Number
ESTAT_INACTIVE = lv_inactive "สถานะที่ Active อยู่ ณ ปัจจุบัน ซึ่งหลังจากรันแล้วจะถูกเปลี่ยนเป็นไม่ Active
ESTAT_ACTIVE = lv_active "สถานะที่ต้องการเซ็ตให้ Active
EXCEPTIONS
CANNOT_UPDATE = 1
OTHERS = 2 .

เรียก BAPI_GOODSMVT_CREATE โดยใช้ PHP

ในบทความนี้เราจะแสดงตัวอย่างของการเรียก BAPI ที่ชื่อ BAPI_GOODSMVT_CREATE ผ่านทางภาษา PHP ก่อนอื่นขออธิบาย BAPI_GOODSMVT_CREATE ก่อน BAPI ตัวนี้ใช้สำหรับสร้าง Material Document จากระบบผ่านนอกผ่านทาง BAPI คล้ายๆกับการทำงานของ Transaction: MB1A, MB1B หรือ MB1C
และทุกครั้งหลังจากเรียนก BAPI ที่กล่าวข้างต้นทำงานแล้ว ไม่พบข้อความผิดพลาด เราจะต้องเรียก BAPI_TRANSACTION_COMMIT เพื่อบันทึกข้อมูลเข้าสู่ระบบจริง แต่หากผลข้อผิดพลาด เราก็สามารถเรียก BAPI_TRANSACTION_ROLLBACK เพื่อดึงข้อมูลย้อนกลับ มาดูตัวอย่างกันดีกว่า

// This function receive refdoc and array of goods movement details
// and return data of material doc, material year and return message
function callissuesap($refdoc, $gmm_detail){

$return = array();

// Create saprfc-instance
$sap = create_connection();

// Import Parameters
$today = date("Ymd");

$header = array();
$header["PSTNG_DATE"] = $today;
$header["DOC_DATE"] = $today;
$header["REF_DOC_NO"] = $refdoc;
$header["HEADER_TXT"] = "";

$code = array();
$code["GM_CODE"] = "03";

$item = array();
$lineitem = array();
foreach ($gmm_detail as $gmm_line) {
$lineitem["ORDERID"] = $gmm_line["orderId"];
$lineitem["MATERIAL"] = $gmm_line["material"];
$lineitem["PLANT"] = $gmm_line["plant"];
$lineitem["STGE_LOC"] = $gmm_line["loc"];
$lineitem["BATCH"] = $gmm_line["batch"];
$lineitem["MOVE_TYPE"] = "261";
$lineitem["ENTRY_QNT"] = $gmm_line["qty"];
$lineitem["ENTRY_UOM"] = $gmm_line["une"];
$item[] = $lineitem;
}

// Call-Function
$result=$sap->callFunction("BAPI_GOODSMVT_CREATE",
array(
array("IMPORT","GOODSMVT_HEADER",$header),
array("IMPORT","GOODSMVT_CODE",$code),
array("EXPORT","MATERIALDOCUMENT",""),
array("EXPORT","MATDOCUMENTYEAR",""),
array("TABLE","GOODSMVT_ITEM",$item),
array("TABLE","RETURN",array())
));

// Call successfull?
if ($sap->getStatus() == SAPRFC_OK) {
$sap->callFunction("BAPI_TRANSACTION_COMMIT",array());
$return["MAT_DOC"] = $result["MATERIALDOCUMENT"];
$return["MAT_DOC_YEAR"] = $result["MATDOCUMENTYEAR"];
$return["RETURN"] = $result["RETURN"];
}
else {
$mess = array();
$messline = array();
$messline["TYPE"] = "E";
$messline["MESSAGE"] = $sap->getStatusTextLong();
$mess[] = $messline;

// $sap->printStatus();
$sap->callFunction("BAPI_TRANSACTION_ROLLBACK",array());
$return["MAT_DOC"] = "";
$return["MAT_DOC_YEAR"] = "";
$return["RETURN"] = $mess;
}
$sap->logoff();

return $return;
}

function create_connection() {
return $sap = new saprfc(array(
"logindata"=>array(
"ASHOST"=>"xxx.xxx.xxx.xxx" // application server
,"SYSNR"=>"xx" // system number
,"CLIENT"=>"xxx" // client
,"USER"=>"xxxxxxxxx" // user
,"PASSWD"=>"xxxxxxxxx" // password
)
,"show_errors"=>false // let class printout errors
,"debug"=>false)) ; // detailed debugging information
}


Information about GM_CODE
GM_Code 01: Goods receipt for purchase order
GM_Code 02: Goods receipt for production order
GM_Code 03: Goods issue
GM_Code 04: Transfer posting
GM_Code 05: Other goods receipts
GM_Code 06: Reversal of goods movements

คุณสามารถอ่านข้อมูลเพิ่มเติมได้ โดยไปที่ TCode: BAPI -> Materials Management -> Inventory Management, Goods Movement, CreateFromData -> Tab Documentation

How to apply BAPI_GOODSMVT_CREATE by PHP

If we want to create material document in SAP R/3 from external system, we can apply BAPI function BAPI_GOODSMVT_CREATE for this action. After we already call this function, we shoud call function BAPI_TRANSACTION_COMMIT or BAPI_TRANSACTION_ROLLBACK to commit or rollback our processes. In the example , i will create issue material doc by apply this function using PHP.

Example

// This function receive refdoc and array of goods movement details
// and return data of material doc, material year and return message
function callissuesap($refdoc, $gmm_detail){

$return = array();

// Create saprfc-instance
$sap = create_connection();

// Import Parameters
$today = date("Ymd");

$header = array();
$header["PSTNG_DATE"] = $today;
$header["DOC_DATE"] = $today;
$header["REF_DOC_NO"] = $refdoc;
$header["HEADER_TXT"] = "";

$code = array();
$code["GM_CODE"] = "03";

$item = array();
$lineitem = array();
foreach ($gmm_detail as $gmm_line) {
$lineitem["ORDERID"] = $gmm_line["orderId"];
$lineitem["MATERIAL"] = $gmm_line["material"];
$lineitem["PLANT"] = $gmm_line["plant"];
$lineitem["STGE_LOC"] = $gmm_line["loc"];
$lineitem["BATCH"] = $gmm_line["batch"];
$lineitem["MOVE_TYPE"] = "261";
$lineitem["ENTRY_QNT"] = $gmm_line["qty"];
$lineitem["ENTRY_UOM"] = $gmm_line["une"];
$item[] = $lineitem;
}

// Call-Function
$result=$sap->callFunction("BAPI_GOODSMVT_CREATE",
array(
array("IMPORT","GOODSMVT_HEADER",$header),
array("IMPORT","GOODSMVT_CODE",$code),
array("EXPORT","MATERIALDOCUMENT",""),
array("EXPORT","MATDOCUMENTYEAR",""),
array("TABLE","GOODSMVT_ITEM",$item),
array("TABLE","RETURN",array())
));

// Call successfull?
if ($sap->getStatus() == SAPRFC_OK) {
$sap->callFunction("BAPI_TRANSACTION_COMMIT",array());
$return["MAT_DOC"] = $result["MATERIALDOCUMENT"];
$return["MAT_DOC_YEAR"] = $result["MATDOCUMENTYEAR"];
$return["RETURN"] = $result["RETURN"];
}
else {
$mess = array();
$messline = array();
$messline["TYPE"] = "E";
$messline["MESSAGE"] = $sap->getStatusTextLong();
$mess[] = $messline;

// $sap->printStatus();
$sap->callFunction("BAPI_TRANSACTION_ROLLBACK",array());
$return["MAT_DOC"] = "";
$return["MAT_DOC_YEAR"] = "";
$return["RETURN"] = $mess;
}
$sap->logoff();

return $return;
}

function create_connection() {
return $sap = new saprfc(array(
"logindata"=>array(
"ASHOST"=>"xxx.xxx.xxx.xxx" // application server
,"SYSNR"=>"xx" // system number
,"CLIENT"=>"xxx" // client
,"USER"=>"xxxxxxxxx" // user
,"PASSWD"=>"xxxxxxxxx" // password
)
,"show_errors"=>false // let class printout errors
,"debug"=>false)) ; // detailed debugging information
}


Information about GM_CODE

GM_Code 01: Goods receipt for purchase order
GM_Code 02: Goods receipt for production order
GM_Code 03: Goods issue
GM_Code 04: Transfer posting
GM_Code 05: Other goods receipts
GM_Code 06: Reversal of goods movements

You can read the further informations in tcode -> Materials Management -> Inventory Management -> GoodsMovement -> CreateFromData -> Tab Documentation.

How to retrieve data from transaction STAT

The STAT transaction is transaction for analyze the statistics by users or programs. These data is kept in the server as file. If we want to retrieve these data, we should apply function 'SAPWL_STATREC_READ_FILE'. this function will return the raw data. you can take these data to analyze or verify depend on your requirement.

Example

CALL FUNCTION 'SAPWL_STATREC_READ_FILE'
EXPORTING
READ_CLIENT = CLIENT
READ_END_DATE = ENDDATE
READ_END_TIME = ENDTIME
READ_START_DATE = STARTDATE
READ_START_TIME = STARTTIME
TABLES
V2_NORMAL_RECORDS = V2_NORMAL_RECORDS.


In above example, i want to retreive stat data by client, enddate, endtime, startdate and starttime. After i call this function, the retreived data will be returned in internal table V2_NORMAL_RECORDS.