Task: To create an RFC for creating a Sales order in SAP.
In the earlier posts we have already seen how to use a BAPI to create a SALES order in SAP ABAP. Today we will create an RFC to post a sales order.
Note: You can use the following BAPIs and Function modules to create a Sales Order in SAP.
BAPI_SALESORDER_CREATEFROMDAT1 Sales Order: Create Sales Order
BAPI_SALESORDER_CREATEFROMDAT1 Sales Order: Create Sales Order
SD_SALESDOCUMENT_CREATE
BAPISDORDER_GETDETAILEDLIST Sales Order: List of All Order Data
BAPI_ORDER_CHANGE_STATUS_GET Change status for order
BAPI_SALESORDER_CHANGE Sales Order: Change Sales Order
BAPI_SALESORDER_GETLIST Sales order: List of all orders for customer
BAPI_SALESORDER_GETSTATUS Sales order: Display status
BAPI_SALESORDER_SIMULATE Sales Order: Simulate Sales Ord
Transactions used in this Tutorial.
SE37 Function Builder
SE38 ABAP Editor
SE80 Object Navigator
SHDB BDC Recorder
First we will do a BDC recording using transaction SHDB. To see details on how to do a BDC recording please click here.
In this case you need to use the transaction VA01 to do the recording.
We will do the recording for the following fields.
Order Type
Sold-to Party
Ship-to Party
Purch Ord No
Material
Quantity
In the function module that we create these will be the Import Parameters.
Note: you need to do the BDC recording for all the mandatory fields for creating a Sales Order.
Once the BDC recording is complete for Transaction VA01 as shown in the previous post (Refer this)
We need to create a program for this recording (Refer this).
Once the above two tasks are complete we need to do some clean up as we have to transfer this program to a function module. Please refer to the following program.
**********************************************************************************************************************************
report ZSALESORDER
no standard page heading line-size 255.
DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE,
CTUMODE LIKE CTU_PARAMS-DISMODE VALUE 'A',
CUPDATE LIKE CTU_PARAMS-UPDMODE VALUE 'L',
MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE,
L_SUBRC LIKE SY-SUBRC,
L_MSTRING(480),
NODATA VALUE '/'.
*include bdcrecx.
start-of-selection.
perform bdc_dynpro using 'SAPMV45A' '0101'.
perform bdc_field using 'BDC_CURSOR'
'VBAK-SPART'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'VBAK-AUART'
'OR'.
perform bdc_field using 'VBAK-VKORG'
''.
perform bdc_field using 'VBAK-VTWEG'
''.
perform bdc_field using 'VBAK-SPART'
''.
perform bdc_dynpro using 'SAPMV45A' '4001'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'VBKD-BSTKD'
'15493'.
perform bdc_field using 'KUAGV-KUNNR'
'7777'.
perform bdc_field using 'KUWEV-KUNNR'
'7777'.
perform bdc_field using 'RV45A-KETDAT'
'04/01/2008'.
perform bdc_field using 'RV45A-KPRGBZ'
'D'.
perform bdc_field using 'VBKD-PRSDT'
'03/19/2008'.
perform bdc_field using 'BDC_CURSOR'
'RV45A-KWMENG(01)'.
perform bdc_field using 'RV45A-MABNR(01)'
'y-351'.
perform bdc_field using 'RV45A-KWMENG(01)'
' 1'.
perform bdc_dynpro using 'SAPMV45A' '4001'.
perform bdc_field using 'BDC_OKCODE'
'=SICH'.
perform bdc_field using 'VBKD-BSTKD'
'15493'.
perform bdc_field using 'KUAGV-KUNNR'
'7777'.
perform bdc_field using 'KUWEV-KUNNR'
'7777'.
perform bdc_field using 'RV45A-KETDAT'
'04/01/2008'.
perform bdc_field using 'RV45A-KPRGBZ'
'D'.
perform bdc_field using 'VBKD-PRSDT'
'03/19/2008'.
perform bdc_field using 'VBKD-ZTERM'
'ZB01'.
perform bdc_field using 'VBKD-INCO1'
'CIF'.
perform bdc_field using 'VBKD-INCO2'
'Düsseldorf'.
perform bdc_field using 'BDC_CURSOR'
'RV45A-MABNR(02)'.
perform bdc_dynpro using 'SAPLSPO2' '0101'.
perform bdc_field using 'BDC_OKCODE'
'=OPT1'.
perform bdc_transaction using 'VA01'.
Form bdc_transaction using tcode.
CALL TRANSACTION TCODE USING BDCDATA
MODE CTUMODE
UPDATE CUPDATE
MESSAGES INTO MESSTAB.
L_SUBRC = SY-SUBRC.
WRITE: / 'CALL_TRANSACTION',
TCODE,
'returncode:'(I05),
L_SUBRC,
'RECORD:',
SY-INDEX.
LOOP AT MESSTAB.
ENDLOOP.
endform.
*----------------------------------------------------------------------
* Start new screen
*----------------------------------------------------------------------
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
CLEAR BDCDATA.
BDCDATA-PROGRAM = PROGRAM.
BDCDATA-DYNPRO = DYNPRO.
BDCDATA-DYNBEGIN = 'X'.
APPEND BDCDATA.
ENDFORM.
*----------------------------------------------------------------------
* Insert field
*----------------------------------------------------------------------
FORM BDC_FIELD USING FNAM FVAL.
IF FVAL <> NODATA.
CLEAR BDCDATA.
BDCDATA-FNAM = FNAM.
BDCDATA-FVAL = FVAL.
APPEND BDCDATA.
ENDIF.
ENDFORM.
**********************************************************************************************************************************
Please make a note that the above program has been modified from the program that was generated using transaction SHDB. The include BDCRECX has been commented out and the code has been inserted in the main program. We will do further modifications to this program while inserting this code in the Remote Enabled Function Module RFC.
We will now create a Function Module and make it Remotely Enabled. How to create a function module see here.
Before creating a function module you need to create a function group see here.
Once the structure of the function module is created we need to create the Import Parameters. The import parameters we vary from system to system and will depend on the fields that you need to transfer to SAP R/3. Ideally there will be Import Parameters, Tables and Export Parameters.
Please see the figure below.
IMPORTANT: Note that the Pass Value filed has been selected. This filed has to be selected for all RFCs.
Once the Import Parameters are set you can create the export parameters.
Here we will be exporting the Sales Order Number.
IMPORTANT: Note that the Pass Value filed has been selected. This filed has to be selected for all RFCs.
Now we need to create an Include program as shown below.
Put the following code in a separate include program. This include program will be inserted in one of the includes of the function module. Namely LZSALESTOP. Please note that this name fill differ in your system. But the last three letters of the include will be TOP.
Also make a note of the following.
The include ending with UXX (in our case LZSALESUXX) should not be modified. SAP generates the following code in this include.
*****************************************************************
* THIS FILE IS GENERATED BY THE FUNCTION LIBRARY. *
* NEVER CHANGE IT MANUALLY, PLEASE! *
*****************************************************************
Put the following code in a separate include program. This include program will be inserted in one of the includes of the function module. Namely LZSALESTOP. Please note that this name fill differ in your system. But the last three letters of the include will be TOP.
INCLUDE LZSALESU01.
***INCLUDE ZSALESCREATEI .
DATA: BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE,
CTUMODE LIKE CTU_PARAMS-DISMODE VALUE 'A',
CUPDATE LIKE CTU_PARAMS-UPDMODE VALUE 'L',
MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE,
L_SUBRC LIKE SY-SUBRC,
L_MSTRING(480),
NODATA VALUE '/'.
Form bdc_transaction using tcode.
CALL TRANSACTION TCODE USING BDCDATA
MODE CTUMODE
UPDATE CUPDATE
MESSAGES INTO MESSTAB.
L_SUBRC = SY-SUBRC.
WRITE: / 'CALL_TRANSACTION',
TCODE,
'returncode:'(I05),
L_SUBRC,
'RECORD:',
SY-INDEX.
endform.
*----------------------------------------------------------------------
* Start new screen
*----------------------------------------------------------------------
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
CLEAR BDCDATA.
BDCDATA-PROGRAM = PROGRAM.
BDCDATA-DYNPRO = DYNPRO.
BDCDATA-DYNBEGIN = 'X'.
APPEND BDCDATA.
ENDFORM.
*----------------------------------------------------------------------
* Insert field
*----------------------------------------------------------------------
FORM BDC_FIELD USING FNAM FVAL.
IF FVAL <> NODATA.
CLEAR BDCDATA.
BDCDATA-FNAM = FNAM.
BDCDATA-FVAL = FVAL.
APPEND BDCDATA.
ENDIF.
ENDFORM.
Now the main code has to be inserted in the body of the function module. The code shown below needs to be inserted in the body of the function module.
Please note that in the code given below the hard coded values are replaced by IMPORT Parameters of the function module.
FUNCTION Z_SALES_CREATE.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(ORDTYP) LIKE VBAK-AUART
*" VALUE(SOLDTO) LIKE KNA1-KUNNR
*" VALUE(SHIPTTO) LIKE KNA1-KUNNR
*" VALUE(PURCHORDNO) LIKE VBKD-BSTKD
*" VALUE(MATNO) LIKE MARA-MATNR
*" VALUE(QTY) LIKE RV45A-KWMENG
*" EXPORTING
*" VALUE(SALESORDNO) LIKE VBAK-VBELN
*"----------------------------------------------------------------------
perform bdc_dynpro using 'SAPMV45A' '0101'.
perform bdc_field using 'BDC_CURSOR'
'VBAK-SPART'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'VBAK-AUART'
ORDTYP.
perform bdc_field using 'VBAK-VKORG'
''.
perform bdc_field using 'VBAK-VTWEG'
''.
perform bdc_field using 'VBAK-SPART'
''.
perform bdc_dynpro using 'SAPMV45A' '4001'.
perform bdc_field using 'BDC_OKCODE'
'/00'.
perform bdc_field using 'VBKD-BSTKD'
PURCHORDNO.
perform bdc_field using 'KUAGV-KUNNR'
SOLDTO.
perform bdc_field using 'KUWEV-KUNNR'
SHIPTTO.
perform bdc_field using 'RV45A-KETDAT'
'04/01/2008'.
perform bdc_field using 'RV45A-KPRGBZ'
'D'.
perform bdc_field using 'VBKD-PRSDT'
'03/19/2008'.
perform bdc_field using 'BDC_CURSOR'
'RV45A-KWMENG(01)'.
perform bdc_field using 'RV45A-MABNR(01)'
MATNO.
perform bdc_field using 'RV45A-KWMENG(01)'
QTY.
perform bdc_dynpro using 'SAPMV45A' '4001'.
perform bdc_field using 'BDC_OKCODE'
'=SICH'.
perform bdc_transaction using 'VA01'.
GET PARAMETER ID 'AUN' FIELD SALESORDNO.
ENDFUNCTION.
Note that the remotely enabled and Start Immediately radio buttons are selected as shown below.
Please add the following line once the Call Transaction is completed.
GET PARAMETER ID 'AUN' FIELD SALESORDNO.
The above command will get the newly created Sales Order in the filed SALESORDNO. Alternatively you can follow the procedure given below using the USEREXIT.
GET PARAMETER ID 'AUN' FIELD SALESORDNO. Is a simpler way of getting the latest sales order number.
For more details on GET PARAMETER ID and SET PARAMETER ID please see the following.
SAP SPA/GPA Parameters
Important. The Sales Order number should be obtained in the EXPORT parameters of the function module. This is not handled in the code given below. But I will mention the user-exit from where the Sales Order number needs to be exported to this function module. We will discuss IMPORTING and EXPORTING from SAP Memory in the next Post.
User Exit Program Name: MV45AFZZ
FORM USEREXIT_SAVE_DOCUMENT.
* Example:
* CALL FUNCTION 'ZZ_EXAMPLE'
* IN UPDATE TASK
* EXPORTING
* ZZTAB = ZZTAB.
LOOP AT XVBEP WHERE UPDKZ NE UPDKZ_DELETE
AND NOT AUFNR IS INITIAL.
SET PARAMETER ID 'ANR' FIELD XVBEP-AUFNR.
ENDLOOP.
ENDFORM.
The Sales order number will be generated at the above point and can be exported to SAP Memory which can then be imported back into the function module.
2 comments:
Good One... very informative
Thanks Rajesh ,Really appreciate the time you have taken to set up this blog.
Post a Comment