Wednesday, May 20, 2020

Send Email for Multiple recipients

FUNCTION yfm_send_email.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_UNAME) TYPE  XUBNAME
*"     VALUE(IV_SUBJECT) TYPE  STRING
*"  EXPORTING
*"     VALUE(ET_MESSAGES) TYPE  BAPIRET2_T
*"  TABLES
*"      IT_EMAILS TYPE  YST_T_LISTEMAIL
*"      IT_TEXT TYPE  SOLI_TAB

Structure of YST_T_LISTEMIL - table type
UNAME 1 Types XUBNAME
TYPE 1 Types YMTYPE -(1 char with Default values 1 - From 2, To , 3- CC)
EMAIL 1 Types AD_SMTPADR
*"----------------------------------------------------------------------
  DATA : ls_emails        TYPE yst_s_listemail,
         lt_obj_head      TYPE TABLE OF solisti1,
         ls_obj_head      TYPE solisti1,
         ls_text          TYPE soli,
         lv_sent_to_all   TYPE os_boolean,
         gr_sender        TYPE REF TO cl_sapuser_bcs,
         lo_sender        TYPE REF TO if_sender_bcs,
         gr_send_request  TYPE REF TO cl_bcs,
         gr_bcs_exception TYPE REF TO cx_bcs,
         gr_recipient     TYPE REF TO if_recipient_bcs,
         gr_document      TYPE REF TO cl_document_bcs.


  CLASS cl_bcs DEFINITION LOAD.
  TRY.
      "Create send request
      gr_send_request = cl_bcs=>create_persistent( ).

      " Get the sender name
      CLEAR ls_emails.
      READ TABLE it_emails INTO ls_emails WITH KEY type = gc_typfm.
      IF sy-subrc EQ 0.
        "Email FROM...
*        gr_sender = cl_sapuser_bcs=>create( ls_emails-uname ).
        lo_sender = cl_cam_address_bcs=>create_internet_address(
                      i_address_string = ls_emails-email
                      i_address_name = ls_emails-email ).
        "Add sender to send request
        CALL METHOD gr_send_request->set_sender
          EXPORTING
            i_sender = lo_sender. "gr_sender.

      ENDIF.
      " CC Emails
      CLEAR ls_emails.
      LOOP AT it_emails INTO ls_emails WHERE type = gc_typcc.
        CLEAR gr_recipient.
        gr_recipient = cl_cam_address_bcs=>create_internet_address( ls_emails-email ).
        "Add recipient to send request
        CALL METHOD gr_send_request->add_recipient
          EXPORTING
            i_recipient = gr_recipient
            i_express   = 'X'.
        CLEAR : ls_emails.
      ENDLOOP.
      " To Emails
      CLEAR ls_emails.
      LOOP AT it_emails INTO ls_emails WHERE type = gc_typto.
        CLEAR gr_recipient.
        gr_recipient = cl_cam_address_bcs=>create_internet_address( ls_emails-email ).
        "Add recipient to send request
        CALL METHOD gr_send_request->add_recipient
          EXPORTING
            i_recipient = gr_recipient.
*          i_copy      = 'X'.
        CLEAR : ls_emails.
      ENDLOOP.

      REFRESH : lt_obj_head[].
      LOOP AT it_text INTO ls_text.
        CLEAR ls_obj_head.
        ls_obj_head-line = ls_text-line.
        APPEND ls_obj_head TO lt_obj_head .
        CLEAR ls_text.
      ENDLOOP.
* Message body and subject
*Create Email document
      gr_document = cl_document_bcs=>create_document( "create document
      i_type = 'HTM' "Type of document HTM, TXT etc
      i_text =  lt_obj_head "it_text "email body internal table
      i_subject = ' '). "email subject here p_sub input parameter

      CALL METHOD gr_send_request->set_message_subject
        EXPORTING
          ip_subject = iv_subject.
      "Add document to send request
      CALL METHOD gr_send_request->set_document( gr_document ).


      "Send email
      CLEAR lv_sent_to_all.
      CALL METHOD gr_send_request->send(
        EXPORTING
          i_with_error_screen = 'X'
        RECEIVING
          result              = lv_sent_to_all ).
      IF lv_sent_to_all = 'X'.
*        WRITE 'Email sent!'.
        REFRESH : et_messages[].
        PERFORM update_message TABLES et_messages
                                USING c_msgcls
                                      '057'
                                       c_s.
        "Commit to send email
        COMMIT WORK.
      ENDIF.

      "Exception handling
    CATCH cx_bcs INTO gr_bcs_exception.
      REFRESH : et_messages[].
      PERFORM update_message TABLES et_messages
                             USING c_msgcls
                                   '058'
                                    c_e.
*      gr_bcs_exception->error_type.
  ENDTRY.



ENDFUNCTION.

Create next number for 3 char and Numeric - Purchase group

FUNCTION YTEST_PGRP_CREATE
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(EMAIL) TYPE  AD_SMTPADR
*"     VALUE(CURR_ENTRY) TYPE  EKGRP
*"  EXPORTING
*"     VALUE(STATUS) TYPE  STRING
*"     VALUE(NEW_ENTRY) TYPE  EKGRP
*"----------------------------------------------------------------------
TABLES: T024.

DATA: it_t024 TYPE TABLE OF T024,
      wa_t024 TYPE T024,
      wa_user TYPE T024,
      lv_tmp TYPE EKORG,
      lv_count TYPE I value 0,
      lv_num TYPE I,
      lv_1 TYPE C,
      lv_2 TYPE C,
      lv_3 TYPE C,
      it_proc TYPE TABLE OF ZFI_VIM_APPR_CTR,
      wa_proc TYPE ZFI_VIM_APPR_CTR,
      lv_adrn TYPE ADR6-ADDRNUMBER,
      lv_user TYPE BNAME.

  SELECT SINGLE * FROM T024
    WHERE SMTP_ADDR EQ EMAIL.

    STATUS = SY-SUBRC.

  IF SY-SUBRC NE 0.
    lv_tmp = CURR_ENTRY.

    WHILE lv_tmp > 0.
      lv_2 = lv_tmp MOD 10.
      lv_tmp = lv_tmp / 10.
      IF lv_count EQ 0.
        lv_3 = lv_2.
        lv_count = lv_count + 1.
      ENDIF.
      lv_1 = lv_tmp.
    ENDWHILE.

    IF lv_3 NE '9'.
      lv_3 = lv_3 + 1.
    ELSE.
      lv_3 = 0.
      IF lv_2 NE 'Z'.
        SEARCH sy-abcde FOR lv_2.
        lv_num = sy-fdpos + 1.
        lv_2 = sy-abcde + lv_num.
      ELSE.
        lv_2 = 'A'.
        SEARCH sy-abcde FOR lv_1.
        lv_num = sy-fdpos + 1.
        lv_1 = sy-abcde + lv_num.
      ENDIF.
    ENDIF.
*    lv_tmp = lv_1 * 10 + lv_2.
*    lv_tmp = lv_tmp * 10 + lv_3.
    CONCATENATE lv_1 lv_2 lv_3 INTO lv_tmp.

    wa_t024-EKGRP = lv_tmp.
    wa_t024-SMTP_ADDR = EMAIL.
    INSERT INTO t024 VALUES wa_t024.
    COMMIT WORK AND WAIT.
    IF SY-SUBRC EQ 0.

    ENDIF.

    NEW_ENTRY = lv_tmp.
  ENDIF.

ENDFUNCTION.

Friday, March 15, 2019

Proper case Conversion in SAP

We have standard FM to convert Texts displayed  to either Upper case or Lower Case.
But to display the Text in Proper case ie., Capital letter after every word is acheived with the following code


PARAMETERS: p_text TYPE char255.
DATA: l_text_final TYPE char255,
      l_text1 TYPE char255,
      l_text2 TYPE char255.
SPLIT p_text AT space INTO TABLE DATA(itab).

LOOP AT itab INTO l_text1.
  TRANSLATE l_text1 TO LOWER CASE.
  TRANSLATE l_text1+0(1) TO UPPER CASE.
 FIND '-' IN l_text1.
  IF sy-subrc  = 0.
  SPLIT l_text1 AT '-' INTO TABLE DATA(itab1).
  clear l_text1.
LOOP AT itab1 INTO l_text2.
  TRANSLATE l_text2 TO LOWER CASE.
  TRANSLATE l_text2+0(1) TO UPPER CASE.
  IF l_text1 IS INITIAL.
    l_text1 = l_text2.
    ELSE.
  CONCATENATE l_text1 l_text2 INTO l_text1 SEPARATED BY '-'.
  ENDIF.
  ENDLOOP.
  ENDIF.
    IF l_text_final IS INITIAL.
    l_text_final = l_text1.
    ELSE.
  CONCATENATE l_text_final l_text1 INTO l_text_final SEPARATED BY space.
    ENDIF.
    ENDLOOP.
WRITE l_text_final.

Friday, July 6, 2018


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

*& Report ZEXITBADI_FIND*&

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

*& This Report Displays the possible Exits and BADIs for a given TCODE*&

*&---------------------------------------------------------------------*
REPORT ZEXITBADI_FIND NO STANDARD PAGE HEADING.

**************************************************************************

TABLES DECLARATION **************************************************************************
TABLES : TSTC, TADIR,TRDIR, TFDIR, ENLFDIR.

TABLES : TSTCT.

**************************************************************************

Types Declaration **************************************************************************
TYPES: BEGIN OF T_INC,

NAME(72),

END OF T_INC.
TYPES: BEGIN OF T_TADIR,

PGMID LIKE TADIR-PGMID, "Program ID in Requests and Tasks

OBJECT LIKE TADIR-OBJECT, "Object Name in Object Directory

OBJ_NAME LIKE TADIR-OBJ_NAME, "Object Name in Object Directory

END OF T_TADIR.

**************************************************************************

Type-pools Declaration **************************************************************************
TYPE-POOLS:ICON.

*************************************************************************

* Data Declarations*

************************************************************************
DATA : JTAB LIKE TADIR OCCURS 0 WITH HEADER LINE.

DATA : FIELD1(30).DATA: VALUE1(100).
DATA: PNAME LIKE TFDIR-PNAME,

TYPE(6),

INCLUDE LIKE TFDIR-INCLUDE,

T TYPE T_INC OCCURS 0 WITH HEADER LINE,

IC TYPE ICON_D,

V_DEVCLASS LIKE TADIR-DEVCLASS,

V_TEXT(60) TYPE C.

**************************************************************************

Selection Screen Declaration

**************************************************************************
PARAMETERS : P_TCODE LIKE TSTC-TCODE OBLIGATORY.
TYPES:BEGIN OF T_MOD,

NAME LIKE MODSAPT-NAME,

MODTEXT LIKE MODSAPT-MODTEXT,

TYP LIKE MODSAP-TYP,

MEMBER LIKE MODSAP-MEMBER,

END OF T_MOD.
**************************************************************************

Data Declaration **************************************************************************
DATA: I_MOD TYPE T_MOD OCCURS 0 WITH HEADER LINE,

I_BADI TYPE STANDARD TABLE OF T_TADIR WITH HEADER LINE ,

I_BDCDATA TYPE STANDARD TABLE OF BDCDATA,

WA_BDCDATA TYPE BDCDATA.
**************************************************************************

START-OF-SELECTION **************************************************************************
START-OF-SELECTION.
SELECT SINGLE * FROM TSTC WHERE TCODE EQ P_TCODE.
IF SY-SUBRC EQ 0.

SELECT SINGLE * FROM TADIR WHERE PGMID = 'R3TR'

AND OBJECT = 'PROG'

AND OBJ_NAME = TSTC-PGMNA.
MOVE : TADIR-DEVCLASS TO V_DEVCLASS.
IF SY-SUBRC NE 0.
SELECT SINGLE * FROM TRDIR WHERE NAME = TSTC-PGMNA.
IF TRDIR-SUBC EQ 'F'.

* Function Module name

SELECT SINGLE * FROM TFDIR WHERE PNAME = TSTC-PGMNA.
* Additional Attributes for Function Modules

SELECT SINGLE * FROM ENLFDIR

WHERE FUNCNAME = TFDIR-FUNCNAME.
* Directory of Repository Objects

SELECT SINGLE * FROM TADIR WHERE PGMID = 'R3TR'

AND OBJECT = 'FUGR'

AND OBJ_NAME EQ ENLFDIR-AREA.
* Get development class of the object

MOVE : TADIR-DEVCLASS TO V_DEVCLASS.

ENDIF.

ENDIF.
* Get the SAP enhancements of the development class of the object

SELECT * FROM TADIR INTO TABLE JTAB

WHERE PGMID = 'R3TR'

AND OBJECT = 'SMOD'

AND DEVCLASS = V_DEVCLASS.

* Get Transaction Code Texts.

SELECT SINGLE * FROM TSTCT WHERE SPRSL EQ SY-LANGU

AND TCODE EQ P_TCODE.
FORMAT COLOR COL_POSITIVE INTENSIFIED OFF.

WRITE:/(19) 'Transaction Code - ', 20(20) P_TCODE,

45(50) TSTCT-TTEXT.

SKIP.

IF NOT JTAB[] IS INITIAL.

LOOP AT JTAB.

* Getting the components of a exit.
SELECT A~NAME A~MODTEXT B~TYP B~MEMBER

INTO CORRESPONDING FIELDS OF TABLE I_MOD

FROM MODSAPT AS A

INNER JOIN MODSAP AS B ON B~NAME = A~NAME

WHERE A~SPRSL = SY-LANGU

AND A~NAME = JTAB-OBJ_NAME

AND B~TYP = ''.

LOOP AT I_MOD.

AT NEW MODTEXT.

FORMAT COLOR COL_GROUP INTENSIFIED OFF.

WRITE:/(150) SY-ULINE.

WRITE:/1 SY-VLINE,

2 JTAB-OBJ_NAME HOTSPOT ON,

21 SY-VLINE ,

22 I_MOD-MODTEXT,

92 SY-VLINE,

/1 SY-ULINE(150).

ENDAT.

CLEAR:PNAME,INCLUDE.

* Deriving the include name inorder to chech whether the exit is *

*implemented or not
SELECT PNAME INCLUDE INTO

(PNAME ,INCLUDE)

FROM TFDIR

WHERE FUNCNAME = I_MOD-MEMBER.

endselect.

PNAME = PNAME+3(37).

CONCATENATE PNAME 'U' INCLUDE INTO PNAME.

* Reads the function module

READ REPORT PNAME INTO T.
LOOP AT T WHERE NAME CS 'include'.

CLEAR:PNAME,field1.

SHIFT T-NAME LEFT DELETING LEADING SPACE.

SPLIT T-NAME AT ' ' INTO field1 PNAME.

SHIFT PNAME RIGHT DELETING TRAILING ''.

SHIFT PNAME RIGHT DELETING TRAILING '.'.

SHIFT PNAME LEFT DELETING LEADING SPACE.
ENDLOOP.
FORMAT COLOR COL_NORMAL INTENSIFIED OFF.

* read the include file

READ REPORT PNAME INTO T.

IF SY-SUBRC = 0.

IC = ICON_OKAY.

ELSE.

IC = ''.

ENDIF.

* For Exit Types

IF I_MOD-TYP = 'E'.

TYPE = 'Func.'.

ELSEIF I_MOD-TYP = 'S'.

TYPE = 'Screen'.

ELSEIF I_MOD-TYP = 'C'.

TYPE = 'Menu'.

ENDIF.

WRITE: /92 SY-VLINE,

93 IC,

98 SY-VLINE,

99 TYPE,

106 SY-VLINE,

107 I_MOD-MEMBER HOTSPOT ON,

150 SY-VLINE.

CLEAR:PNAME,IC,field1,TYPE.

ENDLOOP.
ENDLOOP.

WRITE:/(150) SY-ULINE.

DESCRIBE TABLE JTAB.

SKIP.

FORMAT COLOR COL_TOTAL INTENSIFIED ON.

WRITE:/ 'No of Exits:' , SY-TFILL.

ELSE.

FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.

WRITE:/(95) 'No User Exit exists'.

ENDIF.

**************************************************************************

For Badi's..

**************************************************************************
* Select BADI entries

SELECT PGMID OBJECT OBJ_NAME

FROM TADIR

INTO CORRESPONDING FIELDS OF TABLE I_BADI

WHERE ( PGMID = 'R3TR' )

AND ( OBJECT = 'SXSD' )

AND ( DEVCLASS = V_DEVCLASS ).
* Write BADI detail header

data:v_count type i.

DESCRIBE TABLE I_BADI LINES V_COUNT.

IF V_COUNT > 0.
WRITE: /(95) SY-ULINE.

FORMAT COLOR COL_HEADING INTENSIFIED ON.

WRITE: /1 SY-VLINE,

2 'BADI',

31 SY-VLINE,

32 'Description',

95 sy-vline.

WRITE: /(95) SY-ULINE.

* Write BADI results

LOOP AT I_BADI.

CLEAR V_TEXT.

SELECT SINGLE TEXT

FROM SXS_ATTRT

INTO V_TEXT

WHERE ( SPRSL = SY-LANGU )

AND ( EXIT_NAME = I_BADI-OBJ_NAME ).


FORMAT COLOR COL_NORMAL INTENSIFIED OFF.

WRITE: /1 SY-VLINE,

2 I_BADI-OBJ_NAME HOTSPOT ON,

31 SY-VLINE,

32 V_TEXT,

95 SY-VLINE.

ENDLOOP.
FORMAT COLOR COL_TOTAL INTENSIFIED ON.

WRITE: /(95) SY-ULINE.

WRITE: /1 SY-VLINE,

2 'Total BADI''s:',

31 SY-VLINE,

32 V_COUNT,

95 SY-VLINE.

WRITE: /(95) SY-ULINE.


ELSE.

WRITE: /(95) 'No BADI''s exist.'.

ENDIF.

ELSE.

FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.

WRITE:/(95) 'Transaction Code Does Not Exist'. ENDIF.

**************************************************************************

AT LINE-SELECTION **************************************************************************
AT LINE-SELECTION.

GET CURSOR FIELD FIELD1 VALUE VALUE1.

IF FIELD1(4) EQ 'JTAB'.

* Jump to transaction SMOD
SET PARAMETER ID 'MON' FIELD SY-LISEL+1(10).

CALL TRANSACTION 'SMOD' AND SKIP FIRST SCREEN.

ELSEIF FIELD1(5) EQ 'I_MOD'.

* Viewing the function

SUBMIT WB_MNGR_START_FROM_TOOL_ACCESS AND RETURN WITH ACTION = 'DISPLAY'

* WITH OBJDATA = p_object_data WITH OBJ_TYPE = 'FF' WITH OBJ_NAME = VALUE1 WITH ENCL_OBJ = '' WITH POSITION = '' WITH INCLUDE = '' WITH VERSION = '' WITH TOOL = 'CL_FUNCTION_BUILDER' WITH NEWWNDOW = '' WITH OBJLIST = ''.* Direct BADI to transaction SE18
ELSEIF FIELD1(6) EQ 'I_BADI'. SET PARAMETER ID 'EXN' FIELD SY-LISEL+1(29). CALL TRANSACTION 'SE18' AND SKIP FIRST SCREEN. ENDIF.************************************************************************** TOP-OF-PAGE **************************************************************************
TOP-OF-PAGE.
WRITE:/(150) SY-ULINE. FORMAT COLOR COL_HEADING INTENSIFIED ON. WRITE:/1 SY-VLINE, 2 'Exit Name', 21 SY-VLINE , 22 'Description', 92 SY-VLINE, 93 'Imp.', 98 SY-VLINE, 99 'Type', 106 SY-VLINE, 107 'Function Name', 150 SY-VLINE, /1 SY-ULINE.

SYSTEM_CALLSTACK functionality

  DATA  : gt_abap_stack TYPE abap_callstack,  " Call Stack
          gt_sys_stack TYPE sys_callst.

** get the call stack to get the details of the Program and Even triggered
    CALL FUNCTION 'SYSTEM_CALLSTACK'
*     EXPORTING
*       MAX_LEVEL          = 0
     IMPORTING
       CALLSTACK          =   gt_abap_stack
       ET_CALLSTACK       =   gt_sys_stack.
*** Get the details of the HU details
    READ TABLE gt_abap_stack TRANSPORTING NO FIELDS
        WITH KEY mainprogram = dc_program "Your Program name
                 INCLUDE     = dc_program "Include Name
                 blocktype   = 'EVENT'
                 BLOCKNAME   = 'START-OF-SELECTION'.   "Event Block Name
      IF sy-subrc EQ 0.

your logic  --- to fetch the details of other program or to check the main program details by making 
IMPORT memory data details.


  ENDIF.

NOTE:

Export Memory should be written in the Called Prgram(Main Program)

Writing Data to a Spool in ABAP program

NEW-PAGE PRINT ON DESTINATION c_locl
                        IMMEDIATELY ' '

                        KEEP IN SPOOL c_x
                        LAYOUT c_parimp
                        NO DIALOG.


    PERFORM f410_write_data.


NEW-PAGE PRINT OFF.

Parallel Cursor Example - Which improves performance and avoids Nested Loops


sort: lt_vbpa by kunnr,  "Sorting by key is very important
      lt_kna1 by kunnr.  "Same key which is used for where condition is used here
loop at lt_vbpa into wa_vbpa.
  read lt_kna1 into wa_kna1     " This sets the sy-tabix
       with key kunnr = wa_vbpa-kunnr
       binary search.
  if sy-subrc = 0.              "Does not enter the inner loop
    v_kna1_index = sy-tabix.
    loop at lt_kna1 into wa_kna1 from v_kna1_index. "Avoiding Where clause
      if wa_kna1-kunnr <> wa_vbpa-kunnr.  "This checks whether to exit out of loop
        exit.
      endif.

****** Your Actual logic within inner loop ******

   endloop. "KNA1 Loop
  endif.
endloop.  " VBPA Loop