SAP Coding Guidelines
SAP Coding Guidelines
Copyright Notice
This document contains proprietary information of HCL Technologies Ltd. No part of this document may be reproduced, stored, copied, or transmitted in any form or by means of electronic, mechanical, photocopying or otherwise, without the express consent of HCL Technologies. This document is intended for internal circulation only and not meant for external distribution.
PLCGN010
1.0
02nd Dec 05
Taruna Bhatnagar
HCLT Confidential
Page 2 of 21
PLCGN010
Table of Contents
1 Introduction....................................................................................................................................... 4 1.1 1.2 2 Purpose .................................................................................................................................... 4 Scope ....................................................................................................................................... 4
SAP Development Standards .......................................................................................................... 4 2.1 Multi Country Approach............................................................................................................ 4 2.1.1 2.2 2.3 Country Identifier ........................................................................................................ 4
Online Program ........................................................................................................................ 6 Dialogue Module ...................................................................................................................... 6 Transaction Code ..................................................................................................................... 7 Function Groups....................................................................................................................... 7 Includes .................................................................................................................................... 8 SET/GET parameter ID ............................................................................................................ 8 AREA MENU....................................................................................................................... 8 Message Class/ Number..................................................................................................... 9 Dictionary Objects ............................................................................................................... 9 2.12.1 Domain....................................................................................................................... 9
DATA ELEMENT................................................................................................................. 9 STRUTURES .................................................................................................................... 10 TABLE TYPE .................................................................................................................... 10 VIEW ................................................................................................................................. 11 TABLE ............................................................................................................................... 11 2.17.1 2.17.2 2.17.3 BUFFERING ............................................................................................................ 11 INDEXES ................................................................................................................. 12 LOCK OBJECT ........................................................................................................ 12
2.18 2.19
SEARCH Help................................................................................................................... 13 ABAP Editor ...................................................................................................................... 13 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 Documenting with ABAP Code ................................................................................ 13 Program Documentation .......................................................................................... 13 Extended Program Check........................................................................................ 13 Modification to Code ................................................................................................ 14 Multi Language and Currency.................................................................................. 14 ABAP List Viewer..................................................................................................... 14
HCLT Confidential
Page 3 of 21
PLCGN010
2.20 2.21
Function Builder ................................................................................................................ 14 Menu Planer...................................................................................................................... 15 2.21.1 2.21.2 GUI STATUS (MENU + FUNCTION KEYS) .................................................................... 15 GUI Title................................................................................................................... 15
2.22
SCREEN Painter............................................................................................................... 15 2.22.1 2.22.2 2.22.3 2.22.4 LAYOUT ..................................................................................................................... 15 ELEMENT List ......................................................................................................... 15 SCREEN Attributes.................................................................................................. 15 FLOW Logic ............................................................................................................. 16
1
1.1 Purpose
Introduction
This document describes the policy and procedures for creating SAP R/3 objects. All SAP R/3 developers will use these guidelines.
1.2 Scope
This procedure applies to all personnel developing SAP R/3 objects as part of the SAP development process within Compass Group Plc .The policies and procedures in this document define our requirements for the development of all R/3 objects and also incorporate SAP recommendations where applicable. Compliance will ensure the support and development is kept separate for customer and SAP R/3 delivered applications
HCLT Confidential
Page 4 of 21
PLCGN010
Code GB IE NL BE NO
Examples: ZBCREPO001 - Country Independent with standard layout ZBCREPO001_NL-Dutch specific due to differing report layout for legal requirements Other attribute settings / actions to note: None
2.2
MODULE IDENTIFICATION
Module BC FI TR CO EC LO SD MM IM LE QM PM CS PP Description Basis Components Financial Accounting Treasury Controlling Enterprise Controlling Logistics - General Sales and Distribution Materials Management Investment Management Logistics Execution Quality Management Plant Maintenance Customer Service Production Planning and Control Description Terms and Conditions Miscellaneous Module PS EH PA PT PY PE SV AP BW EO MS IS Description Project System Environment, Health & Safety Personnel Management Personnel Time Management Payroll Accounting Training and Event Management Service Advanced Planner and Optimizer - APO SAP Business Information Warehouse SAP Electronic Commerce Mobile Sales Industry-Specific Components
Bespoke TC YY
HCLT Confidential
Page 5 of 21
PLCGN010
Project based developments must be assigned to their own class, created centrally by the department manager. All other objects should be created as temporary local objects, these will be deleted from the system as part of the quarterly release mechanism, each developer can create permanent local objects by assigning them to class $DV2. Other attribute settings / actions to note: None
Identifier REPO PROG INTF PRIN DATA EXTR OTHE Examples: ZBCREPO001 ZMMINTF001 -
Other attribute settings / actions to note: Enter the title in Proper case, remember this will appear in the title-bar of the SAP session Type 1 should be used Application field is not used - default to * To enable strict version control please set the Editor Lock checkbox Assign to correct development class Replace default header with Other Pattern (Ctrl +E) REPORT and edit Enable report level authorization Z_ABAP_REP
2.5
Dialogue Module
HCLT Confidential
Page 6 of 21
PLCGN010
All dialog ABAP coding should take the following naming convention.
SAPMZ<wxyz> Example:
where
<wxyz> = 4 character ID
Z<wxyz> Example:
where
<wxyz> = 4 character ID
ZDEVS-Developers Scripts Other attribute settings / actions to note: When cloning standard SAP transaction <wxyz> should match the clones transaction The new transaction code must be added to each valid users profile A transaction must be added to an existing or new menu
Z<nnn> Example:
where
HCLT Confidential
Page 7 of 21
PLCGN010
2.8 Includes
Where common code can be re-used within a series of developments you should create includes.
Z<mainabap>_INC Example:
where
ZBCHEADINGS_INC -Include for Compass standard page headings Other attribute settings / actions to note: As per Online Program but setting Type to I
Z<description> Example:
where
Z_<description> Example:
Z_APL-APL Menu
where
Other attribute settings / actions to note If copying from standard keep the <description> as original and enter the short text as Copy of <description>
HCLT Confidential
Page 8 of 21
PLCGN010
<nnn> Example:
where
001(ZC1)-No selection criteria entered Other attribute settings / actions to note Try to find an existing message in SAP classes using SE16 to browse the text fields on table T100 For specific messages avoid creating messages, instead use syntax MESSSGE ?000(ZC1) WITH & & & & For generic messages which could be used by other development always create a message in class ZC1
Use the next free number to find next message number ALWAYS set Self-explanatory checkbox or provide text if the error is not self explanatory!
Z<description> Example:
where
PLCGN010
Ensure both the <description> and attributes short text aid developers using search help Use a generic SAP supplied domain name where possible Maintain Field labels where required and remember to assess any need for translation Assess the need for Parameter ID or change documentation
2.14 STRUTURES
Structures can be used as the dictionary equivalent of ABAP field strings and in large projects or suites of programs you can create these definitions in the Dictionary and reference them using the INCLUDE STRUCTURE statement. Create using the following convention.
Z<mm><description> Example:
where
ZMMAPLHEAD-APL header data for IUTS Other attribute settings / actions to note: Ensure both the <description> and maintenance short text aid developers using search help Field names should all start with Z Use the default by reference entry, revert to direct type entry only in exceptions
Z<mm><description> Example:
None available- This functionality is new to release 4.x Other attribute settings / actions to note: Ensure both the <description> and maintenance short text aid developers using search help Reference to a an existing dictionary table or structure Refer to internal tables section to determine access and key settings
HCLT Confidential
Page 10 of 21
PLCGN010
2.16 VIEW
Four types of view are available and can involve one or more tables. Database views are the dictionary equivalent of an inner join SELECT, projection views the equivalent of SELECT on a single table using a field list, help views support outer joins and the maintenance view the UPDATE of tables linked by key relationships. From Release 4.x you may buffer views as well as tables, the use of this feature is detailed later in this document.
2.17 TABLE
Before creating a database table you should ensure there is no redundant data being stored. E.g. Texts for non unique entries should be held in separate check tables.
Z<mm><description> Example:
where
2.17.1 BUFFERING
HCLT Confidential
Page 11 of 21
PLCGN010
Buffering is not suitable for views or tables in which the accessed data changes frequently i.e. more than once a day. The type of buffering will also depend on the size of the data table and the volume and type of accesses.
Buffering
Full Generic Single None
What is Buffered
Entire table when one record read Records matching key of record read Single record read Nothing
When to Apply
Table <30KB, many reads, few writes Access by Key<32 bytes long Large Table, many reads of few records Tables with many writes
Example:
ZMMUNIGRP - Unit Group (Fully buffered) Other attribute settings / actions to note: None
2.17.2 INDEXES
An index should support the selection of data from a table, as an index must be updated as entries are inserted to its main table they should be used carefully. Analyse how records are accessed and try to re-code the SELECT to use an existing indexes or related tables. Fields should be in order of selectivity such that the main data set is reduced in size as quickly as possible An index in only of use up to the first unspecified field so define as few fields as possible An index will be used for a selection based on the Oracle optimiser (CBO) so do not create similar indexes Z<nn> where <nn> = sequential identifier
HCLT Confidential
Page 12 of 21
PLCGN010
where
<table> = Table
Z_<description> where <description> = short text Z_EARBADFILE Inbound EAR files have been rejected
Other attribute settings / actions to note: Ensure both the <description> and maintenance short text aid developers using search help
2.19.1
The requirements and high level design behind any development should be found within the development script, low level design and line level documentation must be included within the ABAP code itself. As part of the development QA signoff all comments will be reviewed and rejected if insufficient. Using the PATTERN pushbutton you will choose either the REPORT or HEADER template as the basis of developing any new bespoke source code (excluding functions). Any new section of code should be commented and any assumptions noted to ensure the QA sign off is achieved and assist ongoing support of the development.
2.19.2
Program Documentation
Within the reporting dialog a user will have access to the program documentation saved within the editor. The analyst should complete the wording for this section, supplying a short title and purpose being the development
2.19.3
HCLT Confidential
Page 13 of 21
PLCGN010
Before release of any development an extended program check will be performed by the department manager, if this check fails the development will be rejected and passed back to the developer for action. Therefore please run these checks yourself prior to release and correct any errors or warnings. The revised programming standards outlined below should prevent problems in new developments and help correct existing code developed prior to these standards.
2.19.4
Modification to Code
Any modification made to any existing code, bespoke or standard must be commented in the source header and around the lines modified. Any standard code changes will invoke the SAP Modification Assistant, bespoke change should mirror this format as outlined below. If the attributes do not have the Editor Lock set, please do so as part of the modification. In the source header insert a comment line under the modification section with the following details; SAP user ID, Date, Four digit DV2K9nnnn Transport Request number, PRJ CRF H/D reference and short description. Insert a *{ comment indicating if lines are being INSERTed, REMOVEd or REPLACEd Existing code should be REMOVEd by inserting * on relevant lines Add INSERTed or REPLACEd code and end modification with *} comment line matching first
2.19.5
Historically all developments within Compass Group have been UK specific resulting in hard coding of English text and no flexibility in currency reporting. In future the following must apply in all developments; All texts must be held as symbols and output as such using TEXT-nnn syntax All currency amounts must be suffixed with CURR curr, determined via function module Z_GET_ORG_DATA
2.19.6
A suite of new function modules have been delivered with Release 4.x which allows tables of data to be presented tot he user with a report-writer look and feel (*ALV*). The function module REUSE_ALV_LIST_DISPLAY should be used where appropriate using the following import / export parameters.
Parameter
i_callback_program it_fieldcat It_sort t_outtab
Description
Program control is passed to when list is left ( SY-REPID ) Fields for output ( SLIS_FIELDCAT_ALV ) Fields for sort ( SLIS_SORTINFO_ALV ) Internal table in order of it_fieldcat
PLCGN010
Z<mm>_<description>
Other attribute settings / actions to note: Always provide both interface and module documentation Ensure that you assign the function to the most suitable function group Coding within the function module must adhere to ABAP Editor standards
2.21.1
Program specific toolbar / menu / function key definition assigned statically to a screen in PROCESS BEFORE OUTPUT. All functions should be accessible via the menu and function keys or toolbar Try to maintain a single status for similar screen rather than duplicate entries for each screen Use the SET PF-STATUS XXX EXCLUDING <table> syntax to disable unwanted functions screen by screen
2.21.2
GUI Title
Program specific title up to 60 characters in length that can be either static or dynamic text. The & code can be used within the static text and dynamically modified using the SET TITLEBAR XXX WITH <text> syntax.
2.22.1 LAYOUT
Ensure the layout matched the scripted requirements but try to keep the flow of fields / input optimal from the users perspective. If you need to deviate from the script communicate the changes and reasons to the analyst who in turn will agree the change with the business. Use the Screen/Check/Layout functionality to identify where improvements can be made
HCLT Confidential
Page 15 of 21
PLCGN010
The short description, although not displayed at runtime, must contain a meaningful description as the search help and object browser use it. There are no other standards covering the attributes of the screen.
2.23 EFFICIENCY
Performance of both the system and coding is being continually reviewed and our current best practices are included in the Developers Quick Reference Guide (QRG). These include from the design of user interfaces, selection from the database, internal table management and loop processing. As part of the Compass Group approach we require all developments to pass through the runtime analysis transaction SE30, the QA process will reject any development that will benefit from further performance tuning.
3
REPORT BADCODE.
TABLES: MKPF, BKPF, MSEG, LFA1. DATA DATA DATA DATA V_BUPER LIKE T009B-POPER. V_GJAHR LIKE T009B-BDATJ. V_COUNT TYPE I. V_LAST(10).
PLCGN010
DATA
MBLNR LIKE MKPF-MBLNR, USNAM LIKE MKPF-USNAM, BKTXT LIKE MKPF-BKTXT, MATNR LIKE MSEG-MATNR, MENGE LIKE MSEG-MENGE, MEINS LIKE MSEG-MEINS, LIFNR LIKE LFA1-LIFNR, NAME1 LIKE LFA1-NAME1. END OF REPORT.
SELECT-OPTIONS PERIOD FOR BKPF-MONAT DEFAULT '9'. PARAMETER: COMPANY(4), YEAR(4) DEFAULT '1998'. SELECT * FROM MKPF WHERE MJAHR = YEAR. MOVE-CORRESPONDING MKPF TO REPORT. * Check Posting Date matches user selection CALL FUNCTION 'DATE_TO_PERIOD_CONVERT' EXPORTING I_DATE = MKPF-BUDAT I_PERIV = '96' IMPORTING E_BUPER = V_BUPER E_GJAHR = V_GJAHR EXCEPTIONS OTHERS = 1 CHECK V_GJAHR EQ YEAR. CHECK V_BUPER IN PERIOD. SELECT * FROM MSEG WHERE MBLNR = MKPF-MBLNR AND MJAHR = MKPF-MJAHR. MOVE-CORRESPONDING MSEG TO REPORT. SELECT SINGLE * FROM LFA1 WHERE LIFNR = MSEG-LIFNR. MOVE-CORRESPONDING LFA1 TO REPORT. APPEND REPORT. ENDSELECT. ENDSELECT. LOOP AT REPORT. IF REPORT-MBLNR NE V_LAST. WRITE: / REPORT-USNAM, REPORT-BKTXT. ENDIF. WRITE: / REPORT-MATNR, REPORT-MENGE, REPORT-MEINS, REPORT-LIFNR, REPORT-NAME1. V_LAST = REPORT-MBLNR. ENDLOOP. REPORT MODCODE. Add the line-count and line-size keywords. TABLES: MKPF, BKPF, MSEG, LFA1. Place each table on a new line and include description. DATA V_BUPER LIKE T009B-POPER.
Version No. : 1.0 Date: 2nd Dec 05 HCLT Confidential Page 17 of 21
PLCGN010
DATA V_GJAHR LIKE T009B-BDATJ. DATA V_COUNT TYPE I. DATA V_LAST(10). For clarity use : and , notation and add descriptions.
DATA: BEGIN OF REPORT OCCURS 0, MBLNR LIKE MKPF-MBLNR, USNAM LIKE MKPF-USNAM, BKTXT LIKE MKPF-BKTXT, MATNR LIKE MSEG-MATNR, MENGE LIKE MSEG-MENGE, MEINS LIKE MSEG-MEINS, LIFNR LIKE LFA1-LIFNR, NAME1 LIKE LFA1-NAME1. DATA END OF REPORT. The above internal table duplicates header and vendor data for each item! Split the data into three internal tables, joined using the document or vendor number. SELECT-OPTIONS PERIOD FOR BKPF-MONAT DEFAULT '9'. PARAMETER: COMPANY(4), YEAR(4) DEFAULT '1998'. Investigation shows that company is not used and should be removed to avoid confusion. The names should also be changed in line with Compass standards. You should avoid hard coding values into selection screen fields. SELECT * FROM MKPF WHERE MJAHR = YEAR. MOVE-CORRESPONDING MKPF TO REPORT. Convert the above to use a field list and populate using the INTO TABLE syntax. * Check Posting Date matches user selection CALL FUNCTION 'DATE_TO_PERIOD_CONVERT' EXPORTING I_DATE = MKPF-BUDAT I_PERIV = '96' IMPORTING E_BUPER = V_BUPER E_GJAHR = V_GJAHR EXCEPTIONS OTHERS = 1 CHECK V_GJAHR EQ YEAR. CHECK V_BUPER IN PERIOD. The logic at present checks the date for each item read from the database is within the selection screen range. However we can derive the date range once and select only matching items from the database.
SELECT * FROM MSEG WHERE MBLNR = MKPF-MBLNR AND MJAHR = MKPF-MJAHR. MOVE-CORRESPONDING MSEG TO REPORT. SELECT SINGLE * FROM LFA1 WHERE LIFNR = MSEG-LIFNR. MOVE-CORRESPONDING LFA1 TO REPORT.
Version No. : 1.0 Date: 2nd Dec 05 HCLT Confidential Page 18 of 21
PLCGN010
APPEND REPORT. ENDSELECT. Select above should be changed to use the FOR ALL ENTRIES syntax using the internal table populated from MKPF. If we add the INTO TABLW syntax we must move the selection from LFA1 into a LOOP..ENDLOOP structure, e.g. when we write out the report. ENDSELECT. No endselect will be required if INTO TABLE syntax is added. LOOP AT REPORT. IF REPORT-MBLNR NE V_LAST. WRITE: / REPORT-USNAM, REPORT-BKTXT. ENDIF. WRITE: / REPORT-MATNR, REPORT-MENGE, REPORT-MEINS, REPORT-LIFNR, REPORT-NAME1. V_LAST = REPORT-MBLNR. ENDLOOP. With a properly structured internal table control loop processing can be used to add sub and grand totals. We need to read the vendor details here and internally buffer them to avoid duplicate reads from the database.
REPORT GOODCODE LINE-COUNT 65 LINE-SIZE 132. * Include Standard Header TABLES: MKPF, MSEG, LFA1. section) " Header: Material Document " Document Segment: Material " Vendor master (general
PARAMETERS: P_YEAR LIKE T009B-BDATJ OBLIGATORY MEMORY ID GJA, P_PERIOD LIKE T009B-POPER OBLIGATORY. * Document Header DATA: BEGIN OF T_MKPF OCCURS 0, MBLNR LIKE MKPF-MBLNR, USNAM LIKE MKPF-USNAM, BKTXT LIKE MKPF-BKTXT, END OF T_MKPF. * Vendor Description DATA: BEGIN OF T_LFA1 OCCURS 0, LIFNR LIKE LFA1-LIFNR, NAME1 LIKE LFA1-NAME1, END OF T_LFA1. * Document Detail DATA: BEGIN OF T_MSEG OCCURS 0, MBLNR LIKE MSEG-MBLNR, MATNR LIKE MSEG-MATNR, LIFNR LIKE MSEG-LIFNR, MENGE LIKE MSEG-MENGE, MEINS LIKE MSEG-MEINS,
Version No. : 1.0 Date: 2nd Dec 05
HCLT Confidential
Page 19 of 21
PLCGN010
END OF T_MSEG. * Ranges RANGES R_DATE FOR SY-DATUM. " Converted Period/Year Date
* Get Low end of date range CALL FUNCTION 'FIRST_DAY_IN_PERIOD_GET' EXPORTING I_GJAHR = P_YEAR I_PERIV = '96' I_POPER = P_PERIOD IMPORTING E_DATE = R_DATE-LOW EXCEPTIONS OTHERS = 1. IF SY-SUBRC <> 0. MESSAGE E000(Z1) WITH 'Date Conversion Failed'. ENDIF.
* Get High end of date CALL FUNCTION 'LAST_DAY_IN_PERIOD_GET' EXPORTING I_GJAHR = P_YEAR I_PERIV = '96' I_POPER = P_PERIOD IMPORTING E_DATE = R_DATE-HIGH EXCEPTIONS OTHERS = 1. IF SY-SUBRC <> 0. MESSAGE E000(Z1) WITH 'Date Conversion Failed'. ENDIF. * Build Range for use in select R_DATE-SIGN = 'I'. R_DATE-OPTION = 'BT'. APPEND R_DATE. * Get Relevant Documents SELECT MBLNR USNAM BKTXT INTO TABLE T_MKPF FROM MKPF WHERE BUDAT IN R_DATE. * Sort by Key SORT T_MKPF BY MBLNR. * Ensure table is not empty before using FOR ALL ENTRIES IF T_MKPF[] IS INITIAL. MESSAGE E000(Z1) WITH 'No Date Exists for Parameters'. ENDIF. * Get Document Detail
Version No. : 1.0 Date: 2nd Dec 05 HCLT Confidential Page 20 of 21
PLCGN010
MBLNR MATNR LIFNR MENGE MEINS TABLE T_MSEG MSEG ALL ENTRIES IN T_MKPF MBLNR = T_MKPF-MBLNR MJAHR = P_YEAR.
* Output records in document order LOOP AT T_MKPF. SKIP 2. WRITE:/ T_MKPF-USNAM, T_MKPF-BKTXT. * Report all Items for Header LOOP AT T_MSEG WHERE MBLNR = T_MKPF-MBLNR. * Get Vendor Name from 'internal buffer' or database READ TABLE T_LFA1 WITH KEY LIFNR = T_MSEG-LIFNR BINARY SEARCH. IF SY-SUBRC <> 0. SELECT SINGLE NAME1 INTO T_LFA1-NAME1 FROM LFA1 WHERE LIFNR = T_MSEG-LIFNR. IF SY-SUBRC <> 0. T_LFA1-NAME1 = TEXT-001. ENDIF. APPEND T_LFA1 SORTED BY LIFNR. ENDIF. * Write out Detail Lines. WRITE:/ T_MSEG-MATNR, T_MSEG-MENGE, T_MSEG-MEINS, T_MSEG-LIFNR, T_LFA1-NAME1. ENDLOOP. ENDLOOP.
HCLT Confidential
Page 21 of 21