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

Learn ABAP (10)

A very simple way to display graph in SAP (2 Dimensions)

Report zgraph.

Types :
Begin of itab,
text type c,
value type i,
end of itab.

Data :
itab type table of itab,
wa_itab type itab.

wa_itab-text='Audry'.
wa_itab-value=30.
append wa_itab to itab.

wa_itab-text="Charlie'.
wa_itab-value=100.
append wa_itab to itab.

call function 'Graph_2D'
exporting
titl = 'Sudents GPA'
tables
data = itab.

Learn ABAP (9)

Do you know that SAP has the capability to make graph above in 3D and 2D ? Try yourself this code below :

Report zgraph_test.

Types :
Begin of ty_data,
myvar type c,
q1 type i,
q2 type i,
End of ty_data.

Types :
Begin of ty_options,
options(50) type c,
End of ty_options.

DATA:

it_data TYPE STANDARD TABLE of ty_data,

wa_data TYPE ty_data.

DATA:

it_options TYPE STANDARD TABLE of ty_options,

wa_options TYPE ty_options.

wa_data-myvar = 'Y1',

wa_data-q1 = 30.

wa_data-q2 = 50.

append wa_data to it_data.

wa_options-options = 'P3TYPE = TO'.

append wa_options to it_options.

call function 'graph_matrix_3D'

exporting

col1 = 'x1'

col2 = 'x2'

tables

data = it_data

opts = it_options

exceptions

others = 1

Learn ABAP (8)

How to show contents of a table in a smartform ? I'll show you the fastest way to show table that we have made in Learn ABAP (1) i.e table ztylfa1 by smartforms.

/nsmartforms

Global Settings > Global Definition >
Tab [Global Data]
IT_TABLE TYPE TABLE OF ZTYLFA1
WA_TABLE TYPE ZTYLFA1

Tab [Initialization]
Output parameters : IT_TAB
Code : select lifnr name1 regio from ztylfa1 into table it_tab.

Pages and Windows > %PAGE1 > MAIN > %LOOP1
Tab [Data]
Operand IT_TAB INTO WA_TAB

Pages and Windows > %PAGE1 > MAIN > %LOOP1 >%TEXT1
Tab [General Attributes]
&WA_TAB-lifnr& &WA_TAB-name1& &WA_TAB-regio&

Then execute the smartform. You'll see the contents of ztylfa1 as seen on Learn ABAP(1). Congratulations.

Learn ABAP (7)

In the previous lesson - Learn ABAP (6)- I showed you the simplest way to use smartforms. Now, it's time to show you the more interesting part as an ABAP-er. How to call the smartform inside ABAP ?

/nse38
name : zcallsmartform01
attributes : type = 1 (executable program), application = * (cross application)

ABAP code :
report zcallsmartform01.
call function '/1ABCDE/FG00000000'. "it's the function module code

then execute your program with the output device LP01.
Voila ! you see the same output "First Thing First"

Learn ABAP (6)


SMARTFORM

What is SAP smartform ? It's like a report builder in visual programming. The easiest way to show you the procedures is by following these steps :
1. What we want to show
-------------------------
/nsmartforms
Form : zsmartform_01
Pages and Windows > %Page1 > Main Window > %Text1
Text : text_practice
General Attributes : "First Thing First"
click [SAVE], Local Objects
click [ACTIVATE]
click menu [ENVIRONMENT], [FUNCTION MODULE NAME] copy the code

2. Print Preview
---------------
/ose37
paste the code of function module name
click [DISPLAY]
click [TEST]
click [EXECUTE]
OutputDevice : LP01, Page selection : 1, Title : Practice
click [PRINT PREVIEW] then will show the text "First Thing First"


That's it !

Learn ABAP (5)

Internal Tables
In this section, we will learn on how to define an internal table, how to append rows, how to display the rows, and how to find a row which match a search kriteria. By the way, what is an internal table ? Is it a table pre-built in SAP such as lfa1 (vendor master) ? No, it is not. An internal table is a temporary table stored in RAM for manipulating data only.

/nse38

report zinternaltable.

data :
begin of intable occurs 5,
row1(2),
row2(6),
end of intable.

intable-row1 = 'ID'.
intablet-row2 = 'karno'.
append intable.

intable-row1 = 'US'.
intable-row2 = 'george'.
append intable.

intable-row1 = 'CH'.
intable-row2 = 'zhang'
append intable.

loop at intable.
write : / sy-tabix, intable-row1, intable-row2.
endloop.

read table intable with key row1='US'.
write : / sy-uline,
/ intable-row1, intable-row2.

Learn ABAP (4)

Logic Statement
IF statement
-------------
report zifstatement.

data:
begin of data1,
f1 value 'a',
f2 value 'b',
end of data1.

if data1-f1 = data1-f2.
write / 'the same'.
else.
write / 'different'.
endif.

CASE statement
----------------
report zcasestatement.

parameters p1 type i default 5.

case p1.
when 5. write / '5'.
when others. write / 'not 5'.
endcase.


Loop Statement
DO statement
--------------
report zdostatement.
do 13 times.
write sy-index.
enddo.


WHILE statement

-----------------

report zwhilestatement.

while sy-index <>

write sy-index.

endwhile.






Learn ABAP (3)

How to find the differences of dates ?
As simple as subtracting the two dates, but the variable should be in a type of numeric value such as i or p. See the example below :

/nse38

report zdiffdate.
data : date1 type d,
diffdate type i.

date1='20090701'.
diffdate = sy-datum - date1.
write / diffdate.

Learn ABAP (2)

Make A Dialog Screen (Selection Screen)
The easiest one is by using 'parameters'. Let's see this example :

/nse38

report zdialogscreen.
parameters :
p1(20) type c,
p2 as checkbox,
p3 as checkbox.
write : / 'p1 = ',p1, /p2, /p3

How to change the display label ? I don't like p1,p2,p3...what kind of food is that :). Follow this path : Goto > Text Elements > Selection Text. Then input texts that represent p1,p2,p3.


Field String
To mimic the structure in the DDIC, we can define it as a field string. There are two ways, one is by using [data] statement, the second one is by [tables] statement. Both ways do not save the data permanently to tables, just reside in memory for a temporary moment. While in [data] statement, we need to define first the group of fields, but in [tables] statement, we need the pre-existing created tables. Let's see the samples below :

report zdata.
data:
begin of datastructure,
productname type c,
price type p,
end of datastructure.
datastructure-productname = 'desktop PC'.
datastructure-price = 1000.
write: / datastructure-productname, datastructure-price.

report ztables.
tables zlfa. "remember, zlfa tables should be preexisted.
zlfa-name1 = 'desktop'.
zlfa-prize = 1000.
write : /zlfa-name1, zlfa-prize.

How to Define New Type of Variable

"...I want to simplify to define the type variable, what should I do ?..." Simplifying the type of variable may improve the readibility of your program. For example :

/nse38

report znewtype.

types mychar(10) type c.

data :

myvariable type mychar value 'Bellanova'.

write : myvariable.

Learn ABAP (1)

To practice ABAP, use SAP IDES (Internet Demo and Evaluation Systems). It contains links to functional modules and technical modules. Unlike MiniSAP which includes only ABAP module. So, are you ready with SAP IDES ? OK, then let's go the ABAP tutorial below.

When you practice ABAP, please be careful of ruining the existing tables...mmm, how horrible to destroy the tables in SAP IDES. You practice it in the office, don't you ? SAP IDES is not sold. I think, you have a chance to utilize SAP IDES when your company buy SAP. It needs special registration key.

So, here we are. Sorry for all of my RCU fans club, due to short of time, I don't use RCU models for now. Perhaps, one of you may assist me in RCU models ? :-)

CREATE TABLE
First thing first, let's learn to create a transparent table. The tcode is /nse11 (ABAP DDIC, Data Dictionary). We'll imitate the real SAP vendor master (LFA1).

Table name : ztylfa1
Field name :
lifnr (PK) char(3) - vendor's ID
name1 char(30) - vendor's name
regio char(10) - vendor's region, state, province
DE name : zdelifnr, zdename1, zderegio
DM name : zdmlifnr, zdmname, zdmregio

Then populate these data to the table ztylfa1 by following this path Utilities > Table Contents > Create Entries.
--------------------------
Lifnr name1 regio
--------------------------
001 Prima Italian
002 Duo Michigan
003 Treize Bombay
004 Quators Kuala
005 Cinq Geneva
--------------------------

To display the table contents, use this path utilities > Table contents > display.
A very shortcut way, is to use tcode :
/nse16 => display and update
/nsm30 => table with dialog

QUERY TABLE
REPORT ztxquery.
tables ztylfa1.
select * from ztylfa1
write: /n sy-dbcnt, ztylfa1-lifnr, ztylfa1-name1, ztylfa1-regio.
endselect.
if sy-subrc<>0.
write 'Not found'.
endif.

LINK TABLES WITH FOREIGN KEY
Suppose that you want that the ztylfa1-regio field takes data from a separate tabel with validation, then make -regio field as foreign key. Set up the second table (called check table) that contains the value of -regio1. /nse11

Table name : ztycheck
Field name : regio (PK)
DE : zderegio

Populate ztycheck with these data by following this path utilities>create entries
Value of regio : ITA, MIC, BOM, KUA, GEN

Testing the foreign key relationship by inputing data to ztylfa1 table by following this path utilities>table contents>create entries.

Tomorrow, we will see more on DDIC. See you guys !



ABAP Tips

ABAP tables
There are three tables types in SAP, i.e : transparent, pooled and cluster. The tables which hold application data are only transparent tables that you use in day-to-day basis. So, don't worry about the others.

Link between table, field, data element and domain
A table is composed of fields. A field links to data element (DE). A data element links to a domain (DM).
What is data element ? field labels, F1 help.
What is data domain ? field length, data type.

What is the best way to create a table in SAP ?
You may create a table starting from DM, DE to a table or in an opposite way. The first one is called bottom-up approach (intuitive but cumbersome). The second one is called top-down approach (easier and practical).

How to create DE in SAP R/3 release 4.7 and above ?
You experience confused in reading ABAP books, perhaps the old editions ones. Some terms, some screens and some procedures may differ if you use the latest release of SAP IDES (Internet Demo and Evaluation Systems). So of course, there are some differences in SAP ECC 6.0 and SAP R/3 4.7 versions. One hint for compatibility view : in SAP R/3 release 3.0, you came to create DE in "Object Name" field. But in SAP R/3 release 4.7, you call it "Data Type".

How to show line number in ABAP editor environment ?
Follow this path : utilities > settings > back-end editor > with line numbering

How to put comments in ABAP editor ?
Use * (asterisk) for the entire line of comments, and use " (double quote) to put a comment at the end of a syntax.

How to write in new line ?
Pascal language use [writeln] to begin a new line, but how about in ABAP ? use backslash, i.e [write /]

What is a chain operator ?
A chain operator [ : ] is used to represent efficiently repeated syntax such as ...
tables lfa1.
tables lfa2.
becomes in one line i.e tables : lfa1, lfa2.

How to terminate endless loop ?
Restart the application server....and you will have a phone rang from your supervisor and your users he..he..he...it's not a joke :( Endless loop happens when you do not put a brake in between of looping statement. What is looping statement ? DO...ENDDO, WHILE...ENDWHILE. I urge you to create a new session before you practice using looping statement. How to create a new session ? Follow this path : system > create session. In case one of your session experience in an endless loop, just turn off one session and use another session. So the best answer to this very specific problem...use a preventive solution above ! Have a try...Proof it by making an endless loop :D

All SAP products are trademark of SAP AG. This blog is not affiliated with nor endorsed by SAP AG.