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

How to use Code Inspector in quick steps?

AdHoc Inspection for ABAP Program in Quick Method

We will see how we can do a quick code inspection on a single program or an
object set. This is also termed as an Ad Hoc Inspection, since the inspection results are not
saved and are not available for future.


1. Call the SAP transaction SCII. Alternatively, you may go to transaction SCI and on
the main screen, leave the Inspection field blank and press the Create button below
the Inspection Input field. The screen appears as follows:




2. Select the Single radio-button option. From the list box, select the Program option
and then enter the name of the program in the field provided. We enter the name
of a previously created program ZST9_TEST_FOR_ALL_ENTRIES_2.



3. From the area in the lower part of the screen, we will select the option Temporary
Definition. Then we select from the available checks that we want to be carried out.
Selecting or deselecting a particular category will include or exclude all checks within
the category in the inspection. For resetting to default values at any time, select the
menu path by going to Utilities | Set Initial. Alternatively, use the toolbar button
or press F7.



4. Finally, press F8 to carry out the inspection.


The code of the program mentioned is scanned and checked based on the selected checks.
The results are then displayed. The results comprise of tree structure (hierarchy) within nodes
corresponding to the checkbox selected. There are three columns shown for messages within
the different categories and then the checkboxes pertaining to the category in question.
These are Errors, Warnings, and Information messages. You may open the various
categories to see the detail of the messages found in each category. For example,
we have two information messages found under Use of Indexes in SELECT statement.





You may double-click on a particular message to reach the actual line of the code in the
program that generated the message in question.

While selecting the checks to be run from a particular category, a check may have further
attributes. This is denoted by the icon before it. You may click on the icon to view a list of
the attributes and choose the ones relevant for your requirement. If you find an icon, it means
that there is at least one value that must be set for the attributes contained within the check.
Otherwise, selecting the check will produce an error and the inspection will not run.



For running another inspection from the results screen, use the toolbar button .

For viewing the results in a compact display format, use the toolbar button .  


You have also the option of checking objects using object sets or the object contained within a
request. However, the transaction has a limitation that you may not check over 50 objects and
that the inspection may not be saved for reuse. In this case, the inspection is not stored and
does not have a name (that is, it is anonymous). The next recipe will cover the ones that will
overcome these limitations.


How to Sort or Sub-Total a table field in SMARTFORMS?

Sorting and Sub-totaling to table fields.

We will see how we can sort a given table within the Smart Form and calculate
totals based on a particular field as the sort criterion. In this recipe, we will use an example of
employees and their allowances and amounts.

We define a structure in the database ZST9_EMPLOYEES comprising three
fields PERNR, ALLOWANCE, and AMOUNT. We define a table type also based on this
structure, as shown in the following screenshot:




A table EMPLOYEE_TABLE (based on the defined dictionary table type) is included in the
TABLES tab of the Smart Form interface, and a corresponding work area WA_EMPLOYEE
in the global definition.

A table is then created on the Smart Form layout. The loop of the table is shown as follows:



Appropriate texts are created within the cells in order to print the employee number,
allowance, and amount values passed. A program is then created and the Smart Form is
called. A tabular output is generated.



We will see how sorting along with totaling may be so that the total of each
employee's allowance is printed at the end of each employee's details.


For subtotaling and sorting, proceed as follows:

1. Double-click the defined table node in the left-hand pane. In the right-hand pane, on
the Data tab, within Sort Criteria, select the Event on Sort End checkbox. Also, enter
PERNR as the field name.




2. This will add a PERNR Event on Sort End node.





3. We define a subtotal variable in the global definition. This is a temporary storage
variable for totals of each employee's allowances.



4. On the Calculations tab of the table, we enter the values shown in the
following screenshot:





5. Right-click the PERNR Event on Sort End node to create a table line. Use the same
line type used for the rows of the table (containing three cells).

6. For the second and third cell, we define texts outputting SUBTOTAL and VARIABLE
and SUBTOTAL respectively.

7. Also, a line of code is added after the text that clears the SUBTOTAL variable.





8. The final state will look like the one shown in following screenshot:




The settings we did in the Smart Form will output the subtotal of the allowances of each
employee as shown. At the end of each employee, the total allowances are calculated
and printed.




The entries made on the Calculation tab of the table totals all the amounts of a particular
PERNR field (the SORT criterion) and stores in the SUBTOTAL variable. The output will then be
generated at the EVENT on SORT end. After that, we clear the old value of SUBTOTAL so that
the value of the next employee may be calculated.

How to convert SmartForms into PDF document?

Converting Smart Forms to PDF output

we will see how the form output may be suppressed and returned as internal
table to our calling program and then how a PDF is generated within the program. We will
set values to some fields in the control structure of the Smart Form and use it with the
CONVERT_OTF_2_PDF function module.

For generating PDF output without showing the Smart Form on the screen, follow these steps:

1. First, we define two structures cont_parameters and myoutput based on the
dictionary structures ssfctrlop and ssfcrescl respectively.



2. Then, we assign 'X' to the setotf and no_dialog fields of the control structure.



3. The dynamic call of the Smart Form function module is then carried out.



4. Appropriate variables are then defined. The most important is the internal
table pdf_content used for storing the converted PDF output of the form.
The filesize and doc_archive tables are necessary for calling the function
module CONVERT_OTF_2_PDF.



5. The function module CONVERT_OTF_2_PDF is then called. The OTF parameter
passes the value of the field otfdata of the myoutput structure.



The control structure fields no_dialog and the getotf are assigned the value 'X'. This
ensures that the form output is suppressed but the form generated is returned to the program in
otf format. The call to the Smart Form function module (stored in the variable myfunction)
returns to the program the otf format in the otfdata field of the myoutput structure.


We then pass this otfdata to the function module CONVERT_OTF_2_PDF.
The function module converts the smart form otf to a PDF and stores it in the internal table
pdf_content passed to the function module for table lines. The PDF file may then be
saved on the desktop using the method CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD,
or e-mailed to another user.

How to incorporate and print several SMARTFORMS in one SPOOL request?

Printing several forms in one spool request


We will see how printed multiple forms may be included in a single spool
request. We will create a number of form pages comprising employees data. Each page
will contain one employee's salary information. The recipe will focus on the important
parts of the program related to Smart Form processing. We assume that an internal table
EMPLOYEE_LIST exists that contain the employee numbers of all personnel to be processed.


1. First, we call the function module SSF_FUNCTION_MODULE_NAME in order to get the
name of the Smart Form function module. The form name is passed to the function
module. The myfunction variable is based on the type RS38l_FNAM (not shown in
the following screenshot):





2. Then appropriate variables are defined for the Smart Form control structure and
output options based on dictionary structures.



3. The function module SSF_OPEN is then called.




4. A loop is run on the internal table employee_list for all employees. For each
employee, the form get_employee_data is called, which fetches the necessary
information of the employee in question and fills the internal table employee_data.
The no_open and no_close fields of the control parameters structure are assigned
the value 'X'. In addition, the tdnewid field of the output options structure is
assigned 'X'.


5. The function module of the Smart Form is then called and necessary data of the
employee provided along with control parameters and output option structures.





6. Finally, the SSF_CLOSE function module is called.

After executing the program, multiple pages of the forms are generated. For each employee,
a separate page appears along with the necessary information.



The call of the function module SSF_OPEN opens the spool job for form printing.
The tdnewid assigned to 'X' creates a new spool request. Within the loop, the no_open
and no_close fields are set to 'X', which ensures the spool request is not opened or closed
within the loop. The function module of the Smart Form is called in the loop and generates
the necessary output of each employee. When all employees are processed, the SSF_CLOSE
function module is called and the spool request is closed.


We can also avoid the usage of the SSF_OPEN and SSF_CLOSE function modules. In this
case, when we program that on the first employee, the spool job is opened and when we
program that on the last employee, the spool job is closed.

How to use folder options for page protection?


Steps for Page Protection in SMARTFORMS


We will see how we can create a folder comprising a number of lines
(block of text ) so that they are all printed on the same page. If the space within a page is
not enough for printing all the lines, the entire text block is printed on the subsequent page,
that is, page protected.

For defining a folder with page protection, follow these steps:
1. Right-click on the MAIN window. Then, from the context menu that appears, choose
the option Create and then select Folder, as shown in the following screenshot:



2. Next, enter a suitable folder name. In the right-hand pane, on the Output Options tab
of the created folder, check the Page Protection indicator.



3. Then add a text under the given folder.


4. Add three lines to the text.




5. Save and activate your Smart Form.


When the form output is generated, irrespective of the number of lines printed above our
three-line text, a page protection is applied. If the space on the page is not enough so that all
three lines may be printed, a page break is automatically triggered and the lines are printed
on a fresh new page. Otherwise, the same page is utilized. In no case will the three lines be
broken into two pages.


How to improve the performance in SELECT Queries?

Improve the performance by adding hints in SELECT clause


If there are many indexes that contain common fields (or for any other reason), the database
optimizer cannot decide the right index to be used for a particular query.


Hints can be provided using the %_HINTS parameter. We will see the syntax for specifying
HINTS within your SELECT clause in order for a particular index to be used by the database
optimizer. We will see how the hints may be specified when the underlying database is
MS SQL Server.

We will have a small program that runs a SELECT statement on the table
ZST9_VBAK. We will use the index (Z12) that we created in the previous Creating Secondary
Indexes in Database Tables recipe.


For creating the program containing the SELECT clause with the HINT parameter, proceed
as follows:
1. A parameter P_AUFNR is declared for taking as input an order number.
2. Next, a data variable myvbeln is defined.
3. A SELECT statement is then written. The addition %_HINTS followed by the database
name, the table, and table index name is made to the SELECT clause.




It is a very simple addition. There is a special syntax used for specifying the name of the
database index to be used for the particular SELECT statement. The name of the index we
used can be taken from the index name as defined in the ABAP Dictionary. The table name
and the index name are specified within TABLE <tablename> ABINDEX(<suffix>). The syntax
should be proper because other than the database name check, the syntax checker does not
check the index name. Therefore, if a wrong index is used, the corresponding query will not
give a syntax error but the desired results will not be achieved.






In our case, we use MSSQLNT as the database name. However, you may use other database
names such as ADABAS, AS400, DB2, DB6, INFORMIX, MSSQLNT, and ORACLE (depending on
what is applicable to your underlying database). The code pertaining to the HINT parameter of
one database may differ from that of another.
For example, if we have an ORACLE database, the same Z12 index may be specified in the
SELECT statement in the following manner:




Note the database name added along with the changed format for index specification.


How to find problem area in ABAP Programming?

Using transaction SAT to find problem areas

we will see the steps needed to investigate the execution of any report,
transaction, or operate module using the transaction SAT.


we will analyze the runtime of a standard program RIBELF00. We will execute the program on the order number (aufnr) and see the behavior.

For carrying out runtime analysis using transaction SAT, proceed as follows:

1. Call transaction SAT. The screen appears as shown:





2. Enter a suitable name for the variant (in our case, YPERF_VARIANT) and click the Create button below it. This will take you to the Variant creation screen.




3. On the Duration and Type tab, switch on Aggregation by choosing the Per Call
Position radio-button.

4. Then, click on the Statements tab. On the Statements tab, make sure Internal
Tables, the Read Operations checkbox and the Change Operations checkbox,
and the Open SQL checkbox under Database Access are checked.



5. Save your variant. Come back to the main screen of SAT.

6. Make sure that within Data Formatting on the initial screen of SAT, the checkbox for
Determine Names of Internal Tables is selected.

7. Next, enter the name of the program that is to be traced in the field provided (in our
case, it is RIBELF00). Then click the button.

8. The screen of the program appears as shown. We will enter an order number range
and execute the program.

9. Once the program output is generated, click on the Back key to come back to
program selection screen.

10. Click on the Back key once again to generate the evaluation results.


We carried out the execution of the program through the transaction SAT and the evaluation
results were generated. On the left are the Trace Results (in tree form) listing the statements/
events with the most runtime. These are like a summary report of the entire measurement
of the program. They are listed in descending order of the Net time in microseconds and the
percentage of the total time. For example, in our case, the OPEN CURSOR event takes 68
percent of the total runtime of the program.



Selecting the Hit List tab will show the top time consumer components of the program. In this
example, the access of database tables AFRU and VBAK takes most of the time.



Double-clicking any item in the Trace Results window on the left-hand side will display (in the
Hit List area on the right-hand pane) details of contained items along with execution time
of each item. From the Hit List window, double-clicking a particular item will take us to the
relevant line in the program code. For example, when we double-click the Open Cursor VBAK
line, it will take us to the corresponding program code.



We have carried out analysis with Aggregation switched on. The switching on of Aggregation
shows one single entry for a multiple calls of a particular line of code. Because of this, the
results are less detailed and easier to read, since the hit list and the call hierarchy in the
results are much more simplified.
Also within the results, by default, the names of the internal table used are not shown.
In order for the internal table names to appear in the evaluation result, the Determine
Names checkbox of Internal tables indicator is checked.
As a general recommendation, the runtime analysis should be carried out several times for
best results. The reason being that the DB-measurement time could be dependent on a
variety of factors, such as system load, network performance, and so on.

How to use the performance using Hashed Table?


Hashed table for single read access


Hashed table is used to access the internal table in a constant time. It increase the performance for reading the internal table.


We define select-options for taking input of personnel number.




Next, we define a type ty_payroll based on payroll infotype fields pernr and abrdt. 

A structure and a hashed table based on this type are also defined. 

The hashed table has a unique key pernr.



Similarly, an address type ty_address is defined, along with a structure and internal table.


We then write two select statements. 

The first reads PA0003 for all personnel numbers specified and the date of last payroll run (abrdt). 

The second select statement is used to read all the stras addresses corresponding to permanent
address type (subty = 1) valid at the system date.




Finally, a loop is run on the addresses internal table it_address. 

Within the loop, the read table statement is used for reading the payroll table it_payroll
(the hashed table) for each of the personnel number processed. 

Within the loop, the personnel number, abrdt date, and the address field stras are displayed.

We have used field symbols instead of work areas, in conjunction with loop and read statements for better performance.









How to create Shared Memory in SAP?

Creating Shared Memory

Call transaction SE24; enter a suitable name to your root class, as shown in the following screenshot. 

On the Properties tab, we need to make sure that the Shared-Memory checkbox is switched on.




We have named it ZCL_MY_ROOT. We will then define two Instance Attributes,

NUMBER and NAME, having private visibility, as shown in the following screenshot:



Two suitable methods, SET_DATA and GET_DATA, are also added to the class. 

The SET_DATA method contains code that imports number and name and assigns to the attributes 

NUMBER and NAME of the class. The GET_DATA method does just the opposite, that is, it 

exports the NUMBER and NAME attribute for a given shared memory object.

Next, the shared memory area should be created. 

This is done via transaction SHMA.

Enter a suitable name and click on the Create button. 

We have typed the name ZCL_MY_EMP_AREA. On the screen that appears, enter the description 

of the area. Also, enter the name of the root class created earlier in the Root Class field. 

You may leave the Client-Specific Area checkbox unchecked as it is not required for our recipe. 

Now, save your entries. Refer to the following screenshot:




This will also generate an area class by entering the same name ZCL_MY_EMP_AREA.



This area class will contain the necessary methods used for reading, changing, and creating the area, 

such as ATTACH_FOR_UPDATE, ATTACH_FOR_READ, and ATTACH_FOR_WRITE.


For creating the set of code that writes object's contents to the shared memory, follow these steps:

1. Two object references my_handle and my_root are defined, one for area class and the other for root class.

2. The static method attach_for_write of the area class zcl_my_emp_area is called.

3. The CREATE OBJECT with the area handle, my_handle must then be called.

4. The root and the created area instance must be linked using the set_root method of the handle.

5. The set_data method is called with the relevant number and name.

6. The detach_commit method of the area class is then called.



The read program is somewhat similar. However, instead of the attach_for_write method used 

earlier, we will use attach_for_read. 

The same instance name is passed and the handle is received. 

The method imposes a read lock on the area instance. Then, the get_data method of the root object is 

called using the area handle, my_handle. This returns the employee name and number stored earlier 

into the variables name and number respectively.

Finally, the detach method is called and the read lock is released.




While creating the shared memory area, if we select the Transactional Area checkbox, the area 

becomes transactional. In this case, the modifications to the area instance versions are not active 

immediately after the call of detach_commit method. Rather, they become active when the next 

database commit is executed.