Custom Approval Workflow for Mass Invoices
Business Scenario:
There are thousands of low-value, yet high-volume vendor invoices that need to be posted and paid in an efficient and timely manner. Any effort to manually POST these within SAP using Enjoy transaction is very time consuming and labor intensive. In addition to this, it is required that all these invoices obtain Approval through workflow before Posting.
So end users should be able to manually upload invoices in an Excel format using a custom program. The invoice data should be validated and then sent for Approval. Managers should be able to review and approve/reject these invoices. The invoices should be posted in SAP after Approval. In case of Error or rejection, SAP email notifications should be generated to the End user.
This document provides a custom solution for developing Approval Workflow for Mass Invoices.
Benefits:
- Approval and Audit trail
- Timey payout to Vendors
- No dependency on any external interfaces
Create a custom program for uploading an Excel/CSV file containing Vendor Invoice records
Excel file can be saved as CSV (Comma Delimited) file. Invoice Data from CSV file upload can either be staged in Data tables for tracking and auditing purposes. This solution directly processes the Invoice upload and sends it for Approval.
Sample template for Excel/CSV file (more fields can be included based on requirement)
- Call transaction SE38 and create a sample program – Z_MASS_INV_UPLOAD with following options:
- Sample Code for file Upload.
This action opens a Browse window for selecting a File stored on local hard drive and saves it as a SAP office document. The Document ID is available within ‘documents’ table.
* Data declaration
data:
documents TYPE STANDARD TABLE OF sood4,
document TYPE sood4,
folder_id TYPE sofdk.
free documents.
CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’
EXPORTING
region = ‘B’
IMPORTING
folder_id = folder_id
EXCEPTIONS
OTHERS = 0.
document-foltp = folder_id-foltp.
document-folyr = folder_id-folyr.
document-folno = folder_id-folno.
APPEND document TO documents.
* Upload file as SAP Office doc
CALL FUNCTION ‘SO_DOCUMENTS_MANAGER’
EXPORTING
activity = ‘IMPO’
TABLES
documents = documents.
- Sample Code for viewing Uploaded file.
This action retrieves the document by instantiating business object of type ‘MESSAGE’ and calling its ‘DISPLAY’ method.
TYPES: BEGIN OF typ_message_key
foltp TYPE so_fol_tp, “Object folder: Object type from ID
folyr TYPE so_fol_yr, “Object folder: Year from ID
folno TYPE so_fol_no, “Object folder: Number from ID
doctp TYPE so_doc_tp, “Object: Object Type From ID
docyr TYPE so_doc_yr, “Object: Year from ID
docno TYPE so_doc_no, “Object: Number from ID
fortp TYPE so_for_tp, “Forwarder: Object type from ID
foryr TYPE so_for_yr, “Forwarder: year from the ID
forno TYPE so_for_no, “Forwarder: number from the ID
END OF typ_message_key.
DATA :
l_message_key TYPE typ_message_key,
l_object_key TYPE swotobjid-objkey,
l_message TYPE swc_object,
READ TABLE documents INDEX 1 INTO document.
l_message_key-foltp = document-foltp.
l_message_key-folyr = document-folyr.
l_message_key-folno = document-folno.
l_message_key-doctp = document-objtp.
l_message_key-docyr = document-objyr.
l_message_key-docno = document-objno.
swc_container l_message_container.
l_object_key = l_message_key.
swc_create_object l_message ‘MESSAGE’ l_object_key.
swc_call_method l_message ‘Display’ l_message_container.
- Sample Code for triggering workflow
This action raises ‘MassApprovalRequested’ event of Business object ‘ZMASSINV’ (created in next step)
- Create number range ZBATCHNO for Batch Number.
- Generate new Batch number and trigger approval workflow
DATA :
l_bo_objkey LIKE sweinstcou-objkey,
l_bo_obj_type TYPE swotobjid-objtype,
l_event TYPE swetypecou-event,
l_batch_no TYPE numc10.
DATA :
it_swcont TYPE STANDARD TABLE OF swcont INITIAL SIZE 0.
* Generate New Batch number
CALL FUNCTION ‘NUMBER_GET_NEXT’
EXPORTING
nr_range_nr = ’01’
object = ‘ZBATCHNO’
IMPORTING
number = l_batch_no
EXCEPTIONS
interval_not_found = 1
number_range_not_intern = 2
object_not_found = 3
quantity_is_0 = 4
quantity_is_not_1 = 5
interval_overflow = 6
OTHERS = 7.
l_bo_objkey = l_batch_no.
l_bo_obj_type = ‘ZMASSINV’.
l_event = ‘MassApprovalRequested’.
* Pass document details to the workflow as Event parameter
READ TABLE documents INDEX 1 INTO document.
l_message_key-foltp = document-foltp.
l_message_key-folyr = document-folyr.
l_message_key-folno = document-folno.
l_message_key-doctp = document-objtp.
l_message_key-docyr = document-objyr.
l_message_key-docno = document-objno.
* Instantiate Object of type Message
swc_container l_message_container.
l_object_key = l_message_key.
swc_create_object l_message ‘MESSAGE’ l_object_key.
* Transfer Message object to Workflow
swc_set_element it_swcont ‘UploadedDoc’ l_message.
* Raise Event to trigger workflow.
CALL FUNCTION ‘SWE_EVENT_CREATE’
EXPORTING
objtype = l_bo_obj_type
objkey = l_bo_objkey
event = l_event
TABLES
event_container = it_swcont
EXCEPTIONS
objtype_not_found = 1
OTHERS = 2.
IF sy-subrc EQ 0.
COMMIT WORK.
ENDIF.
Create a Custom Business Object – ZMASSINV
- Call transaction SWO1 (Business Object Builder) and create object type ‘ZMASSINV’
- Provide Object name, Description and underlying program name. Supertype field is blank since this object type is not inherited from any standard SAP business object type
- Change Object Release status to ‘Implemented’
- Save and Generate object type ‘ZMASSINV’
Create Key field
For the purpose of demo, we can use a numerical field – Batch number as key field. This number uniquely identifies each upload attempt.
- Place cursor on ‘Key Fields’ and click ‘Create’. On the subsequent popup screen – choose ‘No’ , since this field does not reference any Database table.
- Choose any numerical field e.g. – SYST-SPONO as data type reference
Create Custom Event
- Place cursor on ‘Events’ and click ‘Create’. On the subsequent popup screen – provide event name ‘MassApprovalRequested’. This event will serve as Triggering event for the Mass Invoice Approval Workflow.
- Place cursor on event and change its release status to implemented
- Create Event Parameters
Place cursor on event and choose ‘Parameters’.
Next click ‘Create’ and specify parameter name as ‘UploadedDoc’ of object type MESSAGE. This event parameter represents the Uploaded excel file that is stored as SAP office document.
Create Custom Method
- Post Invoices after Approval
Place cursor on ‘Method and click ‘Create’. On the subsequent popup screen – provide method name ‘PostMassUpldInv’. This is a background method.
Create the same import parameter ‘UploadedDoc’ for this method
Place cursor on Method and Click on Program
Sample method code
begin_method postmassupldinv changing container.
DATA:
uploadeddoc TYPE swc_object,
lt_content_hex LIKE solix OCCURS 0 WITH HEADER LINE,
lt_text_data TYPE STANDARD TABLE OF soli,
l_doc_size TYPE i,
l_obj_len TYPE so_obj_len,
l_obj_key TYPE swotobjid-objkey,
BEGIN OF l_key,
objtp TYPE so_obj_tp,
objyr TYPE so_obj_yr,
objno TYPE so_obj_no,
END OF l_key.
swc_get_element container ‘UploadedDoc’ uploadeddoc.
*Retrieve Contents of the document
swc_call_method uploadeddoc ‘ReadDocumentAttributes’ container.
swc_get_table container ‘Content_Hex’ lt_content_hex .
*Retrieve second half of Object key
swc_get_object_key uploadeddoc l_obj_key.
l_key = l_obj_key+17(17).
*Determine Document size
SELECT SINGLE objlen
INTO l_obj_len
FROM sood
WHERE objtp = l_key-objtp
AND objyr = l_key-objyr
AND objno = l_key-objno.
l_doc_size = l_obj_len.
* Conver to Text
CALL FUNCTION ‘SCMS_BINARY_TO_TEXT’
EXPORTING
input_length = l_doc_size
TABLES
binary_tab = lt_content_hex
text_tab = lt_text_data
EXCEPTIONS
failed = 1
OTHERS = 2.
* At this point – lt_text_data contains Invoice data in a tab delimited format. This data can be processed and individual invoices can be posted using function ‘BAPI_ACC_INVOICE_RECEIPT_POST’
end_method.
Create Custom Approval Workflow
- Call transaction SWDD – Workflow Builder. Click Save and provide name for the Workflow template
- Create Workflow Container variable – ‘UploadedDoc’ of type MESSAGE.
- Create another Container Variable ‘MassUpload’ of type ZMASSINV
Set both these variables as ‘Import’ parameters for the Workflow container.
- Click on the Red-hat icon.
- Set up triggering event for this workflow as ZMASSINV- MASSAPPROVALREQUESTED
- Setup Data Binding from Event container to Workflow Container. The Event object of type ‘ZMASSINV’ and Uploaded Document of type ‘MESSAGE’ must be transferred to the Workflow Container. The Event Creator is the SAP User name of the person who submits Mass invoices for approval.
- Activate the event linkage.
This will cause a workflow instance to be triggered every time a Batch of invoices is Uploaded.
- Add ‘Uploaded Document’ object to Workflow Attachments
Double Click on ‘Undefined Step’ and choose step type as ‘Container Operation’
Append Uploaded document to Attachments table. This will cause the Document to appear within the Approval Decision step.
- Right click on the outcome branch ‘Doc Attached’ and select ‘Create’ – User Decision Step.
Set Work item text as ‘Review Mass Invoice Upload Batch’. Use standard agent determination Rule (get superior) to route approval workflow to Manager of the User who uploads the Batch of Invoice. Set two outcomes of review process – Approve and Reject
Create 2 new variables within Workflow Container – ‘Rejection reason’ and ‘Reviewer’
Click on User Decision –> Control tab -> Binding and retrieve the User name of the Reviewer (Manager) and the Rejection Reason from Task container back to Workflow Container
- Right click on the outcome branch ‘Approved’ and select ‘Create’ – Activity Step
Choose Create task
The ‘Uploaded Document’ object must be passed to the Task Container from Workflow Container.
- Right click on the outcome branch ‘Rejected’ and select ‘Create’ – Send mail Step
Insert variables from Workflow container to make the Email text more meaningful.
Go to ‘Control’ tab -> Binding within Email step and pass Attachments from Workflow Container to Email step – task container so these attachments can be viewed from the Email notification.
Working DEMO
- User executes custom demo program Z_MASS_INV_UPLOAD, uploads a CSV file containing invoice records and hits ‘Submit for Approval’
- The Workflow gets triggered and Approval item is routed to the Manager of the User who uploaded the document.
- The Manager can view the following Decision step by executing this Work item. The uploaded file is available to review as an attachment.
- If the Manager chooses to Reject, a pop up screen prompts for a Rejection reason.
- Following Email is sent back to the User who submitted the Batch of Mass invoices for Approval. The original Uploaded file and the Rejection reason are both available as Email Attachments.
Nice detailed step by step solution.
very nice custom workflow solution. thanks for all the details
Nice posting.