bte.doc_0

Embed Size (px)

Citation preview

Business Transaction Events(see also TheSAPConsultan for duplicate invoice check process 00001110)This activity describes how you can connect additional components (such as in the form of function modules developed yourself or a product from an external software provider) to the standard R/3 System. There are two types of interface available in the General Ledger Accounting (FI-GL), Accounts Receivable and Accounts Payable (FI-AR/FI-AP), and Sales and Distribution (SD) components for this: 1. Publish & Subscribe interfaces (also called "informing interfaces" in the following) These should inform you about particular events (such as a document being entered) in the SAP standard application and make the data generated as a result available to the external software. The external software does not return any data to the standard R/3 System. (I'm confused. MIT is using two business transaction events for the SD to FI interface. Both of these events are Publish & Subscribe interfaces. They both return data to the SAP standard application! They both change the FI document before it is posted. What I've figured out is that if there is an export to memory and an import from memory before and after the function call, then we can't modify the data. We can check this in the calling function module - OUTBOUND_CALL_00503110_E or OPEN_FI_PERFORM_00001020_E, etc) Examples of such events in the R/3 System are: * Master record was created, changed, or blocked * Document was entered, parked, changed, or reversed * Items were cleared or reset Additional processing can be caused in the additional component on the basis of these events and data: * Starting a workflow * Generating or changing additional data * Requesting correspondence FI Clearing (F-32) calls (gathered via SE30): Function OPEN_FI_PERFORM_00001020_E (can't modify data) 00001020 POST DOCUMENT: Prior to final checks

EVENT This Event is reached prior to completing the document. The checks that are carried out at this point include checking, prior to posting, that the document balances to zero, and calculations for tax offsetting. This Event is accessed once per standard posting process and is similar to validation at document level (Event 0003). A document number has not yet been assigned when this Event is reached. INTERFACE All document line items created and the document header data are transferred (both as tables).

Parameter T_BSEG T_BKPF Function OPEN_FI_PERFORM_00001025_E (can't modify data) 00001025 POST DOCUMENT: Final checks completed

EVENT This Event lies following all checks on the whole document, but prior to number assignment. Following this Event, no further error messages may be sent. The document is complete at this point, and no further changes can be made to it prior to posting. INTERFACE All current data, all document data relevant for the posting, and various control parameters are transferred to the additional component. At this point you can still implement your own checks on the current document data. Parameter I_BKDF T_AUSZ1 T_AUSZ2 T_AUSZ3 T_BKP1 T_BKPF T_BSEC T_BSED T_BSEG T_BSET T_BSEU Function OPEN_FI_PERFORM_00001030_E (can't modify data) Function OPEN_FI_PERFORM_00001140_E (can't modify data) 2. Process interfaces (also referred to as "process" in the following) Process interfaces are used to submit business processes to a different control which cannot be realized with the standard system, that is process interfaces replace standard processes. Here you can structure determination of individual field contents or of specific reactions to process flows individually. It is possible to connect different external developments to the standard R/3 System. The additional developments are generally carried out using the ABAP/4 Development Workbench. This way you can, for example, influence the control of payment transactions. Selection of payment method, house bank and partner bank can be made using the payment data (currency, amount, and payee) according to a selection logic which you have defined. FI Clearing (F-32) calls: Function Function Function Function OPEN_FI_PERFORM_00001120_P OPEN_FI_PERFORM_00001130_P OPEN_FI_PERFORM_00001150_P OPEN_FI_PERFORM_00001170_P

Standard Settings Sample modules are delivered in the standard R/3 System which you can copy into your name range and fill them with statements there.

Activities 1. Enter a product using "Settings -> Customer's products". Actually, in 4.6C, from transaction FIBF (IMG -> Financial Accounting -> Financial Accounting Global Settings -> Business Transaction Events): Settings -> Products -> ...of a customer

There are generally a large number of function modules belonging to a product which can be called by different program events from within the standard R/3 System. A product can also be an external software component. If the product is in an external system, enter an RFC destination here. If the product is in the same system, you do not have to enter anything. Important: Do not forget to activate the product after making the following settings. 2. Establish the interfaces with which the R/3 System provides you. To do this, choose Environment -> Info system (P/S) or Environment -> Info system (Processes).

Execute the program. You should enter "A" as the attribute type. You see the respective interfaces with which the R/3 System provides you. Note the key of the interface which you require. You can also select: * By particular SAP application components

* By particular events by entering intervals * Which interfaces are used in activated products * Which interfaces are used within a particular country version or within a particular industry * Which interfaces are used within a particular customer product

3. Enter the function module which you have developed yourself. To do this, choose either Goto -> Edit modules within the info system or Settings -> P/S modules or Process modules -> Customer's in the "SAP Business Framework" menu. Make the following entries: * Key for the interface * The product that you want to use * Function module which belongs to this product. You can also enter several function modules for a product. Caution: The function module must be within your name range, that is must begin with the letter Z. You leave the Ctr and Appl. fields blank unless you want a particular country version or a particular SAP industry-specific component to be enhanced or replaced instead of the

standard process flow. 4. Fill the source text of your function module and activate it. To do this, go again via Environment -> Info system (P/S) or Environment -> Info system (Processes) into the information system and execute the program. Then proceed as follows: * Click twice on the interface you have chosen. If you want, you can look at the interface at this point by choosing Goto -> Interface. Then choose the Back function again afterwards. * Place the cursor on the relevant line and choose Goto -> Function library. You see the sample function module delivered by SAP. * Copy the sample module delivered by SAP and call it the same as the function module entered in step 3. * Fill the source text of the empty function module. * Activate the function module. * Activate the product as described in step 1. 5. Run the R/3 program affected and test whether calling your function module works. Further Notes The other menu paths are only used for information about additional components delivered by SAP or about software already installed by external software providers.

SAP Business Transaction EventsIm extremely sorry that I couldnt write on my blog for some time because of the assignments that I had to finish for my clients! Today I got some free time and decided to talk about SAP business transaction events! SAP business transaction events are one type of customer enhancements provided by SAP! On this post Ill talk about how we can change the logic for duplicate vendor invoice check using SAP business transaction events! We can access the business transaction events using FIBF

Next we have to find the process interface for duplicate invoice check! To do that, follow the following steps. Select the menu as stated below

Execute the info system as stated below

All the processes will be shown below

Select the process 1110 and click on Sample function module as stated below

Copy the sample functional module SAMPLE_PROCESS_00001110 and create ZSAMPLE_PROCESS_00001110. Put the customized logic in the functional module ZSAMPLE_PROCESS_00001110 and activate it!

Now go back to FIBF and execute the menu as shown below

Define a new product and activate it

Now go back to FIBF and execute the menu as shown below

Assign the function module ZSAMPLE_PROCESS_00001110 to the process 1110and the product that was defined in the earlier step.

Bingo!!!!!! Now every time when a vendor invoice is being posted the invoice check will be carried out using the custom logic built into the function module ZSAMPLE_PROCESS_00001110. But make sure that the double invoice check tick has been put on the relevant vendor masters! I hope this post about the SAP business transaction events will help your business! Thank you very much for reading my blog! Please feel free to send me your comments!

Creation of Events via Business Transaction EventsUseG/L accounting and accounts receivable/payable accounting provide publish and subscribe interfaces, which inform other application components or external interested parties (partners, customers) that particular events (such as document entered) have taken place in financial accounting. The data this creates can be used by the interested parties for their own subsequent processing. But no data can be returned to G/L accounting or accounts receivable/payable accounting. SAP Business Workflow uses this interface to create events of object types defined in the Business Object Repository from its events. These events can be used as triggering events of a workflow, for example.

FeaturesWhen a Business Transaction Event occurs the system calls a function module which creates the BOR event. To do this, this function module "converts" the data from the Business Transaction Event into the format suitable for the event and creates the appropriate event for the Business Transaction Event. The function module hence has a parameter interface and implementation which are appropriate for the Business Transaction Event. For every Business Transaction Event that can be linked to an event, SAP provides a suitable function module with appropriate interface and implementation. The following relationships are currently possible: Business Transaction Event Event

00001030

POST DOCUMENT: Update standard data

BKPF

Accounting document

CREATED

Document created

00001040

REVERSE CLEARING: After standard update

BKPF

Accounting document

CLEARINGREVERSED

Clearing reversed

00001050

POST DOCUMENT: FI/CO interface

BKPF

Accounting document

CREATED

Document created

00001110

CHANGE DOCUMENT: Save standard data

BKPF

Accounting document

CHANGED

Document changed

00001320

CUSTOMER MASTER DATA: Save

BUS3007

Customer account

CREATED

Account opened

00001420

VENDOR MASTER DATA: Save

BUS3008

Vendor account

CREATED

Account opened

00001520

CREDIT MANAGEMENT: Save

BUS1010

Customer credit account

CREATED

Account opened

ActivitiesThe Business Transaction Events and the events are already defined. You may create the linkage between them. You can use a wizard for event creation.

Finally, you must set the status of the event whose creation you have just configured to released. To do this, call the Business Object Builder and change the release status of the event for the relevant object type.

REPORT zsmartformpdf_sendemail NO STANDARD PAGE HEADING LINE-SIZE 132 LINE-COUNT 65 MESSAGE-ID zchk. *----------------------------------------------------------------------* TABLES *----------------------------------------------------------------------TABLES : aufk,t352t,t003o,toa_dara,arc_params,nast. *----------------------------------------------------------------------* DATA: INTERNAL TABLES *----------------------------------------------------------------------DATA : it_aufk TYPE STANDARD TABLE OF aufk. DATA: it_lines TYPE STANDARD TABLE OF tline WITH HEADER LINE. DATA: BEGIN OF it_header OCCURS 0. " define a local table header INCLUDE STRUCTURE thead. " using SAPSCRIPT structure DATA: END OF it_header. DATA : wa_aufk LIKE LINE OF it_aufk. DATA : wa_lines LIKE LINE OF it_lines, v_bin_filesize TYPE i, it_docs TYPE STANDARD TABLE OF docs, it_lines1 TYPE STANDARD TABLE OF tline, v_name1 TYPE string, v_path TYPE string, v_fullpath TYPE string, v_filter TYPE string, v_uact TYPE i, v_guiobj TYPE REF TO cl_gui_frontend_services, v_filename TYPE string, v_fm_name TYPE rs38l_fnam. DATA : mstr_print_parms LIKE pri_params, mc_valid(1) TYPE c, mi_bytecount TYPE i, mi_length TYPE i, mi_rqident LIKE tsp01-rqident. DATA : p_linsz LIKE sy-linsz VALUE 132, " Line size p_paart LIKE sy-paart VALUE 'X_65_132'. " Paper Format DATA : var TYPE i VALUE 0 . DATA : p_repid LIKE sy-repid . DATA : it_tsp01 TYPE STANDARD TABLE OF tsp01 WITH HEADER LINE . DATA : it_pdf LIKE tline OCCURS 0 WITH HEADER LINE . * Spool to PDF conversions DATA: gd_spool_nr LIKE tsp01-rqident, gd_destination LIKE rlgrap-filename, gd_bytecount LIKE tst01-dsize, gd_buffer TYPE string. *DATA DECLARATION DATA: gd_recsize TYPE i.

* Spool IDs TYPES: BEGIN OF t_tbtcp. INCLUDE STRUCTURE tbtcp. TYPES: END OF t_tbtcp. DATA: it_tbtcp TYPE STANDARD TABLE OF t_tbtcp INITIAL SIZE 0, wa_tbtcp TYPE t_tbtcp. * Job Runtime Parameters DATA: gd_eventid LIKE tbtcm-eventid, gd_eventparm LIKE tbtcm-eventparm, gd_external_program_active LIKE tbtcm-xpgactive, gd_jobcount LIKE tbtcm-jobcount, gd_jobname LIKE tbtcm-jobname, gd_stepcount LIKE tbtcm-stepcount, gd_error TYPE sy-subrc, gd_reciever TYPE sy-subrc. DATA: w_recsize TYPE i. DATA: gd_subject LIKE sodocchgi1-obj_descr, it_mess_bod LIKE solisti1 OCCURS 0 WITH HEADER LINE, it_mess_att LIKE solisti1 OCCURS 0 WITH HEADER LINE, gd_sender_type LIKE soextreci1-adr_typ, gd_attachment_desc TYPE so_obj_nam, gd_attachment_name TYPE so_obj_des. *----------------------------------------------------------------------* DATA: VARIABLES *----------------------------------------------------------------------DATA: fm_sform TYPE rs38l_fnam, v_name TYPE thead-tdname. CONSTANTS: c_id(4) TYPE c VALUE 'KOPF', c_langu(1) TYPE c VALUE 'E', c_object(10) TYPE c VALUE 'AUFK'. *--Data Declaration for Printing Layout DATA: ls_itcpo TYPE itcpo. DATA: lf_repid TYPE sy-repid. DATA: lf_device TYPE tddevice. DATA: cf_retcode TYPE sy-subrc. DATA: ls_recipient TYPE swotobjid. DATA: ls_sender TYPE swotobjid. DATA: ls_control_param TYPE ssfctrlop. DATA: ls_composer_param TYPE ssfcompop. DATA: ls_addr_key LIKE addr_key. DATA: w_screen(1) TYPE c. DATA: xscreen(1) TYPE c. DATA: da_mess LIKE vbfs OCCURS 0 WITH HEADER LINE. DATA: ws_fname TYPE rs38l_fnam, ws_ctrlp TYPE ssfctrlop, ws_optns TYPE ssfcompop, w_padest LIKE tsp03l-padest. DATA: da_message_printed(1) TYPE c, da_preview_processed(1) TYPE c,

repeat(1) TYPE c, da_subrc LIKE sy-subrc. DATA: w_otfdata TYPE ssfcrescl. DATA: BEGIN OF it_itcoo OCCURS 0. INCLUDE STRUCTURE it coo. DATA: END OF it_itcoo. DATA: w_otf TYPE it coo. IMPORT var FROM MEMORY ID 'JIT1' . FREE MEMORY ID 'JIT1'. IF var = 0. *&---------------------------------------------------------------------* *& Form GET_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM get_data . SELECT aufnr auart zzplat objnr adrnra werks ktext zzplat saknr zzmainftln zzserviceloc zzcounty zzpremise zzpipe ktext adrnra erdat zzcross_street1 zzcross_street2 zzcurkcockftln INTO CORRESPONDING FIELDS OF TABLE it_aufk FROM aufk WHERE aufk~aufnr = s_aufnr. IF it_aufk IS INITIAL. MESSAGE i004. LEAVE TO LIST-PROCESSING. ENDIF. READ TABLE it_aufk INDEX 1 INTO wa_aufk. CONCATENATE sy-mandt wa_aufk-aufnr INTO v_name. *& read long text CALL FUNCTION 'READ_TEXT' EXPORTING id = c_id

language = c_langu name = v_name object = c_object IMPORTING header = it_header TABLES lines = it_lines EXCEPTIONS id =1 language =2 name =3 not_found =4 object =5 reference_check =6 wrong_access_to_archive = 7 OTHERS = 8. ENDFORM. " GET_DATA *&---------------------------------------------------------------------* *& Form CALL_FORM *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM call_form . DATA lv_msgtype TYPE sy-msgty. IMPORT it_aufk it_lines it_header FROM MEMORY ID 'JIT1' . IF var = 0. CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME' EXPORTING formname = 'ZPM_OPERATION' IMPORTING fm_name = fm_sform 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. CHECK NOT fm_sform IS INITIAL. CLEAR w_otfdata. ls_control_param-getotf = 'X'. CALL FUNCTION fm_sform TABLES it_aufk = it_aufk it_lines = it_lines it_header = it_header 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. *-- Setup the Print Parmaters CALL FUNCTION 'GET_PRINT_PARAMETERS' EXPORTING authority = space copies = '1' cover_page = space data_set = space department = space destination = space expiration = '1' immediately = space new_list_id = 'X' no_dialog = 'X' user = sy-uname IMPORTING out_parameters = mstr_print_parms valid = mc_valid EXCEPTIONS archive_info_not_found = 1 invalid_print_params = 2 invalid_archive_params = 3 OTHERS = 4. *-- Make sure that a printer destination has been set up *-- If this is not done the PDF function module ABENDS IF mstr_print_parms-pdest = space. mstr_print_parms-pdest = 'LP01'. ENDIF. *-- Explicitly set line width, and output format so that *-- the PDF conversion comes out OK mstr_print_parms-linsz = p_linsz. mstr_print_parms-paart = p_paart. * for Submit the report to create the abapspool. var = var + 1. FREE MEMORY ID 'JIT1'. EXPORT it_aufk it_lines it_header var TO MEMORY ID 'JIT1' . SUBMIT (p_repid) TO SAP-SPOOL SPOOL PARAMETERS mstr_print_parms WITHOUT SPOOL DYNPRO AND RETURN. *selecting the spool request using the above consructed varibale SELECT * FROM tsp01 INTO TABLE it_tsp01 WHERE rq2name = sy-uname . IF sy-subrc = 0.

*sorting the internal table SORT it_tsp01 BY rqcretime DESCENDING . *reading the first spool request READ TABLE it_tsp01 INDEX 1. REFRESH it_pdf[]. CLEAR it_pdf. *converting the spool request into pdf CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF' EXPORTING src_spoolid = it_tsp01-rqident no_dialog ='' TABLES pdf = it_pdf EXCEPTIONS err_no_otf_spooljob =1 err_no_spooljob =2 err_no_permission =3 err_conv_not_possible = 4 err_bad_dstdevice =5 user_cancelled =6 err_spoolerror =7 err_temseerror =8 err_btcjob_open_failed = 9 err_btcjob_submit_failed = 10 err_btcjob_close_failed = 11. CHECK sy-subrc = 0. * Transfer the 132-long strings to 255-long strings LOOP AT it_pdf. TRANSLATE it_pdf USING ' ~'. CONCATENATE gd_buffer it_pdf INTO gd_buffer. ENDLOOP. TRANSLATE gd_buffer USING '~ '. DO. it_mess_att = gd_buffer. APPEND it_mess_att. SHIFT gd_buffer LEFT BY 255 PLACES. IF gd_buffer IS INITIAL. EXIT. ENDIF. ENDDO. *RECIPIENT DATA : p_email1 TYPE somlreci1-receiver VALUE '[email protected]'. DESCRIBE TABLE it_mess_att LINES gd_recsize. CHECK gd_recsize > 0. *& Subroutine to send the email to the RECIPIENT. PERFORM send_email USING p_email1. ENDIF. ENDIF. ENDFORM. " CALL_FORM

*&---------------------------------------------------------------------* *& Form VALIDATE_AUFNR_INPUTS *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM validate_aufnr_inputs . SELECT SINGLE * FROM aufk WHERE aufnr = s_aufnr. *& Error Message for Order Number IF sy-subrc 0. MESSAGE e018 WITH s_aufnr . ENDIF. ENDFORM. " VALIDATE_AUFNR_INPUTS *&---------------------------------------------------------------------* *& Form POPUP *&---------------------------------------------------------------------* FORM popup . DATA : answer TYPE char1. CLEAR answer. CALL FUNCTION 'POPUP_TO_CONFIRM' EXPORTING titlebar = 'CONFIRMATION' text_question = 'Do You Want To Print' text_button_1 = 'YES' icon_button_1 = 'ICON_CHECKED' text_button_2 = 'CANCEL' icon_button_2 = 'ICON_CANCEL' default_button = '1' display_cancel_button = ' ' start_column = 25 start_row =6 IMPORTING answer = answer EXCEPTIONS text_not_found =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. IF answer = '2'. LEAVE SCREEN . ENDIF . ENDFORM. " POPUP *&---------------------------------------------------------------------* *& Form F_SEND_PDF_BY_EMAIL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_TB_PDF255 text * -->P_LTB_BODY text

* -->P_LTB_RECIPIENTS text * -->P_LW_SUBJECT text * -->P_LW_FILENAME text *----------------------------------------------------------------------* FORM f_send_pdf_by_email TABLES p_tb_pdf STRUCTURE tline p_ltb_body STRUCTURE solisti1 p_ltb_recipients STRUCTURE somlreci1 USING p_lw_subject TYPE so_obj_des p_lw_filename TYPE char100. *Variables y tablas internas. DATA: BEGIN OF ltb_objbin OCCURS 0. INCLUDE STRUCTURE solisti1. DATA: END OF ltb_objbin. DATA: lwa_doc_chng LIKE sodocchgi1, lw_tab_lines LIKE sy-tabix, ltb_objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE, ltb_objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE. *MAIN PROCESSING SECTION-----------------------------------------CLEAR: ltb_objbin, ltb_objtxt, ltb_objpack, lwa_doc_chng. REFRESH: ltb_objbin, ltb_objtxt, ltb_objpack. *Control Data lwa_doc_chng-obj_name = 'URGENT'. lwa_doc_chng-sensitivty = 'P'. lwa_doc_chng-no_change = 'X'. lwa_doc_chng-priority = '1'. lwa_doc_chng-obj_prio = '1'. lwa_doc_chng-obj_langu = sy-langu. lwa_doc_chng-no_change = 'X'. *Email Subject lwa_doc_chng-obj_descr = p_lw_subject. *Email Body LOOP AT p_ltb_body. ltb_objtxt-line = p_ltb_body-line. APPEND ltb_objtxt. ENDLOOP. CLEAR ltb_objtxt. DESCRIBE TABLE ltb_objtxt LINES lw_tab_lines. IF lw_tab_lines GT 0. READ TABLE ltb_objtxt INDEX lw_tab_lines. lwa_doc_chng-doc_size = ( lw_tab_lines - 1 ) * 255 + STRLEN( ltb_objtxt ). CLEAR ltb_objpack-transf_bin. ltb_objpack-body_start = 1. ltb_objpack-body_num = lw_tab_lines. ltb_objpack-doc_type = 'RAW'. APPEND ltb_objpack. ENDIF. LOOP AT p_tb_pdf.

ltb_objbin-line = p_tb_pdf-tdline. APPEND ltb_objbin. ENDLOOP. ltb_objpack-transf_bin = 'X'. ltb_objpack-body_start = 1. DESCRIBE TABLE ltb_objbin LINES lw_tab_lines. ltb_objpack-body_num = lw_tab_lines. ltb_objpack-doc_type = 'PDF'. ltb_objpack-obj_descr = p_lw_filename. ltb_objpack-obj_name = p_lw_filename. ltb_objpack-doc_size = lw_tab_lines * 255. APPEND ltb_objpack. *Call the function to send the PDF file by email CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING document_data = lwa_doc_chng put_in_outbox = 'X' TABLES packing_list = ltb_objpack contents_bin = ltb_objbin contents_txt = ltb_objtxt receivers = p_ltb_recipients 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. CASE sy-subrc. WHEN 0. WHEN 1. RAISE too_many_receivers. WHEN 2. RAISE document_not_sent . WHEN 3. RAISE document_type_not_exist. WHEN 4. RAISE operation_no_authorization. WHEN 5. RAISE parameter_error. WHEN 7. RAISE enqueue_error . WHEN OTHERS. RAISE x_error. ENDCASE. COMMIT WORK. ENDFORM. " F_SEND_PDF_BY_EMAIL *&---------------------------------------------------------------------* *& Form SEND_EMAIL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_P_EMAIL1 text *----------------------------------------------------------------------* FORM send_email USING p_email.

CHECK NOT ( p_email IS INITIAL ). REFRESH it_mess_bod. * Default subject matter gd_subject = 'Subject'. gd_attachment_desc = 'Attachnament'. * CONCATENATE 'attach_name' ' ' INTO gd_attachment_name. it_mess_bod = 'Hi There,'. APPEND it_mess_bod. it_mess_bod = 'Please Find the attached Smart Form for Operation Ticket Control'. APPEND it_mess_bod. DATA : p_sender TYPE somlreci1-receiver VALUE '[email protected]'. * If no sender specified - default blank IF p_sender EQ space. gd_sender_type = space. ELSE. gd_sender_type = 'INT'. ENDIF. * Send file by email as .xls speadsheet PERFORM send_file_as_email_attachment TABLES it_mess_bod it_mess_att USING p_email 'Example .pdf document attached' 'PDF' gd_attachment_name gd_attachment_desc p_sender gd_sender_type CHANGING gd_error gd_reciever. ENDFORM. " SEND_EMAIL *&---------------------------------------------------------------------* *& Form SEND_FILE_AS_EMAIL_ATTACHMENT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_IT_MESS_BOD text * -->P_IT_MESS_ATT text * -->P_P_EMAIL text * -->P_1395 text * -->P_1396 text * -->P_GD_ATTACHMENT_NAME text * -->P_GD_ATTACHMENT_DESC text * -->P_P_SENDER text * -->P_GD_SENDER_TYPE text * P_RETURN Error messages

-->P_RECEIVERS emailr recipients -->P_HTML html text

-->P_GRAPHICS html graphics if any -->P_SUBJECT subject of the email

*---------------------------------------------------------------------* FORM SEND_HTML_EMAIL TABLES P_RETURN TYPE BAPIRET2_TAB P_RECEIVERS STRUCTURE SOMLRECI1

USING P_HTML P_GRAPHICS

TYPE TRFRESULT TYPE TSF_XSF_GR

SEND_PARTNER TYPE BU_PARTNER P_SUBJECT DATA: L_GRAPHIC TYPE SSF_XSF_GR. CONSTANTS: C_GR_DIR TYPE TDTEXT VALUE 'MYGRAPHICS/'. DATA: HTML_DATA TYPE TRFRESULT, GRAPHICS TYPE TSF_XSF_GR, "#EC NOTEXT TYPE SO_OBJ_DES.

LV_SENT_TO_ALL TYPE BOOLEAN, LV_SEND_ADDRESS LI_SENDER TYPE ADR6-SMTP_ADDR, TYPE REF TO IF_SENDER_BCS,

RECEIVER TYPE SOMLRECI1. DATA: LO_BCS LO_DOC_BCS LO_MIME_HELPER LO_RECIPIENT DATA: L_FILENAME LT_SOLI LT_SOLIX LS_SOLI LS_SOLIX DATA: L_HTML_RAW HTML_XSTR HTML_STR HTML_LEN L_OFFSET L_LENGTH L_DIFF L_CONTENT_ID TYPE REF TO CL_BCS, TYPE REF TO CL_DOCUMENT_BCS, TYPE REF TO CL_GBT_MULTIRELATED_SERVICE, TYPE REF TO IF_RECIPIENT_BCS. TYPE STRING, TYPE SOLI_TAB, TYPE SOLIX_TAB, TYPE SOLI, TYPE SOLIX. LIKE LINE OF HTML_DATA-CONTENT, TYPE XSTRING, TYPE STRING, TYPE I, TYPE I, TYPE I, TYPE I, TYPE STRING,

L_CONTENT_TYPE TYPE W3CONTTYPE,

L_OBJ_LEN GR_XSTR L_GR_RAW L_USERNAME

TYPE SO_OBJ_LEN, TYPE XSTRING, TYPE BAPICONTEN, TYPE UNAME,

L_MAIL_ADDRESS TYPE AD_SMTPADR, WA_RETURN TYPE BAPIRET2. DATA: LV_SEND_REQ_BCS LV_ADDRESS_BCS LV_GBT_MIME LV_BCOM_MIME LV_DOCUMENT_BCS TYPE REF TO CX_SEND_REQ_BCS, TYPE REF TO CX_ADDRESS_BCS, TYPE REF TO CX_GBT_MIME, TYPE REF TO CX_BCOM_MIME, TYPE REF TO CX_DOCUMENT_BCS.

CLEAR HTML_XSTR. LOOP AT P_HTML-CONTENT INTO L_HTML_RAW. CONCATENATE HTML_XSTR L_HTML_RAW INTO HTML_XSTR IN BYTE MODE. ENDLOOP. HTML_XSTR = HTML_XSTR(P_HTML-LENGTH). CALL FUNCTION 'SCP_TRANSLATE_CHARS' EXPORTING INBUFF INCODE * OUTCODE CSUBST = HTML_XSTR = '4110' = '0000' = 'X' " UTF-8 "#EC NOTEXT " ACTUAL CODEPAGE "#EC NOTEXT "#EC NOTEXT "#EC NOTEXT

SUBSTC_SPACE = 'X' IMPORTING OUTBUFF OUTUSED EXCEPTIONS OTHERS = HTML_STR = HTML_LEN = 1.

* CHANGE ENCODING UTF-8 TO LATIN1 REPLACE ALL OCCURRENCES OF 'UTF-8' IN HTML_STR WITH 'iso-8859-1' IGNORING CASE. *HACK THE HTML CODE GENERATED BY SARTFORM TO MAKE THE "#EC NOTEXT

*EXTERNAL IMAGES APPEAR AS TAG IN HTML REPLACE ALL OCCURRENCES OF '' IGNORING CASE. REPLACE ALL OCCURRENCES OF '' IN HTML_STR WITH '' IGNORING CASE. "#EC NOTEXT "#EC NOTEXT

REPLACE ALL OCCURRENCES OF '' IGNORING CASE. "#EC NOTEXT

HTML_LEN = STRLEN( HTML_STR ). L_OFFSET = 0. L_LENGTH = 255. WHILE L_OFFSET < HTML_LEN. L_DIFF = HTML_LEN - L_OFFSET. IF L_DIFF > L_LENGTH. LS_SOLI-LINE = HTML_STR+L_OFFSET(L_LENGTH). ELSE. LS_SOLI-LINE = HTML_STR+L_OFFSET(L_DIFF). ENDIF. APPEND LS_SOLI TO LT_SOLI. ADD L_LENGTH TO L_OFFSET. ENDWHILE. CREATE OBJECT LO_MIME_HELPER. CALL METHOD LO_MIME_HELPER->SET_MAIN_HTML EXPORTING CONTENT = LT_SOLI

FILENAME

= 'SAPWEBFORM.HTM'

"#EC NOTEXT "#EC NOTEXT

DESCRIPTION = 'SAP WEB FORM'. LOOP AT P_GRAPHICS INTO L_GRAPHIC. CLEAR GR_XSTR. LOOP AT L_GRAPHIC-CONTENT INTO L_GR_RAW.

CONCATENATE GR_XSTR L_GR_RAW-LINE INTO GR_XSTR IN BYTE MODE. ENDLOOP. GR_XSTR = GR_XSTR(L_GRAPHIC-LENGTH). L_OFFSET = 0. L_LENGTH = 255. CLEAR LT_SOLIX[]. WHILE L_OFFSET < L_GRAPHIC-LENGTH. L_DIFF = L_GRAPHIC-LENGTH - L_OFFSET. IF L_DIFF > L_LENGTH. LS_SOLIX-LINE = GR_XSTR+L_OFFSET(L_LENGTH). ELSE. LS_SOLIX-LINE = GR_XSTR+L_OFFSET(L_DIFF). ENDIF. APPEND LS_SOLIX TO LT_SOLIX. ADD L_LENGTH TO L_OFFSET. ENDWHILE. CONCATENATE C_GR_DIR L_GRAPHIC-GRAPHICS '.BMP' INTO L_FILENAME. CONCATENATE C_GR_DIR L_GRAPHIC-GRAPHICS '.BMP' INTO L_CONTENT_ID. L_CONTENT_TYPE = L_GRAPHIC-HTTPTYPE. L_OBJ_LEN = L_GRAPHIC-LENGTH. "#EC NOTEXT "#EC NOTEXT

*Add images to the email CALL METHOD LO_MIME_HELPER->ADD_BINARY_PART EXPORTING

CONTENT FILENAME EXTENSION DESCRIPTION

= LT_SOLIX = L_FILENAME = 'BMP' = 'GRAPHIC IN BMP FORMAT' "#EC NOTEXT "#EC NOTEXT

CONTENT_TYPE = L_CONTENT_TYPE LENGTH CONTENT_ID ENDLOOP. TRY. LO_DOC_BCS = CL_DOCUMENT_BCS=>CREATE_FROM_MULTIRELATED( I_SUBJECT = P_SUBJECT = L_OBJ_LEN = L_CONTENT_ID.

I_MULTIREL_SERVICE = LO_MIME_HELPER ). CATCH CX_DOCUMENT_BCS INTO LV_DOCUMENT_BCS. * ERROR HANDLING CLEAR WA_RETURN. WA_RETURN-TYPE = 'E'. "#EC NOTEXT

MOVE LV_DOCUMENT_BCS->GET_LONGTEXT( ) TO WA_RETURN-MESSAGE. INSERT WA_RETURN INTO TABLE P_RETURN. EXIT. CATCH CX_BCOM_MIME INTO LV_BCOM_MIME. CLEAR WA_RETURN. WA_RETURN-TYPE = 'E'. "#EC NOTEXT

MOVE LV_BCOM_MIME->GET_LONGTEXT( ) TO WA_RETURN-MESSAGE. INSERT WA_RETURN INTO TABLE P_RETURN. EXIT. CATCH CX_GBT_MIME INTO LV_GBT_MIME. CLEAR WA_RETURN. WA_RETURN-TYPE = 'E'. "#EC NOTEXT

MOVE LV_GBT_MIME->GET_LONGTEXT( ) TO WA_RETURN-MESSAGE. INSERT WA_RETURN INTO TABLE P_RETURN.

EXIT. ENDTRY. * REUSE THE CONTENT PREPARED FOR ALL RECEIVERS LOOP AT P_RECEIVERS INTO RECEIVER.

L_MAIL_ADDRESS = RECEIVER-RECEIVER. * CREATE SEND_REQUEST TRY. LO_BCS = CL_BCS=>CREATE_PERSISTENT( ). LO_BCS->SET_DOCUMENT( I_DOCUMENT = LO_DOC_BCS ). CATCH CX_SEND_REQ_BCS INTO LV_SEND_REQ_BCS. CLEAR WA_RETURN. WA_RETURN-TYPE = 'E'. "#EC NOTEXT

MOVE LV_SEND_REQ_BCS->GET_LONGTEXT( ) TO WA_RETURN-MESSAGE. INSERT WA_RETURN INTO TABLE P_RETURN. EXIT. ENDTRY. * CREATE SENDER CLASS CL_CAM_ADDRESS_BCS DEFINITION LOAD. TRY. IF L_USERNAME NS '@'. L_USERNAME = SEND_PARTNER . TRANSLATE L_USERNAME TO UPPER CASE. "#EC TRANSLANG "#EC NOTEXT

LI_SENDER ?= CL_SAPUSER_BCS=>CREATE( L_USERNAME ). ELSE. DATA: L_FROM_MAIL_ADDRESS TYPE AD_SMTPADR. L_FROM_MAIL_ADDRESS = L_USERNAME. LI_SENDER ?= CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( I_ADDRESS_STRING = L_FROM_MAIL_ADDRESS ).

ENDIF. CALL METHOD LO_BCS->SET_SENDER EXPORTING I_SENDER = LI_SENDER. CATCH CX_ADDRESS_BCS INTO LV_ADDRESS_BCS. * ERROR HANDLING CLEAR WA_RETURN. WA_RETURN-TYPE = 'E'. "#EC NOTEXT

MOVE LV_ADDRESS_BCS->GET_LONGTEXT( ) TO WA_RETURN-MESSAGE. INSERT WA_RETURN INTO TABLE P_RETURN. EXIT. CATCH CX_SEND_REQ_BCS INTO LV_SEND_REQ_BCS. * FAILED TO ADD A SENDER CLEAR WA_RETURN. WA_RETURN-TYPE = 'E'. "#EC NOTEXT

MOVE LV_SEND_REQ_BCS->GET_LONGTEXT( ) TO WA_RETURN-MESSAGE. INSERT WA_RETURN INTO TABLE P_RETURN. EXIT. EXIT. ENDTRY. * CREATE RECIPIENT

TRY. LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( I_ADDRESS_STRING = L_MAIL_ADDRESS ). CATCH CX_ADDRESS_BCS INTO LV_ADDRESS_BCS. * ERROR HANDLING CONTINUE. ENDTRY. TRY.

LO_BCS->ADD_RECIPIENT( I_RECIPIENT = LO_RECIPIENT ). CATCH CX_SEND_REQ_BCS INTO LV_SEND_REQ_BCS. * FAILED TO ADD A RECIPIENT CONTINUE. ENDTRY. * SEND TRY. * RECEIPTS ONLY FOR ERRORS CALL METHOD LO_BCS->SEND_REQUEST->SET_REQUESTED_STATUS EXPORTING I_REQUESTED_STATUS = 'N'. LV_SENT_TO_ALL = LO_BCS->SEND( ). CATCH CX_SEND_REQ_BCS INTO LV_SEND_REQ_BCS. CLEAR WA_RETURN. WA_RETURN-TYPE = 'E'. "#EC NOTEXT "#EC NOTEXT

MOVE LV_SEND_REQ_BCS->GET_LONGTEXT( ) TO WA_RETURN-MESSAGE. INSERT WA_RETURN INTO TABLE P_RETURN. * ERROR HANDLING EXIT. ENDTRY. ENDLOOP. COMMIT WORK. * set the success information CLEAR WA_RETURN. MESSAGE i013 INTO WA_RETURN-MESSAGE. "Email sent successfully " END LOOP AT P_RECEIVERS

INSERT WA_RETURN INTO TABLE P_RETURN. ENDFORM. "SEND_HTML_EMAIL

Adjust the look and feel of the email content Make proper use of the smartstyles (transaction smartstyles) in the smartform to improve the look and feel of the email. Here is how a sample email looks. You can also use static images in the smartform. These images will come as multipart format. You can also make use of URL links in the smartforms to provide the link from email to the webapplication like webshop