Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

In this article,we are going to convert a bdc recording macro code into a portable code that can be utilized and embedded in any posting based program in the background with a log as well using 'CALL TRANSACTION' mechanism.

We can import this recording for T-code ABAVN through the file attached in this article named 'bdc_recording_ABAVN.txt.zip' via Tcode shdb. Just extract it and then import it using the instructions provided in the link below for the importing part in your system:

http://wiki.scn.sap.com/wiki/display/ABAP/ABAP+-+Tips+and+Tricks+Channel?original_fqdn=wiki.sdn.sap....

For creating a batch upload program we will be using a  BDC recording in SAP carried out via transaction code 'SHDB'. We can ask a Functional Consultant of the specific module to create a BDC recording for a given transaction along with the fields that are required for batch upload. Once a recording is created we can generate a macro by clicking the [] Program button to generate the macro code for the given recording in SHDB. We can also run the macro recording in different modes as it was created originally by clicking the Process[] button in SHDB. We will follow steps from when the BDC recording macro has been created and saved as a program,with an option of 'Transfer from Recording' radio button selected in the Field Contents

                             

from the pop-up menu.

In our case,it will be a BDC recording for Tcode = ABAVN, which is used for directly retiring an Asset by Scrapping it,which is used in SAP FI module.

To have a look at the BDC recording macro code generated,refer to the code below:

__________________________________________________________________________________________________________________________

    [BDC Macro Code for Transaction ABAVN ]

report ZASSET_SCRAP_BDC
no standard page heading line-size 255.

include bdcrecx1.

start-of-selection.

perform open_group.

perform bdc_dynpro      using 'SAPLAMDP' '0100'.
perform bdc_field      using 'BDC_OKCODE'
                         
'=TAB02'.
perform bdc_field      using 'RAIFP2-ANLN1'
                           
'1010000017'.
perform bdc_field      using 'RAIFP2-ANLN2'
                         
'0'.
perform bdc_field      using 'RAIFP1-BLDAT'   
                           
'13.10.2015'.
perform bdc_field      using 'RAIFP1-BUDAT'
                         
'13.10.2015'.
perform bdc_field      using 'RAIFP1-BZDAT'
                         
'13.10.2015'.
perform bdc_field      using 'BDC_CURSOR'   
                         
'RAIFP2-SGTXT'.
perform bdc_field      using 'RAIFP2-SGTXT'
                         
'Asset Scrap'.
perform bdc_dynpro      using 'SAPLAMDP' '0100'.
perform bdc_field      using 'BDC_OKCODE'
                         
'=SHWD'.
perform bdc_field      using 'RAIFP2-ANLN1'   
                         
'1010000017'.
perform bdc_field      using 'RAIFP2-ANLN2'
                         
'0'.
perform bdc_field      using 'RAIFP2-MONAT'
                           
'09'.
perform bdc_field      using 'RAIFP1-BLART'
                           
'AA'.
perform bdc_field      using 'BDC_CURSOR'
                         
'RAIFP1-XBLNR'.
perform bdc_field      using 'RAIFP1-XBLNR'   
                         
'ASSET SCRAP'.
perform bdc_dynpro      using 'SAPMSSY0' '0120'.
perform bdc_field      using 'BDC_OKCODE'
                         
'=BUCH'.
perform bdc_transaction using 'ABAVN'.

perform close_group.

__________________________________________________________________________________________________________________________


Here is a brief description of the elements in the BDC recording Macro Code used in the code above:


> All the fields called in the BDC recording start with the the following fields e.g:  'perform bdc_field using 'RAIFP1-BLART'' followed by a value which could either be a constant or a data object etc. as could be seen from the code above.


> The ones with the text 'BDC_CURSOR' follow up with the name of the field where the cursor is to be placed on the screen.

e.g:


perform bdc_field using 'BDC_CURSOR'
'RAIFP2-SGTXT'.


> When a specific screen or tab is called on a screen,it is usually followed with a  Module Pool Name and Screen No. e.g:


perform bdc_dynpro      using 'SAPLAMDP' '0100'.
perform bdc_field      using 'BDC_OKCODE'
'=TAB02'.


Here BDC_OKCODE is the ok code or an identifier for identifying which UI element  in Dynpro screen was clicked followed by the identifier id. For those of you who have done dynpro programming know the system constant sy-ucomm holds this value at runtime.


> Finally the transaction code for which this macro was generated is called using 'BDC_TRANSACTION'. e.g:


perform bdc_transaction using 'ABAVN'.


________


With the BDC recording out of the way,we can now concentrate on how  to convert it for Call Transaction Mechanism. The  best way to map fields is to create a single structure in ABAP for holding all values that are to be passed for all fields in the Macro recording discussed before.

e.g:

DATA: BEGIN OF s_assetscrap,
        bukrs type ANLA-BUKRS,"Company Code
        ANLN1 type anla-anln1,"Asset No
        ANLN2 type ANLA-ANLN2,"Sub Asset No
        BLDAT type RAIFP1-BLDAT,"Document Date
        budat type RAIFP1-BUDAT,"Posting Date
        BZDAT type RAIFP1-BZDAT,"Asset Value Date
        SGTXT type RAIFP2-SGTXT,"Text

        monat type RAIFP2-MONAT,"Posting Period
        BLART type RAIFP1-BLART,"Document Type
        XBLNR type RAIFP1-XBLNR,"Reference
      END OF s_assetscrap.


We now need to add in each of the BDC recording code lines to an internal table that will eventually be passed as a parameter when using the 'Call Transaction' construct. It will be based on 5 parameters as is required when calling a bdc code in Call Transaction construct based on the standard structure named 'BDCDATA'. Here are the fields in the structure:


  1. PROGRAM(Module Pool Name)
  2. DYNPRO(Screen No for the Dynpro)
  3. DYNBEGIN(BDC screen start)
  4. FNAM(Field name on the Screen)
  5. FVAL(Field value to be used)

Based on these fields,this is how we are going to convert it.


e.g:


"Original Code as provided in BDC recording macro


perform bdc_dynpro      using 'SAPLAMDP' '0100'.


"Code Converted for BDC recording called in Call Transaction construct


wa_bdcdata-program = 'SAPLAMDP'.

wa_bdcdata-dynpro = '0100'.

wa_bdcdata-dynbegin = 'X'.

wa_bdcdata-fnam = ''.

wa_bdcdata-fval = ''.


Compare the fields with the code above to get a better idea about value passed here.


Here is the complete code converted to the format as required for Call Transaction construct line by line.


_________[Each of the 19 Lines converted to Call Transaction Format based on BDCDATA structure]


> Add multiple records for this bdc recording in one go as well.


DATA:

     it_bdcdata type standard table of BDCDATA,

     wa_bdcdata like line of it_bdcdata.


"1

wa_bdcdata-program = 'SAPLAMDP'.

wa_bdcdata-dynpro = '0100'.

wa_bdcdata-dynbegin = 'X'.

wa_bdcdata-fnam = ''.

wa_bdcdata-fval = ''.


append wa_bdcdata it_bdcdata.

clear wa_bdcdata.


"...and so on. Only shortened code is shown here for illustrative purposes.


[Refer to the complete code in the attached text file named 'BDC_Recording_Code_Conversion_Code_Fragment.txt.zip'].


_________

The final element of transaction code  will be passed when using the final Call Transaction construct for uploading or posting .


dyn_begin = 'X' indicates that a screen is called at this point,therefore the module pool name and screen no is called here and for the rest field name and value pairs are passed in.


Here is how the final Call Transaction code will be called for posting this Asset Retirement via scrap:


DATA:

     it_msg type standard table of bdcmsgcoll."This will hold all Messages


  CALL TRANSACTION 'ABAVN' USING it_bdcdata

    MODE 'N'

    UPDATE 'S'

    MESSAGES INTO it_msg.

Here it_bdcdata is the BDC recording format converted for compatibility with BDC Recording.

     Mode   = N[For Running in the Background]

               [All Modes in BDC recording are supported like

                'E'(Errors),'A'(All Screens)]

     Update = S[Synchronous]

     it_msg = Holds all error messages and successful messages


For Logging,Internal table it_msg can be looped through for finding successful postings(msgtyp = 'S') and failed postings(msgtyp = 'F') of uploads via call transaction construct. e.g for successful messages:


LOOP AT it_msg INTO wa_msg WHERE msgtyp EQ 'S'.
CALL FUNCTION 'MESSAGE_TEXT_BUILD'
EXPORTING
msgid= wa_msg-msgid
msgnr= wa_msg-msgnr
msgv1= wa_msg-msgv1
msgv2= wa_msg-msgv2
msgv3= wa_msg-msgv3
msgv4= wa_msg-msgv4
IMPORTING
message_text_output = wa_msg_all.

ENDLOOP.


Here the data object 'wa_msg_all' complete error message in one line, where as individual components are also store in one of the structures like wa_msg-msgid(Message Id number),wa_msg-msgnr(Message No),wa_msg-msgv1 to wa_msg-msgv4(contains error or successful messages with description).


Using this code mechanism through Call Transaction we can convert BDC recordings into portable executable code which can be embedded especially if you are working on a selective posting program using an interactive ALV report for selecting records and posting them.