Sunday, November 18, 2018

Send mail with pdf attachment




REPORT  sendmailwithpdf.

* Internal Table declarations
DATA: I_OTF TYPE ITCOO OCCURS WITH HEADER LINE,
      I_TLINE TYPE TABLE OF TLINE WITH HEADER LINE,
      I_RECEIVERS TYPE TABLE OF SOMLRECI1 WITH HEADER LINE,
      I_RECORD LIKE SOLISTI1 OCCURS WITH HEADER LINE,
* Objects to send mail.
      I_OBJPACK LIKE SOPCKLSTI1 OCCURS WITH HEADER LINE,
      I_OBJTXT LIKE SOLISTI1 OCCURS WITH HEADER LINE,
      I_OBJBIN LIKE SOLISTI1 OCCURS WITH HEADER LINE,
      I_RECLIST LIKE SOMLRECI1 OCCURS WITH HEADER LINE,
* Work Area declarations
      WA_OBJHEAD TYPE SOLI_TAB,
      W_CTRLOP TYPE SSFCTRLOP,
      W_COMPOP TYPE SSFCOMPOP,
      W_RETURN TYPE SSFCRESCL,
      WA_DOC_CHNG TYPE SODOCCHGI1,
      W_DATA TYPE SODOCCHGI1,
      WA_BUFFER TYPE STRING, "To convert from 132 to 255
* Variables declarations
      V_FORM_NAME TYPE RS38L_FNAM,
      V_LEN_IN LIKE SOOD-OBJLEN,
      V_LEN_OUT LIKE SOOD-OBJLEN,
      V_LEN_OUTN TYPE I,
      V_LINES_TXT TYPE I,
      V_LINES_BIN TYPE I.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    FORMNAME           = 'ZTEST'
  IMPORTING
    FM_NAME            = V_FORM_NAME
  EXCEPTIONS
    NO_FORM            = 1
    NO_FUNCTION_MODULE = 2
    OTHERS             3.
IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


W_CTRLOP-GETOTF = 'X'.
W_CTRLOP-NO_DIALOG = 'X'.
W_COMPOP-TDNOPREV = 'X'.


CALL FUNCTION V_FORM_NAME
  EXPORTING
    CONTROL_PARAMETERS = W_CTRLOP
    OUTPUT_OPTIONS     = W_COMPOP
    USER_SETTINGS      = 'X'
  IMPORTING
    JOB_OUTPUT_INFO    = W_RETURN
  EXCEPTIONS
    FORMATTING_ERROR   = 1
    INTERNAL_ERROR     = 2
    SEND_ERROR         = 3
    USER_CANCELED      = 4
    OTHERS             5.
IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
I_OTF[] = W_RETURN-OTFDATA[].
CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
    FORMAT                'PDF'
    MAX_LINEWIDTH         = 132
  IMPORTING
    BIN_FILESIZE          = V_LEN_IN
  TABLES
    OTF                   = I_OTF
    LINES                 = I_TLINE
  EXCEPTIONS
    ERR_MAX_LINEWIDTH     = 1
    ERR_FORMAT            = 2
    ERR_CONV_NOT_POSSIBLE = 3
    OTHERS                4.
IF SY-SUBRC <> 0.
ENDIF.
LOOP AT I_TLINE.
  TRANSLATE I_TLINE USING '~'.
  CONCATENATE WA_BUFFER I_TLINE INTO WA_BUFFER.
ENDLOOP.
TRANSLATE WA_BUFFER USING '~'.
DO.
  I_RECORD = WA_BUFFER.
  APPEND I_RECORD.
  SHIFT WA_BUFFER LEFT BY 255 PLACES.
  IF WA_BUFFER IS INITIAL.
    EXIT.
  ENDIF.
ENDDO.
* Attachment
REFRESH: I_RECLIST,
          I_OBJTXT,
          I_OBJBIN,
          I_OBJPACK.
CLEAR WA_OBJHEAD.
I_OBJBIN[] = I_RECORD[].

* Create Message Body Title and Description
I_OBJTXT = 'test with pdf-Attachment!'.
APPEND I_OBJTXT.
DESCRIBE TABLE I_OBJTXT LINES V_LINES_TXT.
READ TABLE I_OBJTXT INDEX V_LINES_TXT.
WA_DOC_CHNG-OBJ_NAME = 'smartform'.
WA_DOC_CHNG-EXPIRY_DAT = SY-DATUM + 10.
WA_DOC_CHNG-OBJ_DESCR = 'Mail send with PDf attchedment'.
WA_DOC_CHNG-SENSITIVTY = 'F'.
WA_DOC_CHNG-DOC_SIZE = V_LINES_TXT * 255.
* Main Text
CLEAR I_OBJPACK-TRANSF_BIN.
I_OBJPACK-HEAD_START = 1.
I_OBJPACK-HEAD_NUM = 0.
I_OBJPACK-BODY_START = 1.
I_OBJPACK-BODY_NUM = V_LINES_TXT.
I_OBJPACK-DOC_TYPE = 'RAW'.
APPEND I_OBJPACK.


* Attachment (pdf-Attachment)
I_OBJPACK-TRANSF_BIN = 'X'.
I_OBJPACK-HEAD_START = 1.
I_OBJPACK-HEAD_NUM = 0.
I_OBJPACK-BODY_START = 1.
DESCRIBE TABLE I_OBJBIN LINES V_LINES_BIN.
READ TABLE I_OBJBIN INDEX V_LINES_BIN.
I_OBJPACK-DOC_SIZE = V_LINES_BIN * 255 .
I_OBJPACK-BODY_NUM = V_LINES_BIN.
I_OBJPACK-DOC_TYPE = 'PDF'.
I_OBJPACK-OBJ_NAME = 'smart'.
I_OBJPACK-OBJ_DESCR = 'attached pdf'.
APPEND I_OBJPACK.

" email receiver list

CLEAR I_RECLIST.
I_RECLIST-RECEIVER = 'salauddin@gmail.com'.
I_RECLIST-REC_TYPE = 'U'.
APPEND I_RECLIST.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    DOCUMENT_DATA              = WA_DOC_CHNG
    PUT_IN_OUTBOX              = 'X'
    COMMIT_WORK                = 'X'
  TABLES
    PACKING_LIST               = I_OBJPACK
    OBJECT_HEADER              = WA_OBJHEAD
    CONTENTS_BIN               = I_OBJBIN
    CONTENTS_TXT               = I_OBJTXT
    RECEIVERS                  = I_RECLIST
  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.
  WRITE:/ 'Error When Sending the File', SY-SUBRC.
ELSE.
  WRITE:/ 'Mail sent'.
ENDIF.

Sunday, July 29, 2018

Messages in ABAP


Messages are usually used to tell the user what is going on. The following types of messages are available in ABAP.
A
Termination
The message appears in a dialog box, and the program terminates. When the user has confirmed the message, control returns to the next-highest area menu.
E
Error
Depending on the program context, an error dialog appears or the program terminates.
I
Status
The message appears in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement.
S
Error
The program continues normally after the MESSAGE statement, and the message is displayed in the status bar of the next screen.
W
Warning
Depending on the program context, an error dialog appears or the program terminates.
X
Exit
No message is displayed, and the program terminates with a short dump. Program terminations with a short dump normally only occur when a runtime error occurs.

The syntax for issuing a message is as follows.
MESSAGE <message> TYPE <message type>.

We can issue a status message as follows. Status message will be displayed in the status bar. After the message is displayed the program continues after the MESSAGE statement.
MESSAGE 'This is a status message' TYPE 'S'.

Information message will be displayed in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement.
MESSAGE 'This is an information message' TYPE 'I'.

Error message in report programs will be displayed in the status bar and when the user press enter, the program terminates.
MESSAGE 'This is an error message' TYPE 'E'.

Warning message behaves similar to error message in report programs.

Exit Message – No message is displayed, and the program terminates with a short dump. Short dumps can be viewed in t-code ST22.
MESSAGE 'This produces short dump' TYPE 'X'.

Termination Message appears in a dialog box, and the program terminates. When the user has confirmed the message, control returns to the next-highest area menu.
MESSAGE 'This is termination message' TYPE 'A'.
Instead of hardcode the message text in the program we can maintain the message text in text symbols. In order to maintain text symbols use the menu path Goto->Text Elements->Text Symbols in ABAP editor.
In the text symbols screen we can maintain the messages with a 3 character identifier.
Then use the following message statement.
MESSAGE text-001 TYPE 'I'.

Tuesday, April 17, 2018

Adding Logo to SAP ALV Report

UPLOADING LOGO

To add a logo to ALV header, we need to have a logo uploaded in to SAP system. The following steps will show you how to upload a customise logo.
First, go to transaction Business Document Navigator (OAOR).
Enter the Class name, Class type, and a name for the logo in Object Key. Then Execute (F8).

On the left-bottom panel, expand Standard Doc. Types and double click on Screen.

On the pop-up window, select the logo file that we want to upload. Then click Open.
Enter description for the logo and press continue. Logo will be saved to system.

ABAP Program
REPORT  zaddlogo.

TYPE-POOLSslis" SLIS contains all the ALV data types

*&———————————————————————*
*& Data Declaration
*&———————————————————————*

DATAlt_sflight TYPE TABLE OF sflight.
DATAg_repid   TYPE sy-repid.
*ALV Header declarations
DATAlt_header        TYPE slis_t_listheader,
              ls_header       TYPE slis_listheader,
              lt_line             LIKE ls_header-info,
              lv_lines          TYPE i,
              lv_linesc(10TYPE c.

*&———————————————————————*
*& START-OF-SELECTION
*&———————————————————————*
START-OF-SELECTION.

g_repid sy-repid.
*&—– Fetch data from the database —–*
SELECT *
      FROM sflight
      INTO TABLE lt_sflight.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
            i_callback_program g_repid
            i_callback_top_of_page 'TOP-OF-PAGE'
            i_structure_name 'SFLIGHT'
      TABLES
            t_outtab lt_sflight.

*———————————————————–*
* Form TOP-OF-PAGE
* Generating Report Header
*———————————————————–*
FORM top-of-page.

*&—– Alv report header —–*
* Title
ls_header-typ 'H'.
ls_header-info 'SFLIGHT Table Report'.
APPEND ls_header TO lt_header.
CLEAR ls_header.

* Date
ls_header-typ 'S'.
ls_header-key 'Date: '.
CONCATENATE sy-datum+6(2'.'
                               sy-datum+4(2'.'
                              sy-datum(4)

                  INTO ls_header-info"todays date
APPEND ls_header TO lt_header.
CLEARls_header.

* Total No. of Records Selected
DESCRIBE TABLE lt_sflight LINES lv_lines.
lv_linesc lv_lines.
CONCATENATE 'Total No. of Records Selected: ' lv_linesc
            INTO lt_line SEPARATED BY space.
ls_header-typ 'A'.
ls_header-info lt_line.
APPEND ls_header TO lt_header.
CLEARls_headerlt_line.
*&—– Pass data and field catalog to ALV function module —–*

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
      EXPORTING
            it_list_commentary lt_header

            i_logo                          'TESTLOGO'.

ENDFORM"top-of-page

Output:



show radio button in a single line


Program:

REPORT  zradiobtn.

*//Radio button design
SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME TITLE TEXT-s01.
*//Radio buttons design
  selection-SCREEN BEGIN OF LINE.
    PARAMETERSp_rad1 RADIOBUTTON GROUP grp1.
    selection-SCREEN COMMENT 10(15FOR FIELD p_rad1.
    PARAMETERSp_rad2 RADIOBUTTON GROUP grp1.
    selection-SCREEN COMMENT 30(15FOR FIELD p_rad2.
    PARAMETERSp_rad3 RADIOBUTTON GROUP grp1.
    selection-SCREEN COMMENT 50(15FOR FIELD p_rad3.
    PARAMETERSp_rad4 RADIOBUTTON GROUP grp1.
    selection-SCREEN COMMENT 70(15for FIELD p_rad4.
  selection-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK blk2.


Change the radio button name in teh selection texts





execute & check the output--