Wednesday, August 19, 2009

Reverse Screen Scrape or BDC

When I was developing on the (ISeries,AS/400,System 38) I always avoid doing screen scraping. Well I'm still not a huge fan I've spent some time with GUIXT and I think it's a pretty good product but I don't use it. Having said all that there is a technique in ABAP that allows you to populate a SAP program from another program. My company uses this technique to move purchase orders from the legacy system to the SAP system.
The way it works is pretty simple we create a text file of the purchase order we received the prior day. I place that text file on to the IFS of the system running SAP. The SAP system senses the file and runs a program that reads the text file into an internal table. It then reads the internal table one record at a time and populates the fields needed for program SAPMM06I Screen 100. It then runs that program in background generating the PO's. This is really a handy tool when you think about it. If I wanted to perform this activity without this tool I would need to know all of the tables that need to be populated along with all of the rules to handle the creation of PO's.



I hope my explanation of why this would be useful makes sense.

Let's go through the steps needed to create a BDC program.


Step 1: Run the transaction SHDB this will allow you to make a recording of a transaction. The following image are the results from this recording. This will give you a list of the fields needed for this program/screen.



Step 2: Setup an internal table.

DATA bdcdata like bdcdata occurs 0 with header line.
Layout of the internal table created by this statement.
Step 3: Open Group
form open_group.
call function 'BDC_OPEN_GROUP'
exporting
client = sy-mandt
group = 'Whatever'
user = sy-uname
keep = 'X'.
endform.
Change the Whatever to be what you want it to be.
Step 4:Setting the program name and screen number.
perform bdc_dynpro      using 'SAPMF05A'    '0123'.
Then you will need to add a subroutine called bdc_dynpro.
form bdc_dynpro using program dynpro.
clear bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
append bdcdata.
endform.
Step 5: Setting the values of the fields.
perform bdc_field       using 'BDC_OKCODE'  '/00'.

Of course you need a subroutine call bdc_field.
form bdc_field  using  fnam fval.
clear bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
append bdcdata.
endform.

You have to call this routine once for each of the fields that need to be populated use the output from SHDB to determine what fields need to be called.

Step 6: Tell the system the transaction to use.

perform bdc_transaction using 'F-26'.

Add the subroutine
form bdc_transaction using tcode.
* batch input session
call function 'BDC_INSERT'
exporting
tcode = tcode
tables
dynprotab = bdcdata.
endform.

Step 7: Close the Batch

Perform close_group.

form close_group.
* close batchinput group
call function 'BDC_CLOSE_GROUP'.

endform.

Step 8: Run the transaction

Up till this point nothing has happened we have simply populated an internal table.

call transaction 'F-26' using bdcdata mode 'E'.

Here is the code for the whole routine I hope you find this of use if you do drop me a message and let me know.

      perform open_group.
concatenate wa_auto-posting_date+4(4) wa_auto-posting_date+0(4) into
wa_date.
perform bdc_dynpro using 'SAPMF05A' '0123'.
perform bdc_field using 'BDC_OKCODE' '/00'.
perform bdc_field using 'BKPF-BUKRS' wa_auto-bukrs.
perform bdc_field using 'BKPF-BLART' 'DZ'.
perform bdc_field using 'RF05A-KONTO' '113014'.
perform bdc_field using 'BKPF-BLDAT' wa_date.
concatenate sy-datum+4(4) sy-datum+0(4) into
wa_date.
perform bdc_field using 'BKPF-BUDAT' wa_date.
perform bdc_field using 'BKPF-WAERS' 'USD'.
perform bdc_dynpro using 'SAPMF05A' '124'.
perform bdc_field using 'RF05A-AGKON' wa_auto-kunnr.
perform bdc_field using 'BKPF-BKTXT' 'MIACH'.
perform bdc_field using 'RF05A-SEL01' wa_auto-xblnr.
perform bdc_field using 'BSEG-WRBTR' wa_auto-amount.
perform bdc_field using 'BDC_OKCODE' '=BU'.
perform bdc_field using 'BDC_OKCODE' '/ENV'.
perform bdc_transaction using 'F-26'.
perform close_group.
call transaction 'F-26' using bdcdata mode 'E'.