Vous êtes sur la page 1sur 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 Date Prepared by / Significant Changes


No. Modified by

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 Purpose .................................................................................................................................... 4
1.2 Scope ....................................................................................................................................... 4
2 SAP Development Standards .......................................................................................................... 4
2.1 Multi Country Approach............................................................................................................ 4
2.1.1 Country Identifier ........................................................................................................ 4
2.2 MODULE IDENTIFICATION............................................................................................................. 5
2.3 Repository Browser Objects..................................................................................................... 5
2.3.1 Development Class .................................................................................................... 5
2.4 Online Program ........................................................................................................................ 6
2.5 Dialogue Module ...................................................................................................................... 6
2.6 Transaction Code ..................................................................................................................... 7
2.7 Function Groups....................................................................................................................... 7
2.8 Includes .................................................................................................................................... 8
2.9 SET/GET parameter ID ............................................................................................................ 8
2.10 AREA MENU....................................................................................................................... 8
2.11 Message Class/ Number..................................................................................................... 9
2.12 Dictionary Objects ............................................................................................................... 9
2.12.1 Domain....................................................................................................................... 9
2.13 DATA ELEMENT................................................................................................................. 9
2.14 STRUTURES .................................................................................................................... 10
2.15 TABLE TYPE .................................................................................................................... 10
2.16 VIEW ................................................................................................................................. 11
2.17 TABLE ............................................................................................................................... 11
2.17.1 BUFFERING ............................................................................................................ 11
2.17.2 INDEXES ................................................................................................................. 12
2.17.3 LOCK OBJECT ........................................................................................................ 12
2.18 SEARCH Help................................................................................................................... 13
2.19 ABAP Editor ...................................................................................................................... 13
2.19.1 Documenting with ABAP Code ................................................................................ 13
2.19.2 Program Documentation .......................................................................................... 13
2.19.3 Extended Program Check........................................................................................ 13
2.19.4 Modification to Code ................................................................................................ 14
2.19.5 Multi Language and Currency.................................................................................. 14
2.19.6 ABAP List Viewer..................................................................................................... 14

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


PLCGN010 SAP Coding Guidelines

2.20 Function Builder ................................................................................................................ 14


2.21 Menu Planer...................................................................................................................... 15
2.21.1 GUI STATUS (MENU + FUNCTION KEYS) .................................................................... 15
2.21.2 GUI Title................................................................................................................... 15
2.22 SCREEN Painter............................................................................................................... 15
2.22.1 LAYOUT ..................................................................................................................... 15
2.22.2 ELEMENT List ......................................................................................................... 15
2.22.3 SCREEN Attributes.................................................................................................. 15
2.22.4 FLOW Logic ............................................................................................................. 16
2.23 EFFICIENCY..................................................................................................................... 16
2.24 Coding Standards ............................................................................................................. 16
2.25 Version Control ................................................................................................................. 16
3 Coding Standard Example ............................................................................................................. 16

1 Introduction
1.1 Purpose
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

2 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 Code
Great Britain GB
Ireland IE
Netherlands NL
Belgium BE
Norway 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 Description Module Description


BC Basis Components PS Project System
FI Financial Accounting EH Environment, Health & Safety
TR Treasury PA Personnel Management
CO Controlling PT Personnel Time Management
EC Enterprise Controlling PY Payroll Accounting
LO Logistics - General PE Training and Event Management
SD Sales and Distribution SV Service
MM Materials Management AP Advanced Planner and Optimizer
- APO
IM Investment Management BW SAP Business Information
Warehouse
LE Logistics Execution EO SAP Electronic Commerce
QM Quality Management MS Mobile Sales
PM Plant Maintenance IS Industry-Specific Components
CS Customer Service
PP Production Planning and
Control

Bespoke Description
TC Terms and Conditions
YY Miscellaneous

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

Identifier Description
REPO Read Only Report
PROG Periodic Insert/Change Internal to
SAP
INTF Periodic Insert/Change External to
SAP
PRIN Print Program (SAPScript Driver)
DATA One Time Master/Transaction Data
Load
EXTR One Time Data Extract
OTHE Other

Examples:

ZBCREPO001 - Basis related report


ZMMINTF001 - 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> where <wxyz> = 4 character ID

Example:

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> where <wxyz> = 4 character ID

Example:

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> where <nnn> = sequential identifier

Example:

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 where <mainabap> = main ABAP object

Example:

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> where <description> = short text

Example:

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> where <description> = short text

Example:

Z_APL-APL Menu

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> where <nnn> = message number

Example:

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> where <description> = short text

Example:

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> where <mm> = module identifier


<description> = short text
Example:

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> where <mm> = module identifier


<description> = short text

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

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> where <mm> = module identifier


<description> = short text

Example:

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 What is Buffered When to Apply


Full Entire table when one record Table <30KB, many reads, few
read writes
Generic Records matching key of record Access by Key<32 bytes long
read
Single Single record read Large Table, many reads of few
records
None Nothing 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> where <table> = Table

Example:

EZ_ZXINBND - Lock object for ZXINBOUND using in OCR Inbound Interface

Other attribute settings / actions to note:


 None

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 Description
i_callback_program Program control is passed to when list is left ( SY-REPID
)
it_fieldcat Fields for output ( SLIS_FIELDCAT_ALV )
It_sort Fields for sort ( SLIS_SORTINFO_ALV )
t_outtab 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 VALUE-
REQUEST 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 Coding Standard Example


REPORT BADCODE.
TABLES: MKPF, BKPF, MSEG, LFA1.
DATA V_BUPER LIKE T009B-POPER.
DATA V_GJAHR LIKE T009B-BDATJ.
DATA V_COUNT TYPE I.
DATA 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

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.
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, " Header: Material Document


MSEG, " Document Segment: Material
LFA1. " Vendor master (general
section)
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, " Doc No.
USNAM LIKE MKPF-USNAM, " User Name
BKTXT LIKE MKPF-BKTXT, " Header Text
END OF T_MKPF.
* Vendor Description
DATA: BEGIN OF T_LFA1 OCCURS 0,
LIFNR LIKE LFA1-LIFNR, " Vendor No.
NAME1 LIKE LFA1-NAME1, " Vendor Name
END OF T_LFA1.
* Document Detail
DATA: BEGIN OF T_MSEG OCCURS 0,
MBLNR LIKE MSEG-MBLNR, " Doc No.
MATNR LIKE MSEG-MATNR, " Material
LIFNR LIKE MSEG-LIFNR, " Vendor
MENGE LIKE MSEG-MENGE, " Quantity
MEINS LIKE MSEG-MEINS, " Unit

Version No. : 1.0 Date: 2nd Dec 05 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 MBLNR MATNR LIFNR MENGE MEINS


INTO TABLE T_MSEG
FROM MSEG
FOR ALL ENTRIES IN T_MKPF
WHERE MBLNR = T_MKPF-MBLNR
AND 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