Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

help needed for report on tables vbak and vbap

Former Member
0 Kudos

Hello,

I needed help in one of my work issues, this is my first project and Iam still learning. kindly help.

I was asked to take erdat,vbeln,posnr from selection screen and compare it with the data already in tables vbap,vbak. If the data

already exists and exception needs to be raised saying that the 'data exists',but if the data is not present in the tables the

tables have to be updated. This has to be done using function module only.

Then I have to write a REPORT calling the above function module and finally displaying the number of records updated and the

list of them.

I have started writing a function module:

FUNCTION z_tableupdate.

*"----


""Local Interface:

*" IMPORTING

*" VALUE(DATE) LIKE VBAK-ERDAT DEFAULT SY-DATUM

*" VALUE(SALESORDERNO) LIKE VBAK-VBELN

*" VALUE(ITEMNO) LIKE VBAP-POSNR

*" EXPORTING

*" VALUE(EX_VBAP) LIKE VBAP STRUCTURE VBAP

*" VALUE(SYS) LIKE SY-SYSID

*" VALUE(EX_VBAK) LIKE VBAK STRUCTURE VBAK

*" EXCEPTIONS

*" DATA_UPDATED

*" DATA_EXISTS

*"----


SELECT * FROM vbak INTO vbak_wa WHERE erdat = date

AND vbeln = salesorderno.

ENDSELECT.

SELECT * FROM vbap INTO vbap_wa WHERE posnr = itemno.

ENDSELECT.

IF sy-subrc EQ 0.

ex_vbap = vbap_wa.

ex_vbak = vbak_wa.

ELSE.

RAISE data_exists.

ENDIF.

sys = sy-sysid.

ENDFUNCTION.

_______________________________________________________________________________________________________________________________________

How is the above code for function module, will this work??

Now can I write a report to call the above function module and update the record?? if so, how to update ??? please help...Lots of thanks in advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Use the following code to get an idea.

Put following code in ur program

report ztest17.

parameters: p_vbeln like vbak-vbeln,

p_posnr like vbap-posnr,

p_erdat like sy-datum.

DATA: begin of i_vbap occurs 0.

include structure vbap.

DATA: end of i_vbap.

DATA: begin of i_vbak occurs 0.

include structure vbak.

DATA: end of i_vbak.

data : v_sysid like sy-sysid.

call function 'z_tableupdate'

exporting DATE =

SALESORDERNO = p_vbeln

ITEMNO = p_posnr

importing EX_VBAP = i_vbap

SYS = v_sysid

EX_VBAK = i_vbak

exceptions

DATA_EXISTS = 1.

if sy-subrc = 1.

WRITE: / 'No data found'.

elseif sy-subrc = 0.

  • write your update statement

endif.

Cheers,

Vikram

7 REPLIES 7

Former Member
0 Kudos

Hi

Welcome to SDN forum

Whay can't you write a simple report in SE38.

why you are using Fun module

Nothing wrong in it.

But first become perfetc in reports in SE38 and then do using the fun modules

write a simple select statement like

SELECT AVBELN AERDAT BPOSNR BMATNR

INTO TABLE ITAB

FROM VBAK AS A JOIN VBAP ON

AVBELN = BVBELN

WHERE A~VBELN IN S_VBELN AND

A~ERDAT IN S_ERDAT AND

B~POSNR IN S_POSNR.

IF SY-SUBRC <> O.

WRITE: / 'No data found'.

ENDIF.

First write this in SE38 by defining a proper selection screen and internal tables

then do as fun module

see the doc for fun module

Function Modules;

Check this matter.

Function Modules are Global ABAP programs created by SAP for reusable purpose.they have IMPORT,EXPORT and TABLE parameters, and EXCEPTIONS to through when error occurs.

You can create them from TCode SE37.

Go through the following doc:

Function modules are cross-program, reusable procedures that are organized into function groups, and whose functions are implemented between the statements FUNCTION and ENDFUNCTION. Function modules and their interfaces are created in the Function Builder.

Function Module Interfaces

The parameter interface of a function module is defined in the Function Builder. It includes the definition of interface parameters and the specification of exceptions that can be triggered by a function module. The Function Builder automatically generates comment lines below the FUNCTION statement in the source code of the function module, which represent the interface of the function module with the following syntax:

Syntax

... [IMPORTING parameters]

[EXPORTING parameters]

[CHANGING parameters]

[TABLES table_parameters]

[{RAISING|EXCEPTIONS} exc1 exc2 ...]

The syntax and semantics of IMPORTING, EXPORTING, CHANGING, RAISING, and EXCEPTIONS mainly correspond to the definition of method interfaces with [CLASS-]METHODS. The additional option of defining table parameters using TABLES is obsolete.

Interface parameters

The interface parameters are defined on the relevant tab pages in the Function Builder.

IMPORTING parameters are input parameters. When the function module is called, a suitable actual parameter must be specified for every non-optional input parameter. The content of the actual parameter is passed to the input parameter when the call is made. The content of an input parameter for which 'pass by reference' is defined cannot be changed in the function module.

EXPORTING parameters are output parameters. When the function module is called, a suitable actual parameter can be specified for every output parameter. The content of an output parameter that is defined for 'pass by value' is transferred to the actual parameter if the function module is completed without errors. An output parameter that is defined for pass by reference is not initialized when the function module is called.

CHANGING parameters are input and output parameters. When the function module is called, a suitable actual parameter must be specified for every non-optional input or output parameter. When the function module is called, the content of the actual parameter is passed to the input/output parameter, and when the function module is completed, the content of the input/output parameter is passed to the actual parameter.

TABLES parameters are table parameters. Table parameters are obsolete CHANGING parameters that are typed as standard tables with a header line. If an internal table without a header line or a table body is passed as an actual parameter to a formal parameter of this type, an empty local header line is generated in the function module. If an internal table with a header line is used as an actual parameter, both the table body and the header line are passed to the function module. Pass by value is not possible in formal parameters defined using TABLES. Formal parameters defined with TABLES can be replaced by formal parameters defined with CHANGING. A local work area can be created for the internal table in the function module by using the addition LIKE LINE OF itab of the DATA statement.

Exceptions

The exception of a function module are defined on the Exceptions tab page in the Function Builder. Here you can select exception classes to define whether class-based exceptions are declared or non-class-based exception are defined. Class-based exceptions are represented in the above syntax by RAISING, and non-class-based exceptions are represented by EXCEPTIONS.

The addition RAISING is used to declare class-based exceptions that can be propagated from the function module to the caller. Exceptions in the categories CX_STATIC_CHECK and CX_DYNAMIC_CHECK must be explicitly declared, otherwise a propagation can lead to an interface violation. A violation of the interface leads to the treatable exception CX_SY_NO_HANDLER. Exceptions of the category CX_NO_CHECK are implicitly always declared. The declaration of exceptions of the category CX_STATIC_CHECK is statically checked in the syntax check. For exceptions of the category CX_DYNAMIC_CHECK, the check is not performed until runtime. In a function module in which class-based exceptions are declared with the RAISING addition, the statement CATCH SYSTEM-EXCEPTIONS cannot be used. Instead, the relevant treatable exceptions should be handled in a TRY control structure.

The addition EXCEPTIONS is used to define a list of non-class-based exceptions that can be triggered in the function module using the statements RAISE or MESSAGE RAISING. Exceptions defined in this way - as with formal parameters - are bound to the function module and cannot be propagated. If an exception of this type is triggered in a function module, and no return value has been assigned to it with the homonymous addition EXCEPTIONS of the CALL FUNCTION statement when the call was made, this leads to a runtime error.

Note

For new developments after release 6.10, SAP recommends that you work with class-based exceptions that are independent of the function module.

RFC is a technology which is used to access a functions (Modules) from

the remote systems.

If a function module is set as remote enabled which can be access from

the remote system via RFC.Eg: U can access the Remote enabled function modules in ur VB,Webdynpro,Java,Visual composer program.

A function module can be set as remote enabled by SE37->Go to ur FM->click the option Button "remote enabled".

But Normal function modules can not accessd from the remote system.

Good Example for RFC enabled function module is : BAPI(Business Application Programming Interface)

Note: All BAPIs are Remote enabled but not all remote enabled function modules are BAPI.

CALLING A FUNCTION MODULE:

1)In U ABAP Editor --> Click "Patter" ---> Selection Option Button "Call Function"

--> Write the Corresponding FM name --> Hit Enter

2)The appropriate import ,export Parameters will be displayed in ur editor

3)Pass the Values Here.

Also check these links.

http://www.geocities.com/victorav15/sapr3/abapfun.html

Check this link:

http://help.sap.com/saphelp_erp2004/helpdata/en/9f/db988735c111d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/ef/d94b78ebf811d295b100a0c94260a5/frameset.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/43/41341147041806e10000000a1553f6/frameset.htm

Check this link:

http://help.sap.com/saphelp_erp2004/helpdata/en/9f/db988735c111d1829f0000e829fbfe/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/ef/d94b78ebf811d295b100a0c94260a5/frameset.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/43/41341147041806e10000000a1553f6/frameset.htm

See the following links:

http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db970e35c111d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_nw04/helpdata/en/26/64f623fa8911d386e70000e82011b8/content.htm

Regards

Anji

0 Kudos

Thanks for the input.

But I need to use function module for data extraction and then use report to generate the output with the list of records updated.

tell me in this direction, thanks so much...

Former Member
0 Kudos

Hi,

Use the following code to get an idea.

Put following code in ur program

report ztest17.

parameters: p_vbeln like vbak-vbeln,

p_posnr like vbap-posnr,

p_erdat like sy-datum.

DATA: begin of i_vbap occurs 0.

include structure vbap.

DATA: end of i_vbap.

DATA: begin of i_vbak occurs 0.

include structure vbak.

DATA: end of i_vbak.

data : v_sysid like sy-sysid.

call function 'z_tableupdate'

exporting DATE =

SALESORDERNO = p_vbeln

ITEMNO = p_posnr

importing EX_VBAP = i_vbap

SYS = v_sysid

EX_VBAK = i_vbak

exceptions

DATA_EXISTS = 1.

if sy-subrc = 1.

WRITE: / 'No data found'.

elseif sy-subrc = 0.

  • write your update statement

endif.

Cheers,

Vikram

0 Kudos

Thanks a lot for replying to my message. I have written a report to extract using the same technique but using two function modules, please see the following. But Iam unable to write a update code: I have a question here, are there in-built function modules available to update the tables? if so let me know. thanks a lot again.

_____________________________________

&----


*& Report ZHA_FMCALL

*&

&----


*&

*&

&----


REPORT zha_fmcall.

----


  • Table Declaration

----


TABLES: vbak,

vbap.

*select-options: date for vbak-erdat DEFAULT sy-datum,

  • saleorno for vbak-vbeln,

  • itemno for vbap-posnr.

----


  • Parameters Declaration for call function z_fm_soupdate

----


PARAMETERS: date LIKE vbak-erdat DEFAULT sy-datum,

saleorno LIKE vbak-vbeln,

itemno LIKE vbap-posnr.

----


  • Types Declaration

----


TYPES: BEGIN OF lt_results,

erdat TYPE erdat,

vbeln TYPE vbeln,

posnr TYPE posnr,

aufnr TYPE aufnr,

vgtyp TYPE vgtyp,

matnr TYPE matnr,

netwr TYPE netwr,

END OF lt_results.

----


  • Data Declaration

----


DATA: system LIKE sy-sysid,

t_results TYPE STANDARD TABLE OF lt_results WITH HEADER LINE.

DATA: BEGIN OF i_vbap OCCURS 0.

INCLUDE STRUCTURE vbap.

DATA: END OF i_vbap.

DATA: BEGIN OF i_vbak OCCURS 0.

INCLUDE STRUCTURE vbak.

DATA: END OF i_vbak.

DATA : v_sysid LIKE sy-sysid.

PERFORM form1.

&----


*& Form FORM1

&----


  • text

----


FORM form1.

----


  • Call function to populate the date from the tables using the

  • user inputs and raise exception if the data already exists

----


CALL FUNCTION 'Z_FM_SOUPDATE'

EXPORTING

date = sy-datum

salesorderno = saleorno

itemno = itemno

IMPORTING

ex_vbap = i_vbap

sys = v_sysid

ex_vbak = i_vbak

EXCEPTIONS

  • DATA_UPDATED = 1

data_exists = 2

  • OTHERS = 3

.

----


  • To raise the exceptions if the data inputed already exists in

  • the table

----


CASE sy-subrc.

WHEN 2.

WRITE:/ 'DATA ALREADY EXISTS'.

EXIT.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDCASE.

*SELECT erdat

  • vbeln

  • aufnr

  • vgtyp

  • matnr from vbap into t_results where erdat in date

  • and vbeln in saleorno

  • and posnr in itemno.

*endselect.

----


  • Call function module popup_to_display_text to pop-up a screen

  • showing whether the record is updated or not

----


CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'

EXPORTING

titel = 'Status'

textline1 = 'Record Updated'

  • TEXTLINE2 = ' '

start_column = 25

start_row = 6

.

----


  • For output to the screen

----


WRITE:/ i_vbak-erdat,

i_vbak-vbeln,

i_vbap-posnr,

i_vbap-aufnr,

i_vbap-vgtyp,

i_vbap-matnr,

i_vbak-netwr.

ENDFORM. "FORM1

_________________________________

Help me with some inputs... thanks again..

0 Kudos

help plz....

0 Kudos

Hi,

What tables are you updating? If you want to update tables like VBAK, VBAP then you should use BAPI BAPI_SALESDOCUMENT_CHANGE.

I cant see where are you doing update in your code.

You can find number of examples for this BAPI in SDN forum.

Cheers,

Vikram

0 Kudos

vikram,

that is the problem i have right now, writing a code to update the table. by the way thanks for the bapi name.

and secondly, i was thinking to update the inputed values to a table.. what do you suggest? can i create a new table or just go for an internal table and use it to extract for the final report to display updated records?

thanks a lot for the input, i was searching all the bapis in the system starting with Z. that way i would have never got the real bapi name. thanks again and waiting for your suggestion.