Friday 20 January 2023

Purchase Order Email with PDF Attachment On Approval

The article will let you go through purchase order email when an approval workflow is triggered at the time of release purchase order.

This article will include following:

1. Purchase Order PDF Conversion

2. Create HTML Content of Purchase Order Email

3. Send Purchse Order Email with Purchase Order Document in PDF format

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Learning

Purchase Order Email with PDF Attachment in SOST

1. Purchase Order PDF Conversion


The purchase order document you created can be in Adobe PDF Form or in Smst Forms.  If this is a smartform, you have to Convert Purchase Order Document into PDF.

Once it is available in PDF contents, you will be able to send the document as Email Attachment.

For converting SmartForm, you may have many best code snippets from SAP Wiki as well.

Here is the shorts of Code Snippet:

"Control Parameters
ls_ctrlop-getotf    = 'X'.
ls_ctrlop-no_dialog = 'X'.
ls_ctrlop-preview   = space.

"Output Options
ls_outopt-tdnoprev  = 'X'.
ls_outopt-tddest    = 'LOCL'.
ls_outopt-tdnoprint = 'X'.

"Get Function Name using FM :  'SSF_FUNCTION_MODULE_NAME'

"Convert Smartform to DF contents

CALL FUNCTION lv_fname
EXPORTING

control_parameters = ls_ctrlop
output_options     = ls_outopt
smmdcdta_po        = smmdcdta_po
smmcondv_po        = sconditionvalues
"output_options = output_options

po_head            = po_head

IMPORTING
job_output_info    = lt_otfdata

TABLES
it_items_po        = it_items_po   .

lt_otf[ ]  =  lt_otfdata-otfdata[ ] .

CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format              = 'PDF'
IMPORTING
bin_filesize       = lv_bin_filesize
bin_file             = lv_bin_xstr
TABLES
otf                     = lt_otf[]
lines                 = lt_pdf_tab[]
EXCEPTIONS
err_max_linewidth     = 1
err_format            = 2
err_conv_not_possible = 3
OTHERS                = 4.

"***Xstring to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer     = lv_bin_xstr
TABLES
binary_tab = lt_binary_content.
 
"" lt_binary_content   
"" is very important since it has the PDF Contents of Smartform

IF Adobe PDF form is directly created for Purch. Order Document, then you can use directly that with its content in  lt_binary_content.

2. Create HTML Content of Purchase Order Email:


The following code snippet will let you have an email contents in HTML format.

The HTML format allows you Beautify the Email Content you are going to send respective receivers. Those receivers can be Internal Management, and can be external Vendor.

You can use the following macro in the code to create the HTML Content in a better readable contents:

:Add the following within the program lines:

DATA:
emailsubject       TYPE char80,
it_message         TYPE STANDARD TABLE OF solisti1,
wa_it_message      LIKE LINE OF it_message.

CLEAR: it_message[], wa_it_message.

DEFINE add_html.
wa_it_message-line = &1.
APPEND wa_it_message TO it_message. CLEAR wa_it_message.
END-OF-DEFINITION.

Now, create the body of the email:

add_html:

'<html>',
'<body>',
'<p><b>Dear concern,</b></p>'.

 CASE email_level.
WHEN 'B'.
add_html:
'<p>Approval of the following Purchase Order is <b>REQUESTED</b>, kindly appprove it. Thanks.'.  "by <b style="color:black;font-size:12px;" >&nbsp;', approver_name, '</b>.</p>'.
WHEN OTHERS.
add_html:
'<p>This is to inform you that following Purchase Order has been <b>APPROVED</b> by <b style="color:black;font-size:12px;" >&nbsp;', approver_name, '</b>.</p>'.
ENDCASE.

add_html:

'<br/>',
'<table >',
'  <tbody>',
'    <tr>',   "#ffcc00
'      <td style="background-color:#EE82EE;font-size:12px;border-width: 2px;border-style: solid;padding: 5px;" ><b>Purchase Order</b></td>',
'      <td style="color:blue;font-size:14px;border-width: 2px;border-style: solid; padding: 5px;" ><b>', purch_order_no, '</b></td>',
'    </tr>',
'      <td style="background-color:#EE82EE;font-size:12px;border-width: 2px;border-style: solid;padding: 5px;" ><b>Vendor</b></td>',
'      <td style="font-size:12px;border-width: 2px;border-style: solid;padding: 5px;" >', purch_order-vndrname, '</td>',
'    </tr>',
'    <tr>',   "#ffcc00
'      <td style="background-color:#EE82EE;font-size:12px;border-width: 2px;border-style: solid;padding: 5px;" ><b>Order Net Value:</b></td>',
'      <td style="font-size:12px;border-width: 2px;border-style: solid; padding: 5px;" >', purch_order-currency, '&nbsp;', char_po_value, '</td>',
'    </tr>',
'  </tbody>',
'</table>'.

add_html:

'  </tbody>',
'</table>'.

add_html:
'</br>',
'</br>',
'<p><em><i>A System generated notification.</i></em></p>',

"'<img src="http://www.sap.com/dam/application/shared/logos/sap-logo-svg.svg/sap-logo-svg.svg"></img>',
"'<img src="http://www.mitchells.com.pk/wp-content/uploads/2018/05/strawberry-squash.png" style="max-width:100%;">',
'<img src="http://www.anysite.com/wp-content/themes/images/logo.jpg" style="max-width:100%;">', 

'</body>',
'</html>'.

3. Create Email with Purchase Order Document attached in PDF format


You will have to have PDF binary content in a container of type solix_tab.

e.g. Data: lt_binary_content TYPE solix_tab.

DATA: main_text      TYPE bcsy_text,
lt_mailsubject TYPE sodocchgi1,
mailsubject    TYPE so_obj_des,
attchsubject   LIKE sood-objdes.

DATA: 

lv_size TYPE i.
mailsubject    = psubject    .

DATA(send_request) = cl_bcs=>create_persistent( ).
DATA(email_document) = cl_document_bcs=>create_document( i_type    = 'HTM'
i_text    = it_message
i_subject = mailsubject
).
DATA lt_att_head TYPE soli_tab.
"APPEND '<(>&< )>SO_FILENAME=Prob_Compl_Emps.xlsx' TO lt_att_head.
APPEND 'Purchase_Order.pdf' TO lt_att_head.
"-----------------------------------------------------------------
"" add the spread sheet as attachment to document object
attchsubject = `Purchase_Order.pdf` && purch_order_no && '_' && sy-datum+4(2) && '_' && sy-datum(4).
attchsubject = `Purchase_Order_` && purch_order_no .
APPEND attchsubject TO lt_att_head.

email_document->add_attachment(
i_attachment_type    = 'pdf'
i_attachment_subject = attchsubject
i_attachment_size    = CONV so_obj_len( lv_size )
i_attachment_header  = lt_att_head
i_att_content_hex    = lt_binpdf_content ).

send_request->set_document( email_document ).
"Recipients
DATA sender TYPE REF TO cl_cam_address_bcs.
DATA recipient TYPE REF TO cl_cam_address_bcs.
DATA: saprecipient          TYPE REF TO if_recipient_bcs.
DATA: emailid TYPE ad_smtpadr.
sender = cl_cam_address_bcs=>create_internet_address( 'donotreply@mail.com' ).
send_request->set_sender( sender ).

recipient = cl_cam_address_bcs=>create_internet_address( 'proc_manager@mail.com' ).
send_request->add_recipient( recipient ).

TRY.

*      CATCH cx_send_req_bcs. " BCS: Send Request Exceptions.
DATA(sent_to_all) = send_request->send(  i_with_error_screen = 'X' ).
CATCH cx_root .
"bcs_exception->get_text().
ENDTRY.
 
Once Email is sent from the program, email contents and attchment itself can be reviewed in the TCod: SOST.

The above image at the start of blog preview this email contents with Rich HTML contents.

You can add up the Company logo as well at the end of email with the URL.

No comments:

Post a Comment