Skip to end of metadata
Go to start of metadata

Purpose

Using function BAPI_CATIMESHEET_INSERT to insert records into database table CATSDB.

Overview

Handling BAPI_CATIMESHEET_INSERT to avoid common errors and overcoming its restriction of not updating any record passed in case even one of the records is having an error. 

Updating database table CATSDB with BAPI_CATIMESHEETMGR_INSERT

1. Function module BAPI_CATIMESHEETMGR_INSERT is used to update records in table CATSDB (CATS: Database Table for Time Sheet). Refer to the first part of the code in the coding section below.

2. As given in the documentation of this BAPI, it updates all the records passed in internal table MI_RECIN only if all the records are error free. (An e.g. of error can be an employee assigned to a wrong work center.)  Even if one of the records is having error data, none of the records in the internal table gets updated. It’s also recommended to pass records in small chunks.

3. To overcome this limitation, there can be several ways :
Few common error checks can be performed on the records before sending them to BAPI. Very general errors: Employee no. not present in the record, Work date not being present, Either STARTTIME  and ENDTIME should be populated or CATSHOURS should have a value. Employee being assigned to incorrect Work Center etc.

4. CATS accept time duration in hours by default. To store in any other UOM (Unit of Measurement) implicit enhancement / user exit has to be implemented to be highlighted in subsequent parts of this wiki. So , without enhancement if time duration (CATSHOURS) is less than 36 seconds , record will not be stored and an error like ‘Record does not specify number of hours’ gets thrown.

5. If STARTTIME and ENDTIME are provided, CATS calculates the duration (CATSHOURS) and need not be calculated in the custom program.

6 a) There is an option of test run as well in the BAPI. So, let’s say we pass a number of records at a time to the BAPI with test run set to ‘X’.  If there lies error in any of the record, RETURN would contain the error message. Also, ME_RECOUT will have no records.  In case there is no error, RETURN will have no records and ME_RECOUT will have the same no. of records as passed in MI_RECIN. In this case, all the records can be sent for update.

b) However, in case of error, returned message does not specify which record has the error. In this case, depending on the acceptance, either records can be updated one by one or an algorithm can be developed to exactly find out the error record, exclude it from the list and update the rest.Handling of error record depends on the design. Second part of the coding section contains the algorithm.

NOTE:It should be noted that whether BAPI accepts multiple employee or single employee for update at a time depends on the setting done in Data Entry Profile.
Records to be updated should be mapped to structure BAPICATS1 and not CATSDB.

//This is a code sample block

*C--Call the BAPI to insert records into CATSDB
  CALL FUNCTION 'BAPI_CATIMESHEETMGR_INSERT'
   EXPORTING
     profile               = mi_data_profile
     testrun               = mi_testrun
    TABLES
      catsrecords_in        = mi_recin
      extensionin           = mi_extension_in
      catsrecords_out       = me_recout
      extensionout          = me_extension_out
      return                = me_message
      longtext              = mi_longtext     

 
Algorithm to separate the error record from the set of records and then pass only the valid records to the BAPI.This is a crude coding,only for the purpose of algorithm.

*&---------------------------------------------------------------------*
*& Report  ZTEST_BAPI_CATS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ztest_bapi_cats.
CLASS lcl_cats DEFINITION DEFERRED.
DATA: obj TYPE REF TO lcl_cats,
      num TYPE i,
      n1 TYPE i,
      n2 TYPE i,
      t_passrec TYPE STANDARD TABLE OF bapicats1.

DATA: tl_recin TYPE STANDARD TABLE OF bapicats1,
      wl_recin TYPE bapicats1,
      wl_bapicats1 TYPE bapicats1.

REFRESH: t_passrec[].
*----------------------------------------------------------------------*
*       CLASS lcl_cats DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_cats DEFINITION.
  PUBLIC SECTION.

    DATA: tl_recout TYPE STANDARD TABLE OF bapicats2,
          tl_message TYPE STANDARD TABLE OF bapiret2,
          tl_chkdata  TYPE STANDARD TABLE OF bapicats1,
          tl_rec2data TYPE STANDARD TABLE OF bapicats1.

    METHODS: transfer_data IMPORTING tl_data TYPE ycatt_bapicats1,
            distribute_data.
ENDCLASS.                    "lcl_cats DEFINITION

START-OF-SELECTION.
  REFRESH: tl_recin[].
  CLEAR:   wl_recin.
  CREATE OBJECT obj.

*C--Tested the algorithm by manually appending 25 records.
*C--This should be replaced as per the individual requirement
*C--Dummy coding to append records
  wl_bapicats1-employeenumber = '00956753'.
  wl_bapicats1-workdate = '20120110'.
  wl_bapicats1-catshours = '2.00'.
  wl_bapicats1-abs_att_type = '9P01'.
  APPEND wl_bapicats1 TO tl_recin.

  wl_bapicats1-employeenumber = '00956753'.
  wl_bapicats1-workdate = '20120107'.
  wl_bapicats1-send_cctr = '0109575002'.
  wl_bapicats1-acttype = 'DTOIND'.
  wl_bapicats1-rec_order = '006000015807'.
  wl_bapicats1-wagetype = 'MI10'.
  wl_bapicats1-catshours = '3.00'.
  APPEND wl_bapicats1 TO tl_recin.

   [.......]


*C--Fill in the data for 25 records now
  CALL METHOD obj->transfer_data
    EXPORTING
      tl_data = tl_recin.
*----------------------------------------------------------------------*
*       CLASS lcl_cats IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_cats IMPLEMENTATION.
  METHOD transfer_data.
    DATA: me_return TYPE STANDARD TABLE OF bapiret2.
    REFRESH: tl_recout,
             tl_message,
             tl_chkdata,
             me_return.

    CALL FUNCTION 'BAPI_CATIMESHEETMGR_INSERT'
 EXPORTING
   profile               = 'YCATUS'
   testrun               = 'X'
  TABLES
    catsrecords_in        = tl_data
*      extensionin           = mi_extension_in
    catsrecords_out       = tl_recout
*      extensionout          = me_extension_out
    return                = tl_message.
*      longtext              = mi_longtext.
    me_return = tl_message[].
    tl_chkdata = tl_data[].
    distribute_data( ).
  ENDMETHOD.                    "transfer_data

  METHOD distribute_data.
    DATA:  tl_recin1 TYPE STANDARD TABLE OF bapicats1,
           tl_recin2 TYPE STANDARD TABLE OF bapicats1,
           tl_rec3data TYPE STANDARD TABLE OF bapicats1.
    REFRESH: tl_recin1[].
    IF tl_message IS NOT INITIAL.
      DESCRIBE TABLE tl_chkdata LINES num.
      IF tl_message IS NOT INITIAL.
        n1 = num DIV 2.
        n2 = n1 + 1.
*C--Check the ending criteria
        CHECK n1 IS NOT INITIAL.
*C--Form two tables of records
*C--First half of internal table
        LOOP AT tl_chkdata INTO wl_recin FROM 1 TO n1.
          APPEND wl_recin TO tl_recin1.
        ENDLOOP.
*C--Second half of internal table
        LOOP AT tl_chkdata INTO wl_recin FROM  n2  TO num.
          APPEND wl_recin TO tl_recin2.
        ENDLOOP.
      ENDIF.
*C--Append this in public variable of data
      APPEND LINES OF tl_recin2 TO tl_rec2data.
*C--Call BAPI update
      CALL METHOD obj->transfer_data
        EXPORTING
          tl_data = tl_recin1.

      tl_rec3data[] = tl_rec2data[].
      REFRESH: tl_rec2data[].
      CALL METHOD obj->transfer_data
        EXPORTING
          tl_data = tl_rec3data.
    ELSE. "MESSAGE IS INTIAL
*C--Append the contents in an internal table containing correct records
      APPEND LINES OF tl_chkdata TO t_passrec.
    ENDIF.
  ENDMETHOD.                    "distribute_data
ENDCLASS.                    "lcl_cats IMPLEMENTATION