Skip to Content

This document is intended to explain the way of sending email notification to the initiator and the approver when a PR is released in SAP MM from transaction code ME54 or ME54N or ME55.

Development steps :

1. Search BADI which will be triggered , when ever a PR is released and finally saved through any of the above transaction code.

    for this purpose you put a break point line no 28 inside method GET_INSTANCE of  class CL_EXITHANDLER,

    Once the break point is set , try to release and save the PR from ME54 or ME54N or ME55, and note the BADI name.

2. With this method i found the BADi defination ME_REQ_POSTED (Purchase Requisition Posted), which will be triggered each time a PR is released and 

    saved.

3. Create a BADI IMplementation for ME_REQ_POSTED and then go to method POSTED in the interface to write the logic as per the requirement (logic to

    get the approver and initiator and other details), once we got the approver ids , then we can use FM

    SO_NEW_DOCUMENT_SEND_API1 to send the mail with desired body.

   

    Below code can be refered if required :

METHOD if_ex_me_req_posted~posted.

*&——————————————————————–*

*& Module Pool       SAPMZEHS_LOG_E                                   *

*&                                                                    *

*&——————————————————————–*

*&——————————————————————–*

*&——————————————————————–*

*&  Program Name    : SAPMZEHS_LOG_E                                  *

*&  Author          : Gejo john            – LNT Infotech             *

*&  Business Analyst: Jagan Mohan          – LNT Infotech             *

*&  Start Date      : 28-Jan-2013                                     *

*&  End Date        : 02-Feb-2013                                     *

*&  Project         : E9857 LNT Electromech Rollout                   *

*&  Ticket No       : E9857 – 16                                      *

*&  WIMS No         :                                                 *

*&  Purpose         : PR Mail Notification                            *                                               *

*&  Details         : E9857 – 16                                      *

*&  Category        : Enhacement                                      *

*&  Trans. Req. No. : Developement –                                  *

*&                    Correction   –  ECDK927877                      *

*&——————————————————————–*

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

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

****This BADI will trigger a mail to the approver when with PR*********

**Details, when ever a PR is released from either ME54 or ME54N********

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

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

**——–Type declaration ———————————————————————**

  TYPES: BEGIN OF                  ty_frgcx,

         frgco(5)             TYPE c,

  END OF                           ty_frgcx.

  TYPES: BEGIN OF                  ty_amount,

         banfn                TYPE banfn,

         menge                TYPE bamng,

         preis                TYPE bapre,

         waers                TYPE waers,

  END OF                           ty_amount.

  TYPES: BEGIN OF                  ty_cre_maidid,

         usrid_long           TYPE comm_id_long,

  END OF                           ty_cre_maidid.

  TYPES : BEGIN OF                 ty_fgrc0,

         frgc1                TYPE frgco,

         frgc2                TYPE frgco,

         frgc3                TYPE frgco,

         frgc4                TYPE frgco,

         frgc5                TYPE frgco,

         frgc6                TYPE frgco,

         frgc7                TYPE frgco,

         frgc8                TYPE frgco,

  END OF                           ty_fgrc0.

  TYPES: BEGIN OF                  ty_udatetime,

         udate                TYPE cduzeit,

         utime                TYPE cdtcode,

  END OF                           ty_udatetime.

  TYPES: BEGIN OF                  ty_udatetimeu,

         username             TYPE cdusername,

         udate                TYPE cduzeit,

         utime                TYPE cdtcode,

  END OF                           ty_udatetimeu.

  TYPES: BEGIN OF                  ty_preapp,

         username             TYPE cdusername,

  END OF                           ty_preapp.

  TYPES: BEGIN OF                  ty_changenr,

        changenr              TYPE cdchangenr,

  END OF                           ty_changenr.

**——–Data declaration ———————————————————————**

  DATA: g_v_frgkz             TYPE frgkz,          ” Release Indicator

        g_v_frgrl             TYPE frgrl,          ” Release Not Yet Completely Effected

        g_v_frgzu             TYPE frgzu,          ” Releasestatus

        g_v_banpr             TYPE banpr,          ” Requisition Processing State

        g_v_frggr             TYPE frggr,          ” Release group

        g_v_frgst             TYPE frgst,          ” Release Strategy in Purchase Requisition

        g_v_werks             TYPE ewerk,          ” Plant

        g_v_bukrs             TYPE bukrs,          ” company code

        g_v_frgcx(5)          TYPE c VALUE ‘FRGC’, ” Release code

        g_v_frgcx1(5)         TYPE c VALUE ‘FRGC’, ” Release code

        g_v_frgcx2(5)         TYPE c,              ” Release code

        g_v_frgcx3(5)         TYPE c,              ” Release code

        g_v_frgcx_val         TYPE frgco,          ” Release code

        g_v_ekgrp             TYPE ekgrp,          ” Purchasing Group

        g_v_appid             TYPE xubname,        ” User id

        g_v_appid1            TYPE xubname,        ” User id

        g_v_ernam             TYPE ernam,

        g_v_vorna             TYPE pad_vorna,

        g_v_nachn             TYPE pad_nachn,

        g_v_banfn             TYPE banfn,

        g_v_banfn_key         TYPE cdtabkey,

        g_v_changenr          TYPE cdchangenr,

        g_v_changenru         TYPE cdchangenr,

        g_v_relcode_me54(2)   TYPE c,

        g_v_relcode_me54n(2)  TYPE c,

        g_v_relcode(20)       TYPE c,

        g_v_fcode(20)         TYPE c,

        g_v_rel_code(2)       TYPE c,

        g_v_final_app(2)      TYPE c,

        g_v_recanind(10)      TYPE c,

        g_v_index             TYPE i,

        g_v_cntr              TYPE n,

        g_v_index1            TYPE n,

        g_v_index2            TYPE n,

        g_v_index3            TYPE n,

        g_v_index4            TYPE n,

        g_v_index5            TYPE n,

        g_v_x_pos             TYPE c,

        g_v_amount_net(11)    TYPE p DECIMALS 3,

        g_v_currency          TYPE waers,

        g_v_amount            TYPE bapre,

        g_v_amnt_c(15)        TYPE c,

        g_v_mob               TYPE ad_tlnmbr,

        g_v_cntno             TYPE ad_tlnmbr.

  DATA: wa_eban               LIKE LINE OF           im_eban,

        it_frgcx              TYPE STANDARD TABLE OF ty_frgcx,

        it_rel_code           TYPE STANDARD TABLE OF ty_frgcx,

        wa_frgcx              LIKE LINE OF           it_frgcx,

        it_fgrc0              TYPE STANDARD TABLE OF ty_fgrc0,

        it_fgrc01             TYPE STANDARD TABLE OF ty_fgrc0,

        wa_fgrc0              LIKE LINE OF           it_fgrc0,

        wa_fgrc01             LIKE LINE OF           it_fgrc0,

        it_zmm_aprover        TYPE STANDARD TABLE OF zmm_aprover,

        wa_zmm_aprover        LIKE LINE OF           it_zmm_aprover,

        it_amount             TYPE STANDARD TABLE OF ty_amount,

        wa_amount             LIKE LINE OF           it_amount,

        it_pa0105             TYPE STANDARD TABLE OF pa0105,

        wa_pa0105             LIKE LINE OF           it_pa0105,

        it_cre_maidid         TYPE STANDARD TABLE OF ty_cre_maidid,

        wa_cre_maidid         LIKE LINE OF           it_cre_maidid,

        str_address           TYPE                   bapiaddr3,

        str_address1          TYPE                   bapiaddr3,

        str_address2          TYPE                   bapiaddr3,

        str_address3          TYPE                   bapiaddr3,

        str_address4          TYPE                   bapiaddr3,

        it_return             TYPE STANDARD TABLE OF bapiret2,

        it_udatetime          TYPE STANDARD TABLE OF cdhdr,

        it_udatetimeu         TYPE STANDARD TABLE OF cdhdr,

        wa_udatetime          LIKE LINE OF           it_udatetime,

        wa_udatetimeu         LIKE LINE OF           it_udatetimeu,

        it_preapp             TYPE STANDARD TABLE OF ty_preapp,

        it_changenr           TYPE STANDARD TABLE OF ty_changenr,

        it_changenru          TYPE STANDARD TABLE OF ty_changenr,

        wa_preapp             LIKE LINE OF           it_preapp,

        it_addtel             TYPE STANDARD TABLE OF bapiadtel,

        wa_addtel             LIKE LINE OF           it_addtel.

**———- Data Declarations for mail sending———————————————————**

  DATA: lt_mailsubject        TYPE sodocchgi1,

        lt_mailrecipients     TYPE STANDARD TABLE OF somlrec90,

        wa_mailrecipients     LIKE LINE OF lt_mailrecipients,

        lt_mailtxt            TYPE STANDARD TABLE OF soli,

        wa_mailtxt            LIKE LINE OF lt_mailtxt.

**———–Field symbole declarations—————————————————————–**

  FIELD-SYMBOLS : <fs_fgrc0>  TYPE ANY,

                  <fs_fgrc01> TYPE ANY,

                  <fs_fgrc02> TYPE ANY.

**————–Start – 01 —clear all variables ——————————————————**

  CLEAR : g_v_banfn,

          g_v_banfn_key.

**————–End   – 01 —clear all variables ——————————————————**

**—————start -02 dont trigger the mail if the user press on cancel and then save in ME54N —-**

  IF sy-tcode EQ ‘ME54N’.

    IMPORT l_fcode TO g_v_fcode FROM MEMORY ID ‘FCD’.

    IF g_v_fcode = ‘MERESET’.

      EXIT.

    ENDIF.

    clear g_v_rel_code.

    IMPORT ls_data-rel_code TO g_v_rel_code FROM MEMORY ID ‘RCD’.

  ENDIF.

**—————End  -02 dont trigger the mail if the user press on cancel and then save—————**

**————–START  – 03   This logic should work for ME54 and ME54N only —————————**

  IF sy-tcode EQ ‘ME54’ OR sy-tcode EQ ‘ME54N’ OR sy-tcode EQ ‘ME55’.

  ELSE.

    EXIT.

  ENDIF.

**————–End   – 03   This logic should work for ME54 and ME54N only—————————–**

**————–STRAT 05 get the release code  ——————————————————–**

  IF sy-tcode = ‘ME54’.

     clear g_v_rel_code.

    IMPORT rm06b-frgab TO g_v_rel_code FROM MEMORY ID ‘CDR’.

  ENDIF.

  IF sy-tcode = ‘ME55’.

    clear g_v_rel_code.

    GET PARAMETER ID ‘BFC’ FIELD g_v_rel_code.

  ENDIF.

**————–End   05 get the release code  ——————————————————–**

  READ TABLE im_eban INTO wa_eban INDEX 1.            ” READ THE PR number

  IF sy-subrc = 0.                                    ” Get the PR details from EBAN table

    SELECT SINGLE frgkz frgrl frgzu banpr werks ernam ” Release indicator,subjected to release

        FROM eban

        INTO (g_v_frgkz,g_v_frgrl,g_v_frgzu,g_v_banpr,g_v_werks,g_v_ernam)

        WHERE banfn = wa_eban-banfn.

    IF sy-subrc = 0.

**—————-Start – 04 company code validatios—————————————————**

      SELECT SINGLE bukrs

        FROM t001k

        INTO g_v_bukrs

        WHERE bwkey = g_v_werks.

      IF g_v_bukrs NE ‘9000’.

        EXIT.

      ENDIF.

**—————-End  – 04 company code validatios—————————————————-**

**—————-Start – 06get the user detials of the initiator with user id g_v_ernam————–**

      IF g_v_ernam IS NOT INITIAL.

        CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

          EXPORTING

            username = g_v_ernam

          IMPORTING

            address  = str_address

          TABLES

            return   = it_return

            addtel   = it_addtel.

      ENDIF.

      CLEAR: g_v_mob, wa_addtel-telephone.            ” Initialtor mob and contact no

      READ TABLE it_addtel INTO wa_addtel INDEX 1.

      g_v_mob    = wa_addtel-telephone.

      CLEAR: g_v_cntno, wa_addtel-telephone.

      READ TABLE it_addtel INTO wa_addtel INDEX 2.

      g_v_cntno  = wa_addtel-telephone.

**—————-End  – 06 get the user detials of the initiator  with user id g_v_ernam————–**

**—————-Start – 07 Get PR amount ————————————————————**

      REFRESH: it_amount.

      SELECT banfn menge preis waers

        FROM eban

        INTO TABLE it_amount

        WHERE banfn EQ wa_eban-banfn AND

              loekz NE ‘X’.

      IF sy-subrc EQ 0.

        CLEAR: g_v_amount_net,

               wa_amount,

               g_v_currency.

        LOOP AT it_amount INTO wa_amount.

          CLEAR g_v_amount.

          g_v_amount  = wa_amount-menge  * wa_amount-preis.

          g_v_amount_net =  g_v_amount_net  +  g_v_amount .   ” Final value is in g_v_amount_net variable

        ENDLOOP.

        g_v_amount_net = g_v_amount_net / 10.

        g_v_currency = wa_amount-waers.

      ENDIF.

**——————End – 07 Get PR amount ————————————————————**

      IF g_v_frgkz EQ ‘X’ AND g_v_frgrl EQ ‘X’” check Release indicator = x and subjected to release = x

        g_v_index = 7.

        DO 8 TIMES.                              ” get the last position of X in G_V_FRGZU(Release status)

          g_v_x_pos = g_v_frgzu+g_v_index.  ”  XX    “

          IF g_v_x_pos = ‘X’.

            g_v_index1 = g_v_index.

            g_v_index5 = g_v_index1 + 1 .

            EXIT.

          ENDIF.

          g_v_index = g_v_index – 1.

        ENDDO.

      ENDIF.

    ENDIF.

****————–start – 08– get all the previous approvers —————————————–**

    g_v_banfn = wa_eban-banfn.

    CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’

      EXPORTING

        input  = g_v_banfn

      IMPORTING

        output = g_v_banfn.

    CONCATENATE  sy-mandt g_v_banfn ‘00001’ INTO g_v_banfn_key.

    SELECT  changenr

      FROM cdpos

      INTO  TABLE it_changenr

      WHERE objectclas = ‘BANF’         AND

            objectid   = g_v_banfn      AND

            fname      = ‘FRGKZ’        AND

            tabkey     = g_v_banfn_key  AND

            value_old  = ‘2’            AND

            value_new  = ‘X’.

    IF sy-subrc = 0 .

      SELECT *

        FROM cdhdr

        INTO TABLE it_udatetime

        FOR ALL ENTRIES IN it_changenr

        WHERE objectclas = ‘BANF’       AND

              objectid   = g_v_banfn   AND

              changenr   = it_changenr-changenr.

      IF sy-subrc EQ 0 .

        SORT it_udatetime BY udate DESCENDING utime DESCENDING.

        READ TABLE it_udatetime INTO wa_udatetime INDEX 1.

      ENDIF.

    ENDIF.

    SELECT  changenr

      FROM cdpos

      INTO TABLE it_changenru

      WHERE objectclas = ‘BANF’         AND

            objectid   = g_v_banfn      AND

            fname      = ‘FRGZU’        AND

            tabkey     = g_v_banfn_key .

    IF sy-subrc = 0 .

      SELECT *

        FROM cdhdr

        INTO TABLE it_udatetimeu

        FOR ALL ENTRIES IN it_changenru

        WHERE objectclas = ‘BANF’      AND

              objectid   = g_v_banfn   AND

              changenr   = it_changenru-changenr.

      IF sy-subrc EQ 0 .

        IF it_udatetime IS NOT INITIAL.

          SORT it_udatetimeu BY udate DESCENDING utime DESCENDING.

          LOOP AT it_udatetimeu INTO wa_udatetimeu.

            IF wa_udatetimeu-udate = wa_udatetime-udate.

              IF wa_udatetimeu-utime GE wa_udatetime-utime.

                wa_preapp-username = wa_udatetimeu-username.

                APPEND wa_preapp TO it_preapp.

              ENDIF.

            ELSEIF wa_udatetimeu-udate GE wa_udatetime-udate.

              wa_preapp-username = wa_udatetimeu-username.

              APPEND wa_preapp TO it_preapp.

            ENDIF.

          ENDLOOP.

        ELSEIF it_udatetime IS  INITIAL.

          LOOP AT it_udatetimeu INTO wa_udatetimeu. .

            wa_preapp-username = wa_udatetimeu-username.

            APPEND wa_preapp TO it_preapp.

          ENDLOOP.

        ENDIF.

      ENDIF.

    ENDIF.

****————–End 08 get a1l the previous approvers ———————————————-**

    IF g_v_banpr = ’03’.                               ” check processing status  = 03

      CASE g_v_index5.                                 ” check release status = 1,2,3,4,5,6,7,8

        WHEN 0.

*          IF G_V_RELCODE EQ ‘ZZ’.

          CONCATENATE g_v_frgcx ‘2’ INTO g_v_frgcx.

          g_v_frgcx2 = g_v_frgcx.

*          ENDIF.

        WHEN 1.

          CONCATENATE g_v_frgcx ‘3’ INTO g_v_frgcx.

          g_v_frgcx2 = g_v_frgcx.

        WHEN 2.

          CONCATENATE g_v_frgcx ‘4’ INTO g_v_frgcx.

          g_v_frgcx2 = g_v_frgcx.

        WHEN 3.

          CONCATENATE g_v_frgcx ‘5’ INTO g_v_frgcx.

          g_v_frgcx2 = g_v_frgcx.

        WHEN 4.

          CONCATENATE g_v_frgcx ‘6’ INTO g_v_frgcx.

          g_v_frgcx2 = g_v_frgcx.

        WHEN 5.

          CONCATENATE g_v_frgcx ‘7’ INTO g_v_frgcx.

          g_v_frgcx2 = g_v_frgcx.

        WHEN 6.

          CONCATENATE g_v_frgcx ‘8’ INTO g_v_frgcx.

          g_v_frgcx2 = g_v_frgcx.

      ENDCASE.

    ELSEIF g_v_banpr = ’08’.      ” processing status  = 08 means PR is rejected

**—————-Start 09 notify rejection  to initiator and all the previous approvers————–**

      wa_mailrecipients-rec_type  = ‘U’.                ” mail to initiator

      wa_mailrecipients-receiver = str_address-e_mail.

      APPEND wa_mailrecipients TO  lt_mailrecipients .

      LOOP AT it_preapp INTO wa_preapp.

        CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

          EXPORTING

            username = wa_preapp-username

          IMPORTING

            address  = str_address2

          TABLES

            return   = it_return.

        CLEAR wa_mailrecipients .                         ” CC to all previous approver

        wa_mailrecipients-rec_type  = ‘U’.

        wa_mailrecipients-copy  = ‘X’.

        wa_mailrecipients-receiver = str_address2-e_mail.

        APPEND wa_mailrecipients TO  lt_mailrecipients .

      ENDLOOP.

* Subject.

      lt_mailsubject-obj_name = ‘SAP – MM’.

      lt_mailsubject-obj_langu = sy-langu.

      CONCATENATE  ‘SAP – MM – PR ‘ wa_eban-banfn ‘has been rejected in  SAP’

             INTO  lt_mailsubject-obj_descr

      SEPARATED BY space.

* Mail Contents

      CONCATENATE  ‘The PR ‘ wa_eban-banfn ‘created by you, has been rejected in SAP ‘  ” str_address-firstname str_address-lastname ‘.’

             INTO  wa_mailtxt

      SEPARATED BY space .

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

      wa_mailtxt = ‘ ‘.

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

      g_v_amnt_c = g_v_amount_net .

      CONDENSE : g_v_amnt_c .

      CONCATENATE ‘PR amount: ‘ g_v_amnt_c g_v_currency INTO wa_mailtxt SEPARATED BY space.

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

      wa_mailtxt = ‘ ‘.

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

      wa_mailtxt = ‘Regards’.

      APPEND wa_mailtxt TOlt_mailtxt. CLEAR wa_mailtxt.

      wa_mailtxt = ‘SAP’.

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

      wa_mailtxt = ‘ ‘.

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

      wa_mailtxt = ‘ ‘.

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

      wa_mailtxt = ‘”This is auto generated email. Please do not reply to this email.”‘.

      APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

* Send Mail

      CALL FUNCTION ‘SO_NEW_DOCUMENT_SEND_API1’

        EXPORTING

          document_data              = lt_mailsubject

        TABLES

          object_content             = lt_mailtxt

          receivers                  = lt_mailrecipients

        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 EQ 0.

        COMMIT WORK.

*   Push mail out from SAP outbox

        SUBMIT rsconn01 WITH mode = ‘INT’ AND RETURN.

        EXIT.

      ENDIF.

**—————-End 09 notify rejection  to initiator and all the previous approvers—————**

    ENDIF.

    SELECT SINGLE frggr frgst ekgrp       ” get Release Group, Release Strategy

       FROM eban

       INTO (g_v_frggr,g_v_frgst,g_v_ekgrp)

       WHERE banfn = wa_eban-banfn.

    IF sy-subrc EQ 0.

      DO 9 TIMES.                         ” collect the release code field greater then the current

        IF g_v_frgcx+4 EQ 9 .

          EXIT.

        ELSE.

          wa_frgcx = g_v_frgcx.

          APPEND wa_frgcx TO it_frgcx.

          g_v_cntr  = g_v_frgcx+4.

          g_v_cntr  = g_v_cntr + 1.

          CONCATENATE g_v_frgcx1 g_v_cntr INTO g_v_frgcx.

        ENDIF.

      ENDDO.

      SELECT SINGLE (g_v_frgcx2)                 ” get Release code

        FROM t16fs

        INTO g_v_frgcx3

        WHERE frggr = g_v_frggr AND

              frgsx = g_v_frgst.

      IF sy-subrc EQ 0.

**————–START 10 WHO IS FINAL APPROVER —————————————————–**

        SELECT frgc1 frgc2 frgc3 frgc4 frgc5 frgc6 frgc7 frgc8

          FROM t16fs

          INTO TABLE it_fgrc0

          WHERE frggr = g_v_frggr AND

                frgsx = g_v_frgst.

        IF sy-subrc = 0.

          READ TABLE it_fgrc0 INTO wa_fgrc0 INDEX 1.

          g_v_index4 = 8.

          DO 8 TIMES .

            ASSIGN COMPONENT g_v_index4 OF STRUCTURE wa_fgrc0 TO <fs_fgrc02>.

            IF <fs_fgrc02> IS NOT INITIAL .

              g_v_final_app = <fs_fgrc02>.

              EXIT.

            ENDIF.

            g_v_index4 = g_v_index4 – 1.

          ENDDO.

        ENDIF.

**————–End 10 who is final approver ——————————————————**

        IF g_v_final_app = g_v_rel_code.     ” IS THis  APPROVal IS THE FINAL APProval AS PER RELEASE STATEGY

**————- start 12 send mail to final approver with all previous approver in CC————–**

          SELECT SINGLE zmm_bname                          ” GET THE USER ID FROM z TABLE

            FROM zmm_aprover

            INTO g_v_appid

            WHERE zmm_frggr = g_v_frggr  AND               ” Release group

                  zmm_frgsx = g_v_frgst  AND               ” Release Strategy

                  zmm_ekgrp = g_v_ekgrp  AND               ” Purchasing Group

                  zmm_frgco = g_v_final_app.                  ” Release code

          IF sy-subrc EQ 0.

*————-Start – 013 Get the mail id of the approver——————————————**

            IF  g_v_appid IS NOT INITIAL.

              CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

                EXPORTING

                  username      = g_v_appid

                  cache_results = ‘X’

                IMPORTING

                  address       = str_address1

                TABLES

                  return        = it_return.

            ENDIF.

*————-End  – 013 Get the mail id of the approver——————————————**

            wa_mailrecipients-rec_type  = ‘U’.                 ” mail to initialtor

            wa_mailrecipients-receiver  = str_address-e_mail.

            APPEND wa_mailrecipients TO  lt_mailrecipients .

*Subject.

            lt_mailsubject-obj_name = ‘SAP – MM’.

            lt_mailsubject-obj_langu = sy-langu.

            CONCATENATE  ‘SAP – MM – PR ‘ wa_eban-banfn ‘for Release in SAP’

                   INTO  lt_mailsubject-obj_descr

            SEPARATED BY space.

*Mail Contents

            CONCATENATE  ‘A PR ‘ wa_eban-banfn ‘has been created in SAP by’ str_address-firstname str_address-lastname ‘and it is final approved .’

                   INTO  wa_mailtxt

            SEPARATED BY space .

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘You are kindly requested to please take a note of the same.’.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            g_v_amnt_c = g_v_amount_net .

            CONDENSE : g_v_amnt_c .

            CONCATENATE ‘Net value: ‘ g_v_amnt_c g_v_currency INTO wa_mailtxt SEPARATED BY space.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            CONCATENATE ‘Phone number:’  g_v_cntno ‘/’ g_v_mob INTO   wa_mailtxt SEPARATED BY space.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            .

            CLEAR: str_address3.

            IF  g_v_appid IS NOT INITIAL.

              CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

                EXPORTING

                  username = g_v_appid

                IMPORTING

                  address  = str_address3

                TABLES

                  return   = it_return.

            ENDIF.

            CONCATENATE ‘Last Approver Name:’ str_address3-fullname INTO wa_mailtxt SEPARATED BY space.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘Regards’.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘SAP’.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘”This is auto generated email. Please do not reply to this email.”‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

*Send Mail

            CALL FUNCTION ‘SO_NEW_DOCUMENT_SEND_API1’

              EXPORTING

                document_data              = lt_mailsubject

              TABLES

                object_content             = lt_mailtxt

                receivers                  = lt_mailrecipients

              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 EQ 0.

              COMMIT WORK.

*Push mail out from SAP outbox

              SUBMIT rsconn01 WITH mode = ‘INT’ AND RETURN.

              EXIT.

            ENDIF.

          ENDIF.

**————-End 12  send mail to final approver with all previous approver in CC—————–**

        ELSE.

          SELECT SINGLE zmm_bname                          ” GET THE USER ID FROM z TABLE

            FROM zmm_aprover

            INTO g_v_appid

            WHERE zmm_frggr = g_v_frggr  AND           ” Release group

                  zmm_frgsx = g_v_frgst  AND           ” Release Strategy

                  zmm_ekgrp = g_v_ekgrp  AND           ” Purchasing Group

                  zmm_frgco = g_v_frgcx3.               ” Release code

          IF sy-subrc EQ 0.

*————-Start 14 Get the mail id of the approver———————————————-**

            IF  g_v_appid IS NOT INITIAL.

              CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

                EXPORTING

                  username      = g_v_appid

                  cache_results = ‘X’

                IMPORTING

                  address       = str_address1

                TABLES

                  return        = it_return.

            ENDIF.

*————-End  014 —Get the mail id of the approver——————————————-**

*————-Start 015 —Send mail to this ID—————————————————–**

*Send mail to the approver if there is an entry in the z table———————-**

*recipients

            wa_mailrecipients-rec_type  = ‘U’.                 ” mail to approver

            wa_mailrecipients-receiver = str_address1-e_mail.

            APPEND wa_mailrecipients TO  lt_mailrecipients .

            CLEAR wa_mailrecipients .                         ” cc to initiator

            wa_mailrecipients-rec_type  = ‘U’.

            wa_mailrecipients-copy  = ‘X’.

            wa_mailrecipients-receiver = str_address-e_mail.

            APPEND wa_mailrecipients TO  lt_mailrecipients .

*Subject.

            lt_mailsubject-obj_name = ‘SAP – MM’.

            lt_mailsubject-obj_langu = sy-langu.

            CONCATENATE  ‘SAP – MM – PR ‘ wa_eban-banfn ‘for Release in SAP’

                   INTO  lt_mailsubject-obj_descr

            SEPARATED BY space.

*Mail Contents

            CONCATENATE  ‘A PR ‘ wa_eban-banfn ‘has been created in SAP by’ str_address-firstname str_address-lastname ‘.’

                   INTO  wa_mailtxt

            SEPARATED BY space .

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘You are kindly requested to approve the same in SAP.’.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            g_v_amnt_c = g_v_amount_net .

            CONDENSE : g_v_amnt_c .

            CONCATENATE ‘PR amount: ‘ g_v_amnt_c g_v_currency INTO wa_mailtxt SEPARATED BY space.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            CONCATENATE ‘Phone number:’  g_v_cntno ‘/’ g_v_mob INTO   wa_mailtxt SEPARATED BY space.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            IF g_v_rel_code NE ‘ZZ’.         ” approve case

              CLEAR : str_address3.

              SELECT SINGLE zmm_bname                          ” GET THE USER ID FROM z TABLE

                FROM zmm_aprover

                INTO g_v_appid1

                WHERE zmm_frggr = g_v_frggr  AND           ” Release group

                      zmm_frgsx = g_v_frgst  AND           ” Release Strategy

                      zmm_ekgrp = g_v_ekgrp  AND           ” Purchasing Group

*                      zmm_frgco = g_v_relcode.             ” Release code

                      zmm_frgco = g_v_rel_code.             ” Release code

              IF sy-subrc = 0.

                IF  g_v_appid1 IS NOT INITIAL.

                  CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

                    EXPORTING

                      username = g_v_appid1

                    IMPORTING

                      address  = str_address3

                    TABLES

                      return   = it_return.

                ENDIF.

              ENDIF.

              CONCATENATE ‘Last Approver Name:’ str_address3-fullname INTO wa_mailtxt SEPARATED BY space.

              APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            ENDIF.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘Regards’.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘SAP’.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘ ‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

            wa_mailtxt = ‘”This is auto generated email. Please do not reply to this email.”‘.

            APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

*Send Mail

            CALL FUNCTION ‘SO_NEW_DOCUMENT_SEND_API1’

              EXPORTING

                document_data              = lt_mailsubject

              TABLES

                object_content             = lt_mailtxt

                receivers                  = lt_mailrecipients

              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 EQ 0.

              COMMIT WORK.

*Push mail out from SAP outbox

              SUBMIT rsconn01 WITH mode = ‘INT’ AND RETURN.

              EXIT.

            ENDIF.

*—————–End-015 Send mail —————————————————————-**

          ELSE.

*           if data is not available in ztable then system has to

*           check with  the next Release Code with the same above logic

            SELECT frgc1 frgc2 frgc3 frgc4 frgc5 frgc6 frgc7 frgc8

              FROM t16fs

              INTO TABLE it_fgrc0

              WHERE frggr = g_v_frggr AND

                    frgsx = g_v_frgst.

            IF sy-subrc = 0.

              READ TABLE it_fgrc0 INTO wa_fgrc0 INDEX 1.

*—————–START 016 WHO IS FINAL APPROVER ————————————————–**

              g_v_index4 = 8.

              DO 8 TIMES .

                ASSIGN COMPONENT g_v_index4 OF STRUCTURE wa_fgrc0 TO <fs_fgrc02>.

                IF <fs_fgrc02> IS NOT INITIAL .

                  g_v_final_app = <fs_fgrc02>.

                  EXIT.

                ENDIF.

                g_v_index4 = g_v_index4 – 1.

              ENDDO.

*—————-End 016 who is final approver —————————————————–**

              g_v_index2 = g_v_index1 + 3.

              g_v_index3 = 8 – ( g_v_index1 + 1 ).

              DO g_v_index3 TIMES.

                ASSIGN COMPONENT  g_v_index2 OF STRUCTURE wa_fgrc0 TO <fs_fgrc0>.

                CLEAR g_v_appid.

                SELECT SINGLE zmm_bname                      ” GET THE USER ID FROM z TABLE

                  FROM zmm_aprover

                  INTO g_v_appid

                  WHERE zmm_frggr = g_v_frggr  AND           ” Release group

                        zmm_frgsx = g_v_frgst  AND           ” Release Strategy

                        zmm_ekgrp = g_v_ekgrp  AND           ” Purchasing Group

                        zmm_frgco = <fs_fgrc0>.              ” Release code

                IF sy-subrc = 0.

*—————–Start 017 Send mail ————————————————————-**

                  CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

                    EXPORTING

                      username = g_v_appid

                    IMPORTING

                      address  = str_address4

                    TABLES

                      return   = it_return.

                  wa_mailrecipients-rec_type  = ‘U’.                  ” mail to approver

                  wa_mailrecipients-receiver = str_address4-e_mail.

                  APPEND wa_mailrecipients TO  lt_mailrecipients .

                  CLEAR wa_mailrecipients .                           ” cc to initiator

                  wa_mailrecipients-rec_type  = ‘U’.

                  wa_mailrecipients-copy  = ‘X’.

                  wa_mailrecipients-receiver = str_address-e_mail.

                  APPEND wa_mailrecipients TO  lt_mailrecipients .

*Subject.

                  lt_mailsubject-obj_name = ‘SAP – MM’.

                  lt_mailsubject-obj_langu = sy-langu.

                  CONCATENATE  ‘SAP – MM – PR ‘ wa_eban-banfn ‘for Release in SAP’

                         INTO  lt_mailsubject-obj_descr

                  SEPARATED BY space.

*Mail Contents

                  CONCATENATE  ‘A PR ‘ wa_eban-banfn ‘has been created in SAP by’ str_address-firstname str_address-lastname ‘.’

                         INTO  wa_mailtxt

                  SEPARATED BY space .

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  wa_mailtxt = ‘You are kindly requested to approve the same in SAP.’.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  wa_mailtxt = ‘ ‘.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  g_v_amnt_c = g_v_amount_net .

                  CONDENSE : g_v_amnt_c .

                  CONCATENATE ‘PR amount: ‘ g_v_amnt_c g_v_currency INTO wa_mailtxt SEPARATED BY space.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  CONCATENATE ‘Phone number:’  g_v_cntno ‘/’ g_v_mob INTO   wa_mailtxt SEPARATED BY space.

*                  CONCATENATE ‘Phone number:’ str_address-tel1_numbr INTO wa_mailtxt SEPARATED BY space.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  IF g_v_rel_code NE ‘ZZ’.         ” approve case

                    CLEAR : str_address3, g_v_appid1.

                    SELECT SINGLE zmm_bname                          ” GET THE USER ID FROM z TABLE

                      FROM zmm_aprover

                      INTO g_v_appid1

                      WHERE zmm_frggr = g_v_frggr  AND           ” Release group

                            zmm_frgsx = g_v_frgst  AND           ” Release Strategy

                            zmm_ekgrp = g_v_ekgrp  AND           ” Purchasing Group

*                      zmm_frgco = g_v_relcode.             ” Release code

                            zmm_frgco = g_v_rel_code.             ” Release code

                    IF sy-subrc = 0.

                      IF  g_v_appid1 IS NOT INITIAL.

                        CALL FUNCTION ‘BAPI_USER_GET_DETAIL’

                          EXPORTING

                            username = g_v_appid1

                          IMPORTING

                            address  = str_address3

                          TABLES

                            return   = it_return.

                      ENDIF.

                    ENDIF.

                    CONCATENATE ‘Last Approver Name:’ str_address3-fullname INTO wa_mailtxt SEPARATED BY space.

                    APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  ENDIF.

                  wa_mailtxt = ‘ ‘.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  wa_mailtxt = ‘Regards’.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  wa_mailtxt = ‘SAP’.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  wa_mailtxt = ‘ ‘.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  wa_mailtxt = ‘ ‘.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

                  wa_mailtxt = ‘”This is auto generated email. Please do not reply to this email.”‘.

                  APPEND wa_mailtxt TO lt_mailtxt. CLEAR wa_mailtxt.

*Send Mail

                  CALL FUNCTION ‘SO_NEW_DOCUMENT_SEND_API1’

                    EXPORTING

                      document_data              = lt_mailsubject

                    TABLES

                      object_content             = lt_mailtxt

                      receivers                  = lt_mailrecipients

                    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 EQ 0.

                    COMMIT WORK.

*Push mail out from SAP outbox

                    SUBMIT rsconn01 WITH mode = ‘INT’ AND RETURN.

                    EXIT.

                  ENDIF.

*————End 017 Send mail ———————————————————————-**

                ELSE.

                  g_v_index2 = g_v_index2 + 1.

                ENDIF.

              ENDDO.

            ENDIF.

          ENDIF.

        ENDIF.

      ENDIF.

    ENDIF.

  ENDIF.

ENDMETHOD.

Some important points which can be usefull based on the requirement.

This badi Will carry only the PR number when it is called  , but in case you require the release code entred/selected by user , then u have to export it and then import it when the BADI is called . e.g

1. In ME54 you need to create a explicit enhacement in include MM06BI0P_PROCESS_D0105 , enhacement point

     es_sapmm06b

    like

ENHANCEMENT ZGET_RELEASECODE.    “active version

****Start change by gej ojohn on 29.1.2013 for object E9857-16***********

* SET PARAMETER ID ‘RCD’ FIELD RM06B-FRGAB.

export RM06B-FRGAB from RM06B-FRGAB to memory id ‘CDR’.

****End change by gej ojohn on 29.1.2013 for object E9857-16***********

ENDENHANCEMENT.

2. In ME54N need to create a implicit enhacement in include LMERELVICI1 method hotspot_click

ENHANCEMENT ZGET_CANCEL_RELESE.    “active version

*****Start change : E9857 – 16 by gejo john (287477)**********02.01.2013*********************

EXPORT l_fcode from l_fcode TO MEMORY ID ‘FCD’.

EXPORT LS_DATA-REL_CODE from LS_DATA-REL_CODE TO MEMORY ID ‘RCD’.

*****End change : E9857 – 16 by gejo john (287477)**********02.01.2013*********************

ENDENHANCEMENT.

3. For ME55 you need not to do any thing direcly in our bADi u can get it like

  IF sy-tcode = ‘ME55’.

    clear g_v_rel_code.

    GET PARAMETER ID ‘BFC’ FIELD g_v_rel_code.

  ENDIF.

4.in case ME54N , the user is allowed to cancel or release the code before saving . in such case we first have to identify whether user have

   canceled or release the PR , then only we can skip the email triggering when user cancel the PR and SAVE.

in this case we have to put a

implicit enhacement in include LMERELVICI1 method hotspot_click. like

ENHANCEMENT ZGET_CANCEL_RELESE.    “active version

*****Start change : E9857 – 16 by gejo john (287477)**********02.01.2013*********************

EXPORT l_fcode from l_fcode TO MEMORY ID ‘FCD’.

EXPORT LS_DATA-REL_CODE from LS_DATA-REL_CODE TO MEMORY ID ‘RCD’.

*****End change : E9857 – 16 by gejo john (287477)**********02.01.2013*********************

ENDENHANCEMENT.

l_fcode will tell you whether user have canceled the PR or released the PR before saving , this value you can import in your badi and can skip in case of cancel .

As in the standard selection screen the input field release code is defind with Memory id ‘BFC’ with parameter statement.

   

Hope the document is usefull for you.

Thanks,

Gejo john.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply