on 2025 Apr 03 7:34 AM
Hello Experts ,
We have a requirement to send an email when the sales order is released. We have found an event Change in the BO i_Salesordertp. This is the class we have created
CLASS zcl_sales_order_event_handler DEFINITION
PUBLIC
ABSTRACT
FINAL
FOR EVENTS OF i_salesordertp.
PUBLIC SECTION.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
Local types :
LASS lcl_so_event_consumption DEFINITION INHERITING FROM cl_abap_behavior_event_handler.
PRIVATE SECTION.
METHODS:
so_change_consume FOR ENTITY EVENT event_parameters FOR salesorder~changed.
ENDCLASS.
CLASS lcl_so_event_consumption IMPLEMENTATION.
METHOD so_change_consume.
READ ENTITIES OF i_salesordertp
IN LOCAL MODE
ENTITY salesorder
ALL FIELDS WITH
CORRESPONDING #( event_parameters )
RESULT FINAL(sales_orders)
FAILED FINAL(failed)
REPORTED FINAL(reported).
" get so release details
DATA: sales_order_range TYPE RANGE OF zso_release_noti-sales_order.
sales_order_range = VALUE #(
FOR so IN sales_orders
sign = 'I' option = 'EQ'
( low = so-salesorder )
).
SELECT
FROM zso_release_noti
FIELDS *
WHERE sales_order IN @sales_order_range
INTO TABLE @DATA(so_release_details).
LOOP AT sales_orders INTO DATA(sales_order).
DATA(release_detail) = VALUE #( so_release_details[ sales_order = sales_order-salesorder ] OPTIONAL ).
" when SO Approval status is Released and no release details found
IF sales_order-salesdocapprovalstatus = zif_sales_order=>sales_doc_approval_status-released AND
release_detail IS INITIAL.
" retrieve PO Details
DATA(po_number) = sales_order-purchaseorderbycustomer.
DATA(po_date) = sales_order-customerpurchaseorderdate.
SELECT SINGLE
FROM i_user
FIELDS \_addrcurdefaultemailaddress-emailaddress
WHERE userid = @sales_order-createdbyuser
INTO @DATA(so_created_by_email).
SELECT SINGLE
FROM i_buspartemailaddresstp_3
FIELDS emailaddress
WHERE businesspartner = @sales_order-soldtoparty
INTO @DATA(sold_to_party_email).
so_created_by_email = 'akilk@deloitte.com'.
sold_to_party_email = 'sdas25@deloitte.com'.
DATA: mail_content TYPE REF TO cl_bcs_mail_textpart.
DATA(body_content) =
|<p>Dear Sir/Madam,</p>| && |<br/>| &&
|<br/>| &&
|<p>This email serves to acknowledge that we have received your purchase order.</p>| && |<br/>| &&
|<p>PO: { po_number }</p>| && |<br/>| &&
|<p>PO Date: { po_date }</p>| && |<br/>| &&
|<p>We sincerely thank you for your business and value your partnership.</p>| && |<br/>| &&
|<br/>| &&
|<p>Best Regards,</p><br/>| &&
|<p>Vinati Organics Limited</p>|.
mail_content = cl_bcs_mail_textpart=>create_text_html(
iv_content = body_content
).
* zcl_email_utility=>send_async(
* sender = CONV #( so_created_by_email )
* receivers = VALUE #(
* ( address = sold_to_party_email copy = cl_bcs_mail_message=>to )
* )
* subject = 'Purchase Order Acknowledgement Email'
* content = mail_content
* ).
DATA(lv_destination) = 'NONE'.
CALL FUNCTION 'Z_FM_SO_EMAIL'
DESTINATION lv_destination
EXPORTING
sender = so_created_by_email
receiver = sold_to_party_email
subject = 'Purchase Order Acknowledgement Email'
content = body_content.
DATA:
lo_operation TYPE REF TO if_bgmc_op_single,
lo_process TYPE REF TO if_bgmc_process_single_op,
lo_process_factory TYPE REF TO if_bgmc_process_factory,
lo_process_monitor TYPE REF TO if_bgmc_process_monitor,
lx_bgmc TYPE REF TO cx_bgmc.
lo_operation = NEW zcl_so_bgpf_operation_contr(
VALUE #(
sender = so_created_by_email
receivers = VALUE #(
( address = sold_to_party_email copy = cl_bcs_mail_message=>to )
)
subject = 'Purchase Order Acknowledgement Email'
content = mail_content
)
).
TRY.
lo_process_factory = cl_bgmc_process_factory=>get_default( ).
lo_process = lo_process_factory->create( ).
lo_process->set_name( |Email for SO Release of { sales_order-salesorder }| )->set_operation( lo_operation ).
* lo_process_monitor = lo_process->save_for_execution( ).
CATCH cx_bgmc INTO lx_bgmc.
DATA(lv_error_result) = lx_bgmc->get_longtext( ).
ENDTRY.
" create release details for future validation
" TODO: handle bgPF errors and update table accordingly
DATA(new_release_detail) = VALUE zso_release_noti(
is_released = abap_true
sales_order = sales_order-salesorder
).
INSERT zso_release_noti FROM @new_release_detail.
ENDIF.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
We have tried all the possible ways by calling the RFC using destination NONE but it gives an error.
Also we tried calling it using BGRFC and that also dump saying illegal statement encountered.
We commented the code for * lo_process_monitor = lo_process->save_for_execution( ). it avoided the dump but
email is not getting triggered. I know std would have a commit work calling later in the save phase but does not seem to be working.
Source codesnippet
Can somebody guide on what else cane be done to achieve this requirement. Thanks !
Request clarification before answering.
Try adding the following line before executing any statements that are not allowed in the modify phase.
cl_abap_tx=>save( ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
You can check the following:
1. You need to check the RFC destination, whether it can be called successfully. It could be that the destination is not maintained correctly.
2. You need to check the trigger separately. You can debug whether the trigger action can be triggered.
Best regards,
Dequan Xu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 7 | |
| 5 | |
| 5 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.