Skip to Content

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)

/wp-content/uploads/2013/07/1_246179.jpg

  • Call transaction SE38 and create a sample program – Z_MASS_INV_UPLOAD with following options:

/wp-content/uploads/2013/07/2_246180.jpg

  • 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.

/wp-content/uploads/2013/07/3_246181.jpg

  • 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.

/wp-content/uploads/2013/07/4_246182.jpg

  • 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.

/wp-content/uploads/2013/07/5_246183.jpg/wp-content/uploads/2013/07/6_246184.jpg

    • 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’

/wp-content/uploads/2013/07/7_246185.jpg

  • 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

/wp-content/uploads/2013/07/8_246186.jpg

  • Change Object Release status to ‘Implemented’

/wp-content/uploads/2013/07/9_246188.png

  • Save and Generate object type ‘ZMASSINV’

/wp-content/uploads/2013/07/10_246189.jpg

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.

/wp-content/uploads/2013/07/11_246190.jpg

  • Choose any numerical field e.g. – SYST-SPONO as data type reference

/wp-content/uploads/2013/07/12_246191.jpg

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.

/wp-content/uploads/2013/07/13_246192.jpg

  • Place cursor on event and change its release status to implemented

/wp-content/uploads/2013/07/14_246193.jpg

  • Create Event Parameters

Place cursor on event and choose ‘Parameters’.

/wp-content/uploads/2013/07/15_246194.jpg/wp-content/uploads/2013/07/16_246195.jpg

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.

/wp-content/uploads/2013/07/17_246197.jpg

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.

/wp-content/uploads/2013/07/18_246198.jpg/wp-content/uploads/2013/07/19_246199.jpg

Create the same import parameter ‘UploadedDoc’ for this method

/wp-content/uploads/2013/07/20_246200.jpg

Place cursor on Method and Click on Program

/wp-content/uploads/2013/07/21_246201.jpg/wp-content/uploads/2013/07/22_246203.jpg

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’

/wp-content/uploads/2013/07/23_246204.jpg

end_method.


Create Custom Approval Workflow


  • Call transaction SWDD – Workflow Builder. Click Save and provide name for the Workflow template

/wp-content/uploads/2013/07/24_246206.jpg

  • Create Workflow Container variable – ‘UploadedDoc’ of type MESSAGE.

/wp-content/uploads/2013/07/25_246207.jpg

  • Create another Container Variable ‘MassUpload’ of type ZMASSINV

/wp-content/uploads/2013/07/26_246208.jpg

Set both these variables as ‘Import’ parameters for the Workflow container.

/wp-content/uploads/2013/07/tt_246235.jpg/wp-content/uploads/2013/07/28_246210.jpg

  • Click on the Red-hat icon.

/wp-content/uploads/2013/07/29_246211.jpg

  • Set up triggering event for this workflow as ZMASSINV- MASSAPPROVALREQUESTED

/wp-content/uploads/2013/07/30_246212.jpg

  • 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.

/wp-content/uploads/2013/07/31_246213.jpg

/wp-content/uploads/2013/07/32_246214.jpg

  • Activate the event linkage.

This will cause a workflow instance to be triggered every time a Batch of invoices is Uploaded.

/wp-content/uploads/2013/07/33_246215.jpg

  • Add ‘Uploaded Document’ object to Workflow Attachments

Double Click on ‘Undefined Step’ and choose step type as ‘Container Operation’

/wp-content/uploads/2013/07/34_246216.jpg

Append Uploaded document to Attachments table. This will cause the Document to appear within the Approval Decision step.

/wp-content/uploads/2013/07/35_246217.jpg

  • Right click on the outcome branch ‘Doc Attached’ and select ‘Create’ – User Decision Step.

/wp-content/uploads/2013/07/36_246218.jpg

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

/wp-content/uploads/2013/07/37_246219.jpg

Create 2 new variables within Workflow Container – ‘Rejection reason’ and ‘Reviewer’

/wp-content/uploads/2013/07/38_246220.jpg/wp-content/uploads/2013/07/39_246221.jpg

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

/wp-content/uploads/2013/07/40_246222.jpg

  • Right click on the outcome branch ‘Approved’ and select ‘Create’ – Activity Step

/wp-content/uploads/2013/07/41_246223.jpg

Choose Create task

/wp-content/uploads/2013/07/42_246224.jpg

/wp-content/uploads/2013/07/43_246225.jpg

The ‘Uploaded Document’ object must be passed to the Task Container from Workflow Container.

/wp-content/uploads/2013/07/44_246226.jpg

  • Right click on the outcome branch ‘Rejected’ and select ‘Create’ – Send mail Step

/wp-content/uploads/2013/07/45_246227.jpg

Insert variables from Workflow container to make the Email text more meaningful.

/wp-content/uploads/2013/07/46_246228.jpg

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.

/wp-content/uploads/2013/07/47_246229.jpg

Working DEMO


  • User executes custom demo program Z_MASS_INV_UPLOAD, uploads a CSV file containing invoice records and hits ‘Submit for Approval’

/wp-content/uploads/2013/07/48_246230.jpg

  • The Workflow gets triggered and Approval item is routed to the Manager of the User who uploaded the document.

/wp-content/uploads/2013/07/49_246231.png

  • The Manager can view the following Decision step by executing this Work item. The uploaded file is available to review as an attachment.

/wp-content/uploads/2013/07/50_246232.jpg

  • If the Manager chooses to Reject, a pop up screen prompts for a Rejection reason.

/wp-content/uploads/2013/07/51_246233.png

  • 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.

/wp-content/uploads/2013/07/52_246234.jpg

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply