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: 

Issue in updating Z table

Former Member
0 Kudos

Dear Friends,

I have an issue in Updating Ztable sequentially.

Scenario : One program updates my Ztable, from that table my program read records and post the documents ( BAPI_ACC_DOCUMENT_POST) . After successful posting of  document that Z-table has to update with document number. Programs are scheduled in background(SM37). Here problem is for few records even document get created successfully it's not updating the Z-Table. Please check the code below and suggest. Thanks in advance.

Code :   

CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
         EXPORTING
           DOCUMENTHEADER    = BAPIACHE09
         IMPORTING
           OBJ_TYPE          = OBJ_TYPE
           OBJ_KEY           = OBJ_KEY
           OBJ_SYS           = OBJ_SYS
         TABLES
           ACCOUNTGL         = IT_ACCOUNTGL[]
           ACCOUNTRECEIVABLE = IT_ACCOUNTRECEIVABLE[]
           CURRENCYAMOUNT    = IT_CURRENCYAMOUNT[]
           RETURN            = IT_RETURN[].

       READ TABLE IT_RETURN WITH KEY TYPE = 'E'.
       IF SY-SUBRC = 0. " ERROR OCCURED
         CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
         " updating error status in staging table
         LS_HEADER-BAPI_FLAG = 'E'.
         LS_HEADER-SAP_DATE = SY-DATUM.
         LS_HEADER-SAP_TIME = SY-UZEIT.
         READ TABLE IT_RETURN INDEX 2.
         LS_HEADER-STS_DESC = IT_RETURN-MESSAGE.
        UPDATE ZIB_INVOICE_HD FROM LS_HEADER.
         COMMIT WORK.
         WAIT UP TO 1 SECONDS.
       ELSEIF OBJ_KEY IS NOT INITIAL. " SUCCESS
         CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
         WAIT UP TO 2 SECONDS.
*       CONDENSE OBJ_KEY.
         LS_HEADER-BAPI_FLAG = 'S'.
         LS_HEADER-SAP_DATE = SY-DATUM.
         LS_HEADER-SAP_TIME = SY-UZEIT.
         LS_HEADER-STS_DESC = OBJ_KEY.
         UPDATE ZIB_INVOICE_HD FROM LS_HEADER.
         COMMIT WORK.
         WAIT UP TO 1 SECONDS.
       ENDIF.
     ELSE.
       LS_HEADER-BAPI_FLAG = 'E'.
       LS_HEADER-SAP_DATE = SY-DATUM.
       LS_HEADER-SAP_TIME = SY-UZEIT.
       LS_HEADER-STS_DESC = 'ERROR: Data issue'.
       UPDATE ZIB_INVOICE_HD FROM LS_HEADER.
       COMMIT WORK.
       WAIT UP TO 1 SECONDS.

ENDIF.

1 ACCEPTED SOLUTION

venkateswaran_k
Active Contributor
0 Kudos

Okay Do as follows then,

do not update the ztable for each post.

Store the record in internal table it_results..

Finally update the ztable from it_results.

Like

         LS_HEADER-BAPI_FLAG = XXXXX

         LS_HEADER-SAP_DATE = SY-DATUM.

         LS_HEADER-SAP_TIME = SY-UZEIT.

         LS_HEADER-STS_DESC = IT_RETURN-MESSAGE.

         append ls_header to it_results.


Finally

      UPDATE ztable from table it_results


Regard,

Venkat


10 REPLIES 10

venkateswaran_k
Active Contributor
0 Kudos

Your IF-Else condition is not seems to be correct.

Reformat like this

After the BAPI

Check for IT_RETURN[]. is initial

if it is initial

   write code for success - update / insert data into z table

else

   write code for failure - update / insert data into z-table

endif

Also, what is the primary key for your z-table.  There is a chance of getting overwriten by the last document.

former_member289261
Active Contributor
0 Kudos

What are the key fields of your ztable ?

Try using "BAPI_TRANSACTION_COMMIT after update command and in which case you wont have to use COMMIT WORK statement.

Former Member
0 Kudos

Thanks for your early reply. 

@ Mr. Krishnamurthy

I have written the code same way, you have suggested with If Else.

@ Mr. Rawat : Primary key is a field called Invoice_No in the Z-Table.

Main problem at a given point my program picks 300 records and try to post, It loops 300 times, each time after successful document creation , it should update Ztable against that Invoice number (Primary Key) with newly document number generated through that BAPI. 

Suppose all 300 are successful but after few successful updation there will be couple of blank status field, even though document created successfully.

I am suspecting WAIT UP TO 1 SECONDS statement causing problem.

0 Kudos

Hi Omkar,

Looking into your code I have a doubt that other type of messages are causing this problem, for example in your code you have handled only type 'E' message and when object_key is not initial so what about the message type 'W' 'I' 'A' and 'X', in this case it will not update the table.

For a cross verification take the record whose status is not updated and check posting it in debugging mode and check the messages.

Regards,

Shadab.

0 Kudos

Hi,

You are saying that the newly generated document number is the primary key of ztable.

But you are trying to use UPDATE command with the same.

UPDATE command is used to update an existing record.

How can you UPDATE a record which isn't there in the table ? Shouldn't it be INSERT command to be used here ? and LS_HEADER structure the primary key field INVOICE_NO is not filled up after document posting so the query will not be able to create a record.

( It will create a record for the first time with INVOICE_NO field with value 0000000000 and will give a dump from second time onwards with message "Record already exists with primary key 000000000" ).

These are some basic fundamentals which are lacking in the code. Please check.

Regards,

Ashish

0 Kudos

Dear Mr.Maldar ,

That is also one point, i will change my prog.

Thanks.

0 Kudos

Dear Mr. Rawat,

Newly generated Document no is not primary key, there is a field called Invoice_No that is primary key, against that Invoice_no i generate sap document number. After document generated successfully against that Invoice_No field, there is another blank field called "STS_DESC" there i update document no. 

0 Kudos

I am sorry...I misunderstood your invoice_no field.

venkateswaran_k
Active Contributor
0 Kudos

Okay Do as follows then,

do not update the ztable for each post.

Store the record in internal table it_results..

Finally update the ztable from it_results.

Like

         LS_HEADER-BAPI_FLAG = XXXXX

         LS_HEADER-SAP_DATE = SY-DATUM.

         LS_HEADER-SAP_TIME = SY-UZEIT.

         LS_HEADER-STS_DESC = IT_RETURN-MESSAGE.

         append ls_header to it_results.


Finally

      UPDATE ztable from table it_results


Regard,

Venkat


0 Kudos

Thank You.

I will try and let you know.