Thursday, September 11, 2008

Insert separator in a string after specified position

By Joyjit Ghosh,
Kolkata, India.


Code:

*&---------------------------------------------------------------------*
*& Report Z_DEMO_JG
*&
*&---------------------------------------------------------------------*
*&
*& Insert separator in a string after specified position
*&---------------------------------------------------------------------*

REPORT z_demo_jg.

* Data declaration

DATA: l_char1(3) TYPE c.

DATA: l_char TYPE string.

DATA: l_pos TYPE i.

DATA: l_len TYPE i.

DATA: l_len1 TYPE i.

* Selection screen

PARAMETERS: l_srt(255) TYPE c DEFAULT `12344566` obligatory, " String

l_spch(1) type c DEFAULT `,` obligatory, " Separator

l_no type int3 DEFAULT 1 obligatory. " No of places

* Length of the string

l_len = STRLEN( l_srt ).

* Divide the length by 3

l_len1 = l_len DIV l_no.

l_pos = 0.

DO l_len1 TIMES.

* Storing next l_no chars

l_char1 = l_srt+l_pos(l_no).

* Add separator with it

CONCATENATE l_char l_char1 l_spch INTO l_char.

l_pos = l_pos + l_no.

ENDDO.

* Check remaining length

l_len1 = l_len - l_pos.

IF l_len1 > 0.

* Storing remaining characters

l_char1 = l_srt+l_pos(l_len1).

CONCATENATE l_char l_char1 INTO l_char.

ENDIF.

CONDENSE l_char NO-GAPS.

* Display original string

write /.

WRITE : 'Original String:',

l_srt.

l_srt = l_char.

* Display modified string

write /.

WRITE : 'Modified String :',

l_srt.

.

Selection screen:


Output:


Creating dynamic variant using table TVARV

By Joyjit Ghosh,
Kolkata, India.


Let us say for the given selection screen we need to create a dynamic variant.

Step 1. First maintain a variable with values in table TVARV. This can be done from transaction STVARV. Later this variable needs to be assigned to the variant.


Step 2. Next create the dynamic variant by pressing the SAVE button on the selection screen.

In the next screen (ABAP: Save as Variant) enter Variant name, description, set the ‘Selection variable’(L) radio button and press ‘Selection variable’ push button.


In the next screen check that green traffic light is on under column T (T: Table variable from TVARV). Then click on button.

Now from the popup select the variable name created in Step1 and save the variant.


Step 3. Select the variant from Variant catalog.


It will populate ‘Airline’ select-option with the values maintained in TVARV table against variable ZAIRLINE_CODE.

Global Macro

By Joyjit Ghosh,
Kolkata
, India
.

Most of the time we define macro in our program by DEFINE … END-OF-DEFINITION statement.

Ex:

DEFINE macro_name

Statement……

Statement …..

………………..

END-OF-DEFINITION.

And this macro can be called in the following way

macro_name par1 par2 …par9. “par1… par9 = Parameters, separated by spaces

This above definition is local to the program where it is defined i.e. we cannot call this macro from another program. But we can create global macro that can be called by any program.

Global macro can be created by maintaining entries in table TRMAC.

Most popular example of standard global (system) macro is BREAK which is defined in table TRMAC as shown in the screen shot below.

From any program we can call this macro as:

BREAK user_id. “ user_id = placeholder for userid

Similarly we can maintain our own global macro in TRMAC as shown below:

Add new entry to TRMAC

Create custom macro

Call this global macro from a report

Output:




Dynamic selection screen with list box

By Joyjit Ghosh,
Kolkata, India.

Code:

*&---------------------------------------------------------------------*
*& Report Z_DYNAMIC_SCREEN
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT z_dynamic_screen.

TYPE-POOLS: vrm.

DATA: name TYPE vrm_id, list TYPE vrm_values, value LIKE LINE OF list.

TABLES: sscrfields.

* Selection screen

* List box
PARAMETERS: ps_parm AS LISTBOX VISIBLE LENGTH 10

USER-COMMAND abc.

* Radio buttons
PARAMETERS: rb1 RADIOBUTTON GROUP ab MODIF ID bl2,

rb2 RADIOBUTTON GROUP ab MODIF ID bl2,

rb3 RADIOBUTTON GROUP ab MODIF ID bl3.


INITIALIZATION.

* Populate list box values

name = 'PS_PARM'.

value-key = '1'. value-text = 'Line 1'. APPEND value TO list.

value-key = '2'. value-text = 'Line 2'. APPEND value TO list.


AT SELECTION-SCREEN OUTPUT.

* Set list box with value

CALL FUNCTION 'VRM_SET_VALUES'

EXPORTING

id = name

values = list.


* Control the display of screen components

LOOP AT SCREEN.

IF ps_parm = 1.

IF screen-name = 'RB1' OR screen-name = 'RB2' .

screen-invisible = 0.

ELSEIF screen-name = 'RB3'.

screen-invisible = 1.

ENDIF.

MODIFY SCREEN.

ELSEIF ps_parm = 2.

IF screen-name = 'RB1' OR screen-name = 'RB2' .

screen-invisible = 1.

ELSEIF screen-name = 'RB3'.

screen-invisible = 0.

ENDIF.

MODIFY SCREEN.

ELSEIF ps_parm = space.

rb1 = 'X'.

clear: rb2,rb3.

IF screen-name = 'RB1' OR screen-name = 'RB2' OR

screen-name = 'RB3'.

screen-invisible = 0.

MODIFY SCREEN.

ENDIF.

ENDIF.

ENDLOOP.

AT SELECTION-SCREEN.

IF sscrfields-ucomm = 'ABC'.

ENDIF.

START-OF-SELECTION.

WRITE: / 'Parameter:', ps_parm.


Selection screen:

Thursday, July 31, 2008

Simple ALV GRID (Full screen)

By Joyjit Ghosh,
Kolkata, India.

REPORT z_alv_object_1.

DATA: i_mara TYPE STANDARD TABLE OF mara.
DATA: dref TYPE REF TO cl_salv_table.


*&---------------------------------------------------------------------*
*& Start-of-selection event
*&---------------------------------------------------------------------*
START-OF-SELECTION.

* Fetch data from database
SELECT * FROM mara
INTO TABLE i_mara
UP TO 100 ROWS.

CHECK sy-subrc = 0.

* Get the ALV instance
TRY.
CALL METHOD cl_salv_table=>factory
* EXPORTING
* list_display = IF_SALV_C_BOOL_SAP=>TRUE
* r_container =
* container_name =
IMPORTING
r_salv_table = dref
CHANGING
t_table = i_mara
.
CATCH cx_salv_msg .
MESSAGE i000(z_zzz_ca_messages)
WITH 'Error in ALV processing'(001).

LEAVE LIST-PROCESSING.
ENDTRY.

IF dref IS BOUND.

* Display AlV report
CALL METHOD dref->display( ).

ENDIF.

Wednesday, July 30, 2008

Execute report from sap mail

By Joyjit Ghosh,
Kolkata, India.

*&---------------------------------------------------------------------*
*& Report Z_DEMO_MAIL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT z_demo_mail.


*&---------------------------------------------------------------------*
*& Global data declaration
*&---------------------------------------------------------------------*

DATA:
w_document_data LIKE sodocchgi1.

DATA:
i_object_header TYPE STANDARD TABLE OF solisti1,
w_object_header TYPE solisti1,

i_object_content TYPE STANDARD TABLE OF solisti1,
w_object_content TYPE solisti1,

i_contents_hex TYPE STANDARD TABLE OF solix,
w_contents_hex TYPE solix,

i_object_para TYPE STANDARD TABLE OF soparai1,
w_object_para TYPE soparai1,

i_object_parb TYPE STANDARD TABLE OF soparbi1,
w_object_parb TYPE soparbi1,

i_receivers TYPE STANDARD TABLE OF somlreci1,
w_receivers TYPE somlreci1.

*&---------------------------------------------------------------------*
*& Selection screen
*&---------------------------------------------------------------------*
PARAMETER : uname TYPE syuname DEFAULT sy-uname.

*&---------------------------------------------------------------------*
*& Start-of-selection event
*&---------------------------------------------------------------------*
START-OF-SELECTION.

* Populate receiver info
clear: w_receivers.
w_receivers-receiver = uname.
w_receivers-rec_type = 'B'.
append w_receivers to i_receivers.

* Populate header data
w_document_data-obj_name = 'Your action required'.
w_document_data-obj_descr = 'Pl. press execute button'.
w_document_data-sensitivty = 'O'.
w_document_data-proc_type = 'R'.
w_document_data-proc_name = 'RSINCL00'.


* Populate mail content
clear: w_object_content.
concatenate 'Please execute report: '
w_document_data-proc_name
into w_object_content-line separated by space.
append w_object_content to i_object_content.


CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data = w_document_data
DOCUMENT_TYPE = 'RAW'
* PUT_IN_OUTBOX = ' '
* COMMIT_WORK = ' '
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =
tables
* OBJECT_HEADER =
OBJECT_CONTENT = i_object_content
* CONTENTS_HEX =
OBJECT_PARA = i_object_para
* OBJECT_PARB =
receivers = i_receivers
EXCEPTIONS
TOO_MANY_RECEIVERS = 1
DOCUMENT_NOT_SENT = 2
DOCUMENT_TYPE_NOT_EXIST = 3
OPERATION_NO_AUTHORIZATION = 4
PARAMETER_ERROR = 5
X_ERROR = 6
ENQUEUE_ERROR = 7
OTHERS = 8
.
IF sy-subrc = 0.
message s000(z_zzz_ca_messages)
with 'Mail send successfully'(001).
ENDIF.

Sunday, July 27, 2008

Changing font style in ALV

By Joyjit Ghosh,
Kolkata, India.

By using the Function Module REUSE_ALV_GRID_DISPLAY_LVC we can change the font style in ALV report.

Example:

*&---------------------------------------------------------------------*

*& Report Z_DEMO_ALV_JG *

*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------

REPORT z_demo_alv_jg .

* Include for all style values

INCLUDE <cl_alv_control>.

* Internal table for final output data

DATA: i_flight TYPE STANDARD TABLE OF sflight.

* Internal table for field catalog info

DATA: i_fields TYPE lvc_t_fcat.

* Field symbol for field catalog

FIELD-SYMBOLS: <wa_fields> TYPE lvc_s_fcat.

* Select data

SELECT * FROM sflight

INTO TABLE i_flight

UP TO 100 ROWS.

IF sy-subrc = 0.

* Get field catalog

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SFLIGHT'

CHANGING

ct_fieldcat = i_fields

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3

.

IF sy-subrc = 0.

* Changing the style of field catalog

LOOP AT i_fields ASSIGNING <wa_fields>.

IF sy-tabix > 4.

<wa_fields>-style = ALV_STYLE_FONT_ITALIC.

ELSE.

<wa_fields>-style = ALV_STYLE_FONT_BOLD.

ENDIF.

ENDLOOP.

ENDIF.

* Calling the FM to display ALV report

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'

EXPORTING

i_structure_name = 'SFLIGHT'

i_grid_title = 'Style demo'(001)

it_fieldcat_lvc = i_fields

TABLES

t_outtab = i_flight

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDIF.

Output: