HCM Process and Forms: Send form as an attachment in E-mail at the end of the process
Applies to:
SAP ECC 6.0 EHP 4
Summary
SAP Administrative Services – Process & Forms component, offers an efficient way to manage cross-role processes involving HR Master data. Allows SAP customers to build and execute their own specific processes irrespective of data, process flow and roles involved. Complex HR processes takes time to complete and different stakeholders are involved in the process. At the end of process it is good idea to send an email to all stakeholders with read only PDF copy of the form with data/comments entered by all approvers. This article describes development approach taken for a similar customer requirement. This article assumes that the reader has basic understanding of HCM process and from framework, ABAP Objects and workflows.
Author: Parag Parikh
Company: Deloitte Consulting LLP
Created on: 24th March 2012
Author Bio
Parag Parikh is an SAP ABAP, SAP workflow consultant with 4.5 years of experience. He has extensively worked on SAP ABAP and SAP workflow. Parag also has functional skills for SAP FS-CD solution. He has worked on many SAP modules including SAP FI/CO, SD, MM, QM, PP, PLM-RM, SRM, HCM, ESS/MSS and EH&S. Parag is working with Deloitte Consulting LLP as Workflow Specialist.
Table of Contents
Getting the process instance: CL_HRASR00_WF_PROCESS_OBJECT.
Program Exit class CL_HRASR00_POBJ_WF_EXIT.
Methods to get form data and render PDF layout
Related Content
Copyright
Getting the process instance: CL_HRASR00_WF_PROCESS_OBJECT
Each HCM Process and Form workflow is controlled by an instance of class CL_HRASR00_WF_PROCESS_OBJECT. This class uses a unique case ID from SAP Case Management. Since each process workflow is triggered using event TRIGGERED of the class CL_HRASR00_WF_PROCESS_OBJECT, this process instance can easily be obtained in workflow using binding with triggering event.
All the data about the process, container values, notes and attachment are stored with respect the unique case ID by SAP Case Management.
Program Exit class CL_HRASR00_POBJ_WF_EXIT
Each dialogue step and process instance is in turn controlled by SAP class CL_HRASR00_POBJ_WF_EXIT. This class should is used as program exit for each step and workflow instance.
Each step of the process is in turn controlled class CL_HRASR00_WF_STEP_OBJECT.
At each step of the process, the agents enter/edit data in Adobe Interactive Form. This data is stored in back end Records Management system using methods of classes above. Note that the classes refer to fields of FORM SCENARIO and not the Adobe Form itself.
Method to get PDF layout in binary format:
Using the class hierarchy provided by SAP, we can easily write an ABAP method to render directly the Adobe Form in binary format along with the data entered till a particular step in process.
Shown below are the parameters of the method to get PDF content in BINARY format.
This is an example of a code sample for the method:
*–Data
TYPE-POOLS srmgs.
*–Local Objects
DATA:lo_pobj_runtime TYPE REF TO if_hrasr00_process_runtime.
*–Local Variables
DATA: l_authorized TYPE boole_d,
l_ok TYPE boole_d.
*–Internal Tables and Wokr Areas
DATA: lo_message_list TYPE REF TO if_hrbas_message_handler ,
ls_xstring TYPE xstring ,
ls_container TYPE xstring ,
lt_solix_tab TYPE solix_tab ,
ls_solix_tab LIKE LINE OF lt_solix_tab .
*–Constants
DATA:lc_check TYPE c LENGTH 1 VALUE ‘X’ ,
lc_r TYPE asr_activity VALUE ‘R’ . “Read Activity
*–Get instance of POBJ_RUNTIME
CLEAR:l_authorized,l_ok.
CALL METHOD cl_hrasr00_process_runtime=>get_instance
EXPORTING
scenario_guid = io_step_object->parent_scenario_guid
step_guid = io_step_object->guid
activity = lc_r
message_handler = lo_message_list
no_auth_check = lc_check
IMPORTING
instance_pobj_runtime = lo_pobj_runtime
is_authorized = l_authorized
is_ok = l_ok.
*–Get data container of the form
CLEAR ls_container.
CALL METHOD lo_pobj_runtime->get_latest_data_container
EXPORTING
scenario_guid = io_step_object->parent_scenario_guid
message_handler = lo_message_list
no_auth_check = lc_check
activity = lc_r
IMPORTING
data_container = ls_container
is_ok = l_ok
is_authorized = l_authorized.
*–Generate PDF as xstring
CALL METHOD cl_hrasr00_pdf_generation=>generate_pdf
EXPORTING
step_guid = io_step_object->guid
scenario_guid = io_step_object->parent_scenario_guid
message_handler = lo_message_list
form_scenario = io_step_object->scenario
data_container = ls_container
form_scenario_version = io_step_object->version
IMPORTING
xstring = ls_xstring
is_ok = l_ok.
*–Get binary content
CALL METHOD cl_bcs_convert=>xstring_to_solix
EXPORTING
iv_xstring = ls_xstring
RECEIVING
et_solix = lt_solix_tab.
*–This table can now directly be used as an Email attachment
et_pdf_xstring[] = lt_solix_tab[].
As shown below, from the workflow we can easily obtain the details of STEP_OBJECT from Process Form Task or any other SAP foreground task defined for HCM P&F.
Sample Code to attach this PDF to email:
Once the binary content of the form is with us, we can easily create this PDF as an attachment to any email using CL_BCS and its methods.
Below is one sample code to do so.
Import parameters can be adjusted as per requirements only parameter needed to fetch the PDF layout is STEP_OBJECT instance i.e. IO_STEP_OBJECT in example above. Content of the email like email text and logic to get sender and receiver email IDs cab be modified accordingly.
*–local variables
DATA: l_date TYPE c LENGTH 10 ,
l_pernr TYPE pa0001–pernr ,
l_admin TYPE so_sap_nam ,
l_email TYPE comm_id_long ,
l_usrid TYPE pa0105–pernr ,
l_att_name TYPE so_obj_des ,
l_ename1 TYPE pa0001–ename ,
l_ename TYPE pa0001–ename .
*–Data for sending E-mail
DATA: lo_send_request TYPE REF TO cl_bcs ,
lo_document TYPE REF TO cl_document_bcs ,
lo_sender TYPE REF TO cl_sapuser_bcs ,
lo_recipient TYPE REF TO if_recipient_bcs ,
lo_bcs_exception TYPE REF TO cx_root ,
lt_mail_body TYPE soli_tab ,
ls_content TYPE soli ,
lt_soli_tab TYPE solix_tab ,
ls_reciever TYPE ad_smtpadr ,
l_sub TYPE c LENGTH 50 ,
l_doc_len TYPE so_obj_len ,
l_cnt TYPE i ,
l_result TYPE os_boolean ,
l_text TYPE string .
*–Constants
CONSTANTS:lc_true TYPE c LENGTH 1 VALUE ‘X’.
***********************************************************************
* Fetch the DATA required *
******************************************************************
*–Get PERNR in internal format
CLEAR l_pernr.
l_pernr = i_pernr.
*–Get formatted name of employee
CLEAR l_ename.
SELECT ename
FROM pa0001
INTO l_ename
UP TO 1 ROWS
WHERE pernr = l_pernr
AND begda <= sy–datum
AND endda >= sy–datum.
ENDSELECT.
IF sy–subrc <> 0.
l_ename = i_pernr.
ENDIF.
*–Get formatted name of HR Administrator
CLEAR l_ename1.
SELECT ename
FROM pa0001
INTO l_ename1
UP TO 1 ROWS
WHERE pernr = i_pernr_admin
AND begda <= sy–datum
AND endda >= sy–datum.
ENDSELECT.
IF sy–subrc <> 0.
l_ename = i_pernr_admin.
ENDIF.
*–Get PDF attachment of the form
REFRESH lt_soli_tab.
CALL METHOD zhrcl_process_and_forms=>create_pdf_attachment
EXPORTING
io_step_object = io_step_object
CHANGING
et_pdf_xstring = lt_soli_tab.
***********************************************************************
* E-mail Contents *
******************************************************************
*–Create E-mail Body
REFRESH lt_mail_body.
*–HTML Header tag
CLEAR ls_content.
ls_content = ‘<html>’.
APPEND ls_content TO lt_mail_body.
*–Start of body
CLEAR ls_content.
ls_content = ‘<body>’.
APPEND ls_content TO lt_mail_body.
*–First paragraph
CLEAR ls_content.
ls_content = ‘<p style=”font-family:arial;font-size:12px;”>’.
APPEND ls_content TO lt_mail_body.
*–Date in MM/DD/YYYY format
CLEAR l_date.
WRITE i_init_date TO l_date MM/DD/YYYY.
*–This is to notify you that a Separation approval request for
* Employee <First Name Last Name> with Employee ID <#####> on
* <Date of submission – mm/dd/yyyy> was successfully approved by
* <Final Manager’s First Name Last Name> on <Date of final approval
* – mm/dd/yyyy> and was automatically processed in SAP.
CLEAR ls_content.
CONCATENATE text–017 l_ename text–002 l_pernr text–003 l_date
INTO ls_content SEPARATED BY space.
APPEND ls_content TO lt_mail_body.
CLEAR l_date.
WRITE sy–datum TO l_date MM/DD/YYYY.
CLEAR ls_content.
CONCATENATE text–019 l_ename1 text–003 l_date text–020
‘.’ ‘</p>’ INTO ls_content SEPARATED BY space.
APPEND ls_content TO lt_mail_body.
*–Add blank line
CLEAR ls_content.
ls_content = ‘<br />’.
APPEND ls_content TO lt_mail_body.
*–Add note on trigger from workflow
CLEAR ls_content.
CONCATENATE ‘<p style=”font-family:arial;font-size:12px;”>’ ‘( ‘
‘<strong>’ text–006 ‘</strong>’ text–005 ‘</p>’
INTO ls_content SEPARATED BY space.
APPEND ls_content TO lt_mail_body.
*–End of body and HTML
CLEAR ls_content.
ls_content = ‘</body>’.
APPEND ls_content TO lt_mail_body.
CLEAR ls_content.
ls_content = ‘</html>’.
APPEND ls_content TO lt_mail_body.
***********************************************************************
* E-mail Sending functionality *
******************************************************************
*–Create persistent object for CL_BCS class
TRY.
lo_send_request = cl_bcs=>create_persistent( ).
CLEAR l_text.
CATCH cx_send_req_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000(zhr_msg) WITH l_text RAISING error_sending_email.
ENDTRY.
l_sub = text–016. “Subject
*– Get the length of the line
*– Assumption: contents table will not be initial
CLEAR l_cnt.
DESCRIBE TABLE lt_mail_body LINES l_cnt.
READ TABLE lt_mail_body INTO ls_content INDEX l_cnt.
l_doc_len = ( l_cnt – 1 ) * 255 + STRLEN( ls_content ).
TRY.
*–Mail sent with high priority
lo_document = cl_document_bcs=>create_document(
i_type = ‘HTM’
i_text = lt_mail_body
i_length = l_doc_len
i_subject = l_sub
i_language = sy–langu
i_importance = ‘1’ ).
CLEAR l_text.
CATCH cx_document_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
*–Add document to send request
TRY.
CALL METHOD lo_send_request->set_document( lo_document ).
CLEAR l_text.
CATCH cx_send_req_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
*–Attach completed form
l_att_name = text–028.
TRY.
lo_document->add_attachment(
EXPORTING
i_attachment_type = ‘PDF’
i_attachment_subject = l_att_name
i_att_content_hex = lt_soli_tab ).
CATCH cx_document_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
***********************************************************************
* SENDER *
***********************************************************************
*–Set the default sender name if the sender_ID is initial.
TRY.
lo_sender = cl_sapuser_bcs=>create( sy–uname ).
CLEAR l_text.
CATCH cx_address_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
TRY.
CALL METHOD lo_send_request->set_sender
EXPORTING
i_sender = lo_sender.
CLEAR l_text.
CATCH cx_send_req_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
***********************************************************************
* RECIEVER *
******************************************************************
*–Get latest Email ID of HR Admin from IT0105
*–First fetch PERNR from PA0105
CLEAR:l_usrid,l_email.
SELECT pernr
FROM pa0105
INTO l_usrid
UP TO 1 ROWS
WHERE subty = ‘0001’
AND begda <= sy–datum
AND endda >= sy–datum
AND usrty = ‘0001’
AND usrid = i_initiator+2(12).
ENDSELECT.
IF sy–subrc <> 0.
MESSAGE e226 WITH i_initiator+2(12) RAISING error_sending_email.
ENDIF.
*–Get email ID
SELECT usrid_long
FROM pa0105
INTO l_email
UP TO 1 ROWS
WHERE pernr = l_usrid
AND subty = ‘0010’
AND begda <= sy–datum
AND endda >= sy–datum
AND usrty = ‘0010’.
ENDSELECT.
IF sy–subrc <> 0.
EXIT. “No email if not Email Id maintained
ENDIF.
CLEAR ls_reciever.
ls_reciever = l_email. “Email ID of HR administrator
TRY.
*–Get instance of recipient E-mail address
lo_recipient
= cl_cam_address_bcs=>create_internet_address( ls_reciever ).
CLEAR l_text.
CATCH cx_address_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
TRY.
CALL METHOD lo_send_request->add_recipient
EXPORTING
i_recipient = lo_recipient
i_express = abap_true.
CLEAR l_text.
CATCH cx_send_req_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
***********************************************************************
* Send E-mail *
******************************************************************
TRY.
*–Send E-mail immidietly
lo_send_request->set_send_immediately( abap_true ).
CLEAR l_text.
CATCH cx_send_req_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
*–Send email
TRY.
CALL METHOD lo_send_request->send(
RECEIVING
result = l_result ).
CLEAR l_text.
CATCH cx_send_req_bcs INTO lo_bcs_exception.
CALL METHOD lo_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e000 WITH l_text RAISING error_sending_email.
ENDTRY.
*–Mail sent succesfully, commit work
IF l_result = abap_true.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = abap_true.
ENDIF.
Shown below is the sample Email Generated with the PDF form as attachment.
We see that in the attached file. All the details filled in by agents till the particular process step is available. The class methods described above can be reused in any ABAP program or as single step task defined from PFTC as per requirements with necessary amendments if needed.
Related Content
Below links are useful to understand HCM Process and Forms framework technically as well as functionally.
http://help.sap.com/saphelp_erp60_sp/helpdata/en/42/f273461e5132c3e10000000a1553f6/content.htm
http://help.sap.com/saphelp_erp60_sp/helpdata/en/a6/dd55f9f7cb5740833f58118e6420ba/frameset.htm
http://help.sap.com/saphelp_erp60_sp/helpdata/en/42/f273461e5132c3e10000000a1553f6/frameset.htm
Copyright
© Copyright 2012 SAP AG. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.
Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation.
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries.
Oracle is a registered trademark of Oracle Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc.
HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.
Java is a registered trademark of Oracle Corporation.
JavaScript is a registered trademark of Oracle Corporation, used under license for technology invented and implemented by Netscape.
SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries.
Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company.
All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary.
These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies (“SAP Group”) for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.
I was asked recently by a colleague that is it possible to use the same approach with HR renewals to send FPM based forms adobe copy as Email attachment?
Theoretically if the adobe form layout is defined for the form scenario in addition to FPM based form , this should work.
Will keep the document updated with the findings soon.
Thanks Parag for this article! I was able to use some of it for our needs. We're archiving the HCM P&F PDFs into ArchiveLink after WF completes. This becomes part of employee's EPF (Electronic Personnel File) for HR user to view directly from PA20.
Appreciated it!
Ashish.