Skip to Content

Scenario: Step-by-step creation of a FM to Get Opening/Closing Balance of G/L as on Key Date.

As we have Standard FM in SAP For G/L Balances: –

BAPI_GL_GETGLACCBALANCE:- closing balance of G/L account for a year.

BAPI_GL_GETGLACCCURRENTBALANCE: – closing balance of G/L account for the current year

BAPI_GL_GETGLACCPERIODBALANCES:- Posting period balances for each G/L account.

And

BAPI_AP_ACC_GETKEYDATEBALANCE:- Opening Vendor balance

BAPI_AR_ACC_GETKEYDATEBALANCE:- Opening Customer Balance

But we don’t have any standard FM to get Opening/Closing G/L Account Balance As on key date.

This Document specifies Step by Step how to create a FM for getting Opening/Closing G/L Account Balance with code.

    1.  Create FM in SE37:- ZGL_OPENING_CLOSING_BAL_KEYDAT.

     2.  Maintain Parameters in Import and Export as shown below.

             

              

      3.  Source code:-

             

Code:-

FUNCTION ZGL_OPENING_CLOSING_BAL_KEYDAT.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     REFERENCE(COMP) TYPE  BKPF-BUKRS
*”     REFERENCE(DATE) TYPE  BKPF-BUDAT
*”     REFERENCE(ACCT) TYPE  BSEG-HKONT
*”  EXPORTING
*”     REFERENCE(BALANCE) TYPE  BSEG-DMBTR
*”———————————————————————-
DATA : COMPANYCODEID  LIKE  BAPI0002_2-COMP_CODE     ,
POSTING_DATE  
LIKE  BAPI0002_4-POSTING_DATE  ,
FISCAL_YEAR   
LIKE  BAPI0002_4-FISCAL_YEAR   ,
FISCAL_PERIOD 
LIKE  BAPI0002_4-FISCAL_PERIOD .

DATA : ACCOUNT_BALANCES  TYPE TABLE OF BAPI3006_4 WITH HEADER LINE.
DATA : BAL TYPE BSEG-DMBTR.

DATA : IV_DATE  TYPE  D   ,
EV_MONTH_BEGIN_DATE 
TYPE  D,
EV_MONTH_END_DATE 
TYPE  D .

DATA : COMPANYCODE  LIKE  BAPI3006_0-COMP_CODE,
GLACCT 
LIKE  BAPI3006_0-GL_ACCOUNT ,
FISCALYEAR 
LIKE  BAPI3006_4-FISC_YEAR,
CURRENCYTYPE 
LIKE  BAPI3006_5-CURR_TYPE VALUE 10.

DATA : IT_BKPF TYPE TABLE OF BKPF WITH HEADER LINE.

DATA : BEGIN OF IT_BSEG OCCURS 0,
BELNR
TYPE BSEG-BELNR,
GJAHR
TYPE BSEG-GJAHR,
SHKZG
TYPE BSEG-SHKZG  ,
DMBTR
TYPE BSEG-DMBTR,
END OF IT_BSEG.

COMPANYCODEID = COMP.
POSTING_DATE = DATE.

IV_DATE  = DATE.

CALL FUNCTION ‘HR_JP_MONTH_BEGIN_END_DATE’
EXPORTING
IV_DATE             = IV_DATE
IMPORTING
EV_MONTH_BEGIN_DATE = EV_MONTH_BEGIN_DATE
EV_MONTH_END_DATE   = EV_MONTH_END_DATE.

CALL FUNCTION ‘BAPI_COMPANYCODE_GET_PERIOD’
EXPORTING
COMPANYCODEID = COMPANYCODEID
POSTING_DATE  = POSTING_DATE
IMPORTING
FISCAL_YEAR   = FISCAL_YEAR
FISCAL_PERIOD = FISCAL_PERIOD.

IF EV_MONTH_END_DATE NE DATE.

IF FISCAL_PERIOD = 1.
FISCAL_PERIOD =
12              .
FISCAL_YEAR = FISCAL_YEAR –
.
ELSE.
FISCAL_PERIOD =  FISCAL_PERIOD –
1.
ENDIF.

ENDIF.

COMPANYCODE  =  COMP.
GLACCT  = ACCT.
FISCALYEAR  = FISCAL_YEAR.

CALL FUNCTION ‘BAPI_GL_ACC_GETPERIODBALANCES’
EXPORTING
COMPANYCODE      = COMPANYCODE
GLACCT           = GLACCT
FISCALYEAR       = FISCALYEAR
CURRENCYTYPE     = CURRENCYTYPE
TABLES
ACCOUNT_BALANCES = ACCOUNT_BALANCES.

READ TABLE ACCOUNT_BALANCES  WITH KEY FISC_YEAR = FISCALYEAR FIS_PERIOD = FISCAL_PERIOD.
IF SY-SUBRC = 0.
BALANCE =  ACCOUNT_BALANCES-BALANCE.
ENDIF.

IF EV_MONTH_END_DATE NE DATE.

SELECT * FROM BKPF INTO TABLE IT_BKPF WHERE BUKRS = COMP AND BUDAT >= EV_MONTH_BEGIN_DATE
AND BUDAT <= DATE.

IF IT_BKPF[] IS NOT INITIAL.

SELECT BELNR GJAHR SHKZG DMBTR
FROM BSEG INTO CORRESPONDING FIELDS OF TABLE IT_BSEG
FOR ALL ENTRIES IN IT_BKPF
WHERE BELNR = IT_BKPF-BELNR AND GJAHR = IT_BKPF-GJAHR AND HKONT = ACCT AND BUKRS = COMP.

ENDIF.

LOOP AT IT_BSEG.
IF IT_BSEG-SHKZG = ‘H’.
BAL = BAL – IT_BSEG-DMBTR.
ELSEIF IT_BSEG-SHKZG = ‘S’.
BAL = BAL + IT_BSEG-DMBTR.
ENDIF.
ENDLOOP.

BALANCE = BALANCE + BAL.

ENDIF.

ENDFUNCTION.

Output:-

            

             

To report this post you need to login first.

2 Comments

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

  1. Alejandro Bindi

    Thanks for the blog, but I’d like to make one clarification:

     

    In the initial comments, you mention module BAPI_GL_GETGLACCPERIODBALANCES (f.g. 1028, UNRELEASED)

    But in the coding example, you used BAPI_GL_ACC_GETPERIODBALANCES (f.g. 3006, RELEASED).

    Both f.m.’s cover the exact same functionality.

    But regardless of the release indicator, the one to be used is BAPI_GL_GETGLACCPERIODBALANCES

    I found differences with the released one output versus FS10N, and besides, this is hidden in it’s documentation:

     

    FU BAPI_GL_ACC_GETPERIODBALANCES
    ____________________________________________________
    Txt.brv.
    Posting period balances for each G/L account
    Note!: The GetPeriodBalances method will no longer be supported. The functions will be replaced by the GetPeriodBalances method in object type BUS1028 of the general ledger.
    If you have previously used this method, change the usage to that of the new method in object type BUS1028.
    Use the method in object type BUS1028 the next time you use this function.

     

    Regards

    (0) 

Leave a Reply