0% found this document useful (0 votes)
409 views21 pages

SAP Coding Guidelines

Uploaded by

haryani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
409 views21 pages

SAP Coding Guidelines

Uploaded by

haryani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SAP Coding Guidelines

SAP Coding Guidelines


Version No.: 1.0 Date: 02nd Dec 05

ECS Quality, Noida HCL Technologies

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

SAP Coding Guidelines

Revision History Version No. Date Prepared by / Modified by Significant Changes

1.0

02nd Dec 05

Taruna Bhatnagar

Glossary Abbreviation Description

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 2 of 21

PLCGN010

SAP Coding Guidelines

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

MODULE IDENTIFICATION............................................................................................................. 5 Repository Browser Objects..................................................................................................... 5 2.3.1 Development Class .................................................................................................... 5

2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12

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

2.13 2.14 2.15 2.16 2.17

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

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 3 of 21

PLCGN010

SAP Coding Guidelines

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

2.23 2.24 2.25 3

EFFICIENCY..................................................................................................................... 16 Coding Standards ............................................................................................................. 16 Version Control ................................................................................................................. 16

Coding Standard Example ............................................................................................................. 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

SAP Development Standards

2.1 Multi Country Approach


2.1.1 Country Identifier
All developments should be created to be country independent, where it becomes necessary to limit functionality to a single country the suffix _<CC> must be appended to the object name. <CC> matching the corresponding values from SAP customizing table T005, some common example are shown below. Language and currency differences do not make a development country dependent as the texts and monetary elements will be coded to allow translation or conversion.

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 4 of 21

PLCGN010

SAP Coding Guidelines

Country Great Britain Ireland Netherlands Belgium Norway

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

2.3 Repository Browser Objects


2.3.1 Development Class
General developments that will be transported should be assigned to class ZDV2.

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 5 of 21

PLCGN010

SAP Coding Guidelines

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

2.4 Online Program


All online ABAP coding should take the following naming convention. Z<mm><identifier><nnn> where <mm> = module identifier <identifier> = See table below <nnn> = sequential identifier Description Read Only Report Periodic Insert/Change Internal to SAP Periodic Insert/Change External to SAP Print Program (SAPScript Driver) One Time Master/Transaction Data Load One Time Data Extract Other

Identifier REPO PROG INTF PRIN DATA EXTR OTHE Examples: ZBCREPO001 ZMMINTF001 -

Basis related report Purchase Order interface

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

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 6 of 21

PLCGN010

SAP Coding Guidelines

All dialog ABAP coding should take the following naming convention.

SAPMZ<wxyz> Example:

where

<wxyz> = 4 character ID

SAPMZDEVS- Developers Scripts


Other attribute settings / actions to note: Accept default TOP include name MZ<wxyz>TOP Enter the title in Proper case, remember this will appear in the title-bar of the SAP session 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) HEADER and edit

2.6 Transaction Code


If a transaction code is created then this must be named as follows

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

2.7 Function Groups


Function modules should be grouped based on usage as the entire function module is loaded when a single function is called, there is hence no need to assign a module identifier.

Z<nnn> Example:

where

<nnn> = sequential identifier

Z001-Extended Table Maintenance Other attribute settings / actions to note: None

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 7 of 21

PLCGN010

SAP Coding Guidelines

2.8 Includes
Where common code can be re-used within a series of developments you should create includes.

Z<mainabap>_INC Example:

where

<mainabap> = main ABAP object

ZBCHEADINGS_INC -Include for Compass standard page headings Other attribute settings / actions to note: As per Online Program but setting Type to I

2.9 SET/GET parameter ID


If no suitable standard parameter exists then create using the following convention.

Z<description> Example:

where

<description> = short text

ZFB-Flag to activate Force to Background reporting


Other attribute settings / actions to note: None

2.10 AREA MENU


You should always ensure that transactions or buttons placed on menu can also be accessed via the keyboard.

Z_<description> Example:
Z_APL-APL Menu

where

<description> = short text

Other attribute settings / actions to note If copying from standard keep the <description> as original and enter the short text as Copy of <description>

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 8 of 21

PLCGN010

SAP Coding Guidelines

2.11 Message Class/ Number


All customer messages are to be created in class ZC1 you must NEVER modify or copy existing messages, when no free message is available in class ZC1 you must ask for next class to be created centrally.

<nnn> Example:

where

<nnn> = message number

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!

2.12 Dictionary Objects


2.12.1 Domain
As the domain defines only type and length you should not need create a domain in the customer range unless defining value lists, therefore use the relevant SAP supplied domains. e.g. CHARxx, DECxx, INTxx. Use the search help on the dictionary screen to identify the most suitable domain, if a new domain is required you must ask for this to be created centrally. Other attribute settings / actions to note: None

2.13 DATA ELEMENT


As the data element is use specific you are more likely to create a data element in the customer range. However always use the search help to identify an existing SAP or customer element that could be used. If none can be found then create using the following convention.

Z<description> Example:

where

<description> = short text

ZBSU - BSU (uses SAP standard Domain BZRIK = Sales district )


Other attribute settings / actions to note:
Version No. : 1.0 Date: 2nd Dec 05 HCLT Confidential Page 9 of 21

PLCGN010

SAP Coding Guidelines

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

<mm> = module identifier <description> = short text

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

2.15 TABLE TYPE


Similar to structures the table type is the dictionary equivalent to the new ABAP internal tables declaration and will benefit large projects and program suites as structures above. You can create these definitions in the Dictionary and reference them using the TYPE statement. Create using the following convention.

Z<mm><description> Example:

where <mm> = module identifier <description> = short text

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

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 10 of 21

PLCGN010

SAP Coding Guidelines

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.

Z_ <description> where <description> = short text Example:


Z_BSEG-Limited View from BSEG Table. Other attribute settings / actions to note: Ensure both the <description> and maintenance short text aid developers using search help

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

<mm> = module identifier <description> = short text

ZFICONF001- Compass FI configuration


Other attribute settings / actions to note: Bespoke Configuration tables must use CONF at the start of their <description > Ensure both the <description> and maintenance short text aid developers using search help Delivery class will generally be C for customising (lookup) data or A for transaction data Table maintenance must be generated for all class C tables and class A if requested All tables should include the client field MANDT as the first field All bespoke field names should all start with Z All key fields should be assigned a check and/or value table Technical Settings must be completed for all tables including Buffering and Log changes Try to set an accurate data class and size category to aid

2.17.1 BUFFERING

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 11 of 21

PLCGN010

SAP Coding Guidelines

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

Example: ZFISALEDET~Z01 MANDT/ZMAT (Material Lookup for Reconciliation)

Other attribute settings / actions to note: None

2.17.3 LOCK OBJECT


For any bespoke table, which requires exclusive updating, a Lock Object must be created and checked within the ABAP source code. It is not necessary to have all key fields of a table within the lock object, but in most cases you should.

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 12 of 21

PLCGN010

SAP Coding Guidelines

EZ_<table> Example: EZ_ZXINBND None

where

<table> = Table

Lock object for ZXINBOUND using in OCR Inbound Interface

Other attribute settings / actions to note:

2.18 SEARCH Help


An elementary search help will support the possible entries (F4) function allowing searches based on fields from one or more linked tables, returning one or more of these fields for input. These can be bought together into a collective search help and are improved versions of the release 3.x help views and matchcodes.

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 ABAP Editor


The editor is a very flexible tool and its use outside of the scope of this document, however this section does cover the standard approach to coding that should be applied

2.19.1

Documenting with ABAP Code

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

Extended Program Check

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 13 of 21

PLCGN010

SAP Coding Guidelines

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

Multi Language and Currency

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

ABAP List Viewer

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

2.20 Function Builder


Individual functions will need to be assigned to a suitable function group, it is worth noting that when a function module is called the whole group is loaded into memory. Therefore is it advisable to restrict the number of functions, grouping them logically based on use, rather than on name or description.
Version No. : 1.0 Date: 2nd Dec 05 HCLT Confidential Page 14 of 21

PLCGN010

SAP Coding Guidelines

Z<mm>_<description>

where <mm> = module identifier <nn> = sequential identifier

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 Menu Planer


The Interface Objects option gives an overview of the main elements of a screen GUI i.e. Status (menu + function keys) and title.

2.21.1

GUI STATUS (MENU + FUNCTION KEYS)

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 SCREEN Painter


Only developers with either BC410 or a good working knowledge of dialog programming should be developing screen-based transactions.

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

2.22.2 ELEMENT List


Where possible all screen elements should be based on an existing dictionary object, where this is not possible you must refer to program objects. There are no standard covering the attributes of any screen element.

2.22.3 SCREEN Attributes

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 15 of 21

PLCGN010

SAP Coding Guidelines

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.22.4 FLOW Logic


The flow logic must contain the PROCESS BEFORE OUTPUT (PBO) and PROCESS AFTER INPUT (PAI) keywords even if no modules are called. Only add the PROCESS ON VALUEREQUEST or PROCESS ON HELP-REQUEST if you are adding this bespoke functionality. Screen specific modules should be suffixed _<dyn> where <dyn> = screen number Other modules should contain a CASE sy-dynnr statement to modify functionality based on screen number Always add a MODULE EXIT AT EXIT-COMMAND to capture exit functions Do not place SELECT statements in the PBO of a screen, code in the PAI of the calling screen if possible

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.

2.24 Coding Standards


We have reviewed the use of ABAP syntax on an ongoing basis and have included in the Developers Quick Reference Guide (QRG) a definition of how these must be applied within Compass Group. No further standards will therefore be held within this document.

2.25 Version Control


Any single SAP object should only be open in one transport request and under a single developer ID, we are investigating changes in authorizations to prevent an object transferring from one request to another. The editor lock should now be set on any source code, preventing ownership being taken by another developer working within the same request.

3
REPORT BADCODE.

Coding Standard Example

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).

DATA: BEGIN OF REPORT OCCURS 0,


Version No. : 1.0 Date: 2nd Dec 05 HCLT Confidential Page 16 of 21

PLCGN010

SAP Coding Guidelines

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

SAP Coding Guidelines

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

SAP Coding Guidelines

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

" Doc No. " User Name " Header Text

" Vendor No. " Vendor Name

" " " " "

Doc No. Material Vendor Quantity Unit

HCLT Confidential

Page 19 of 21

PLCGN010

SAP Coding Guidelines

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

SAP Coding Guidelines

SELECT INTO FROM FOR WHERE AND

MBLNR MATNR LIFNR MENGE MEINS TABLE T_MSEG MSEG ALL ENTRIES IN T_MKPF MBLNR = T_MKPF-MBLNR MJAHR = P_YEAR.

* Sort by Key ready for binary search SORT T_MSEG BY MBLNR.

* 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.

Version No. : 1.0 Date: 2nd Dec 05

HCLT Confidential

Page 21 of 21

You might also like