540 likes | 797 Views
Lesson 1: Introduction to ABAP OBJECTS. Todd A. Boyle, Ph.D. St. Francis Xavier University. Section Report. Develop a simple report that will accept two numbers from the user and display the sum of the two numbers on a report. What is ABAP OBJECTS?.
E N D
Lesson 1: Introduction to ABAP OBJECTS Todd A. Boyle, Ph.D. St. Francis Xavier University
Section Report • Develop a simple report that will accept two numbers from the user and display the sum of the two numbers on a report.
What is ABAP OBJECTS? • Advanced Business Application Programming. • Previously known as ABAP/4. • Since renamed to ABAP OBJECTS. • Why do we need to develop ABAP applications in SAP R/3?
Why use ABAP? • ABAP OBJECTS is the programming language SAP developers use to build transactions and reports and customize SAP R/3. • The main purpose of ABAP OBJECTS is to provide additional functionality to existing SAP applications. • We do not use ABAP OBJECTS to develop custom applications from scratch.
Defining a program • REPORT program name. • The REPORT command has various features to format output to the screen or printer. We will discuss these features later. • program name must start with a Z. • You can also use a Y, but Z is used far more often.
Commenting your code • It is expected that you will have sufficient comments in all the ABAP OBJECTS programs you write. Why? • It is EXPECTED of all PROFESSIONAL software developers. • Due to the high turnover rate of SAP R/3 & ABAP OBJECTS consultants, other people may be stuck maintaining your program.
Adding comments in ABAP OBJECTS To comment an entire line use an asterisks (*). * This program will…
Adding comments in ABAP To comment part of a line use the double quote (“) <program code> “This statement will….
Variables and Constants • We have given the program a name and added a description of the program in the comments. • To manipulate and gather data for processing we need variables, records, and constants.
Variables in ABAP • MUST be declared before they are used. • Can be declared anywhere in the program, BUT for consistency they should be declared at the beginning of a program or subroutine. • Begin each variable with a letter and then a mixture of letters and numbers
Variables • Use the underscore (_) to separate distinct words. • DO NOT use the hyphen (-). This has a special purpose that we will discuss later. • NO reserved words (the same name as an ABAP command).
Variables • Variables defined at the program level are considered global. • Variables in defined in a subroutine are visible only to the subroutine.
Declaring variables • DATA var[(LENGTH)] [TYPE type] [DECIMALS number] [VALUE initial value] • var is the name of the variable • [(length)] is the size of the variable • TYPE is the type of variable. The possible data types for variables in ABAP are:
Data types for ABAP OBJECTS • Type/Description/Example • C Character hello world • D Date 19990101 • N Numeric text 10000 • P Packed Decimal 22.50 • T Time 104500 • I Integer 12123
Declaring Variables: Example 1 • Declare a variable that will hold a person’s job title. • It should be 10 characters in length and be initialize to MANAGER. DATA JOB_TITLE (10) TYPE C VALUE ‘MANAGER’.
Declaring Variables: Example 2 • Declare a variable to hold the employee ID number. • When we declare a variable of type I (i.e., Integer) we do not specify the variable size. • DATA EMP_ID TYPE I.
Declaring Variables: Example 3 • Declare a variable to store the person’s date of birth. • Initialize the variable to December 31, 1900. DATA DATE_OF_BIRTH TYPE D VALUE ‘19001231’.
Declaring Variables: Decimal Type • DATA var[(LENGTH)] [TYPE type] [DECIMALS number] [VALUE initial value] • The DECIMAL is the number of decimal places for the packed decimal type. • The size of the field = digits before the decimal + digits after the decimal. • IF A FIELD HAS 6 DIGITS BEFORE THE DECIMAL AND 2 DIGITS AFTER , THEN IT MUST BE DECLARED AS A SIZE 8.
Declaring Variables: Decimal Type • Declare a variable that will store the employee income. • It is comprised of 8 digits before the decimal point and 2 digits following it. • Initialize the variable to 1 DOLLAR.
Answer • DATA EMP_INCOME (10) TYPE P DECIMALS 2 VALUE ‘1.00’.
Constants • Data that does not change its value during program execution. • Defined the same as DATA variables. • Example: • CONSTANTS PLANT_NUM TYPE I VALUE ‘6523’.
Run-time Parameters • Allows the user to pass data to the program at run time. • At run time, the default SAP R/3 screen will be display with the parameters you have coded. • PARAMETERS parameter name TYPE type.
Run-time Parameters • If we wanted the user to enter an employee ID. • PARAMETERS EMP_ID TYPE I. • The parameter name is limited to 8 characters. • For the lesson example, the input screen would look like this:
Example Program Let us review what we covered so far, by examining the simple ABAP calculator program. ZWSCALC
ABAP OBJECTS Statements • ABAP OBJECTS does not care where a statement begins on a line. • However, to ensure program professionalism, you should indent sections of code for readability. • You can easily do this using PRETTY PRINTER.
ABAP OBJECTS Statements • Multiple statements can be placed on a single line. To improve readability, this is not recommended. • Blank lines can be placed anywhere in the program and should be used to improve readability. • ALL ABAP OBJECTS statements (except comments) must end with a period (.).
Coding Statements • We will now discuss how to display data including: • Moving data into a variable • Moving data between variables • Performing basic computations • Writing data to the report/screen • Coding your first complete ABAP OBJECTS program.
MOVE • Move data into or between variables is done using the MOVE statement. There are two forms of the MOVE statement. • MOVE value TO var • var = value
Moving values into a variable • MOVE ‘10’ TO PERSON_AGE. • OR • PERSON_AGE = ‘10’.
Moving data from one variable to another • Move the contents of PERSON_AGE to DISPLAY_AGE: MOVE PERSON_AGE TO DISPLAY_AGE • or DISPLAY_AGE = PERSON_AGE.
Computations • COMPUTE variable = expression. • COMPUTE is optional. • ADD value TO variable. • SUBTRACT value FROM variable. • MULTIPLY variable BY value. • DIVIDE variable BY value.
Computations • COMPUTE INCOME_TAX = GROSS_INCOME * TAX_RATE. • ADD 1 TO REC_COUNTER • MULTIPLE GROSS_INCOME BY TAX_RATE. (What is the problem with this?)
Outputting text • To output data to the screen, we use the write command. • WRITE ‘text’. • WRITE field name.
WRITE • WRITE ‘Boyle’. • WRITE LAST_NAME. • Some of the basic write options include: : = groups of text, variables. / = break to a new line WRITE: /, FIRST_NAME, LAST_NAME.
Write: Outputting groups of text • If we wanted to write the following: Weekly Sales Total: 13456.32 • We could code: WRITE CON_SALE_HEADER. WRITE W_SALES_TOT. • OR WRITE: CON_SALE_HEADER, W_SALES_TOT.
Write: Breaking to a new line WRITE “HELLO”. WRITE “WORLD”. • would give us: • HELLO WORLD
Write: Breaking to a new line • What if we want the text on two separate lines? • We use a “/” following the WRITE WRITE / ‘HELLO’. WRITE / ‘WORLD’. • We can combine both grouping and breaking: WRITE: /, CON_SALE_HEADER, W_SALES_TOT.
Review • Let us finish the calculator program. ZWSCALC • Modify the program to accept four numbers from the user and calculate and display the average. • Instead of retyping the program you can create a copy of it.