Posted: Sat Sep 22, 2007 4:29 pm Post subject: Batch Create and Characteristics via BAPI
Original: http: /www.sapfans.com/forums/viewtopic.php?t=125320
Author: NeilinOZ
Below is a very simple example of how to create a batch and then apply characteristics to it. I have been trying to do this for a while and finally got it to work. As a few others have asked the same question I thought I would post an example. You will obviously have to change the characteristic names to the ones in your system but you should get the idea of how it all works.
Hope someone finds it usefull.
Code:
*---------------------------------------------------------------------*
* Report ZTESTBAPI *
* This is a dummy program designed to show how to create a batch and *
* then apply/change characteristics via BAPI control. *
* Author: NeilinOZ, Adelaide Australia
*---------------------------------------------------------------------*
REPORT ztestbapi.
TABLES: klah, ksml, cabn.
* holds data for charcateristics with type NUM
DATA: BEGIN OF numtab OCCURS 0.
INCLUDE STRUCTURE bapi1003_alloc_values_num.
DATA: END OF numtab.
* holds data for charcateristics with type CHAR/DATE
DATA: BEGIN OF chatab OCCURS 0.
INCLUDE STRUCTURE bapi1003_alloc_values_char.
DATA: END OF chatab.
* holds data for charcateristics with type CURR
DATA: BEGIN OF curtab OCCURS 0.
INCLUDE STRUCTURE bapi1003_alloc_values_curr.
DATA: END OF curtab.
* Error return table
DATA: BEGIN OF rettab OCCURS 0.
INCLUDE STRUCTURE bapiret2.
DATA: END OF rettab.
DATA: BEGIN OF it_ksml OCCURS 0.
INCLUDE STRUCTURE ksml.
DATA: END OF it_ksml.
* characteristic names
DATA: BEGIN OF it_cabn OCCURS 0,
atinn LIKE cabn-atinn,
atnam LIKE cabn-atnam,
atfor LIKE cabn-atfor,
END OF it_cabn.
DATA: BEGIN OF it_object OCCURS 0.
INCLUDE STRUCTURE bapi1003_object_keys.
DATA: END OF it_object.
* example of an object key 00000000000730000007505847897897
DATA: w_object LIKE bapi1003_key-object.
* Change these defaults to suit your system
PARAMETERS: p_matnr LIKE mara-matnr OBLIGATORY DEFAULT '7300000',
p_werks LIKE t001w-werks OBLIGATORY DEFAULT '0750',
p_charg LIKE mcha-charg OBLIGATORY DEFAULT '5847897898',
p_lgort LIKE dfbatch-lgort OBLIGATORY DEFAULT '1000',
p_klart LIKE klah-klart OBLIGATORY DEFAULT '022',
p_class LIKE klah-class OBLIGATORY DEFAULT 'RETREAD'.
*---------------------------------------------------------------------*
* FORM get_characteristics *
*---------------------------------------------------------------------*
FORM get_characteristics.
* extract the characteristic names for KLART/CLASS
CLEAR: klah, ksml, it_cabn.
REFRESH it_cabn.
SELECT SINGLE clint FROM klah
INTO CORRESPONDING FIELDS OF klah
WHERE klart = p_klart
AND class = p_class.
IF syst-subrc = 0.
SELECT * FROM ksml
INTO CORRESPONDING FIELDS OF ksml
WHERE clint = klah-clint.
SELECT SINGLE * FROM cabn
INTO CORRESPONDING FIELDS OF cabn
WHERE atinn = ksml-imerk.
IF syst-subrc = 0.
it_cabn-atinn = cabn-atinn.
it_cabn-atnam = cabn-atnam.
it_cabn-atfor = cabn-atfor.
APPEND it_cabn.
ENDIF.
ENDSELECT.
ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
* FORM create_batch *
*---------------------------------------------------------------------*
FORM create_batch.
CLEAR: rettab.
REFRESH: rettab.
* create the batch using screen values, you will most likely have to
* determine the next batch number from the number range.
CALL FUNCTION 'BAPI_BATCH_CREATE'
EXPORTING
material = p_matnr
batch = p_charg
plant = p_werks
batchstoragelocation = p_lgort
TABLES
return = rettab.
ENDFORM.
*---------------------------------------------------------------------*
* FORM build_object_key *
*---------------------------------------------------------------------*
FORM build_object_key.
ENDFORM.
*---------------------------------------------------------------------*
* FORM extract_original_batch *
*---------------------------------------------------------------------*
FORM extract_original_batch.
* extract the original characteristic data if exists
CLEAR: numtab, chatab, curtab, rettab.
REFRESH: numtab, chatab, curtab, rettab.
*---------------------------------------------------------------------*
* FORM update_original_batch *
*---------------------------------------------------------------------*
FORM update_original_batch.
* Below are some examples of characteristics to update, obviously you
* would change these to match the ones in your system
* ATNAM is the characteristic name.
* find characteristic format in it_cabn and call update routine
READ TABLE it_cabn WITH KEY atnam = 'TREAD'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam 'HW203'.
READ TABLE it_cabn WITH KEY atnam = 'ORRETREAD'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '11225HW203C'.
READ TABLE it_cabn WITH KEY atnam = 'CASINGCODE'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '7100044'.
READ TABLE it_cabn WITH KEY atnam = 'CASINGSTATUS'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam 'CUSTOMER'.
READ TABLE it_cabn WITH KEY atnam = 'CASEVAL'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '0'.
READ TABLE it_cabn WITH KEY atnam = 'CUSTOMER'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '0276933500'.
READ TABLE it_cabn WITH KEY atnam = 'FROMLOC'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '0584'.
READ TABLE it_cabn WITH KEY atnam = 'BRAND'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam 'BRI'.
READ TABLE it_cabn WITH KEY atnam = 'NOCAPS'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '1'.
READ TABLE it_cabn WITH KEY atnam = 'RETURNLOC'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '0584'.
READ TABLE it_cabn WITH KEY atnam = 'RECDATE'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '26.07.2004'.
READ TABLE it_cabn WITH KEY atnam = 'SENTDATE'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '26.07.2004'.
READ TABLE it_cabn WITH KEY atnam = 'FLOC'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '0750'.
READ TABLE it_cabn WITH KEY atnam = 'AUTOCLAVE'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam '4'.
READ TABLE it_cabn WITH KEY atnam = 'REPAIRCODE'.
PERFORM update_table USING it_cabn-atfor it_cabn-atnam 'MINOR'.
* perform the update
PERFORM bapi_change.
ENDFORM.
*---------------------------------------------------------------------*
* FORM update_table *
*---------------------------------------------------------------------*
FORM update_table USING atfor atnam value.
* depending on data format, start building the characteristics table
* ready for update
CASE atfor.
WHEN 'NUM'.
READ TABLE numtab WITH KEY charact = atnam.
IF syst-subrc = 0.
numtab-value_from = value.
MODIFY numtab INDEX syst-tabix.
ELSE.
numtab-charact = atnam.
numtab-value_from = value.
APPEND numtab.
ENDIF.
WHEN 'CURR'.
WHEN 'CHAR' OR 'DATE'.
READ TABLE chatab WITH KEY charact = atnam.
IF syst-subrc = 0.
chatab-value_neutral = value.
MODIFY chatab INDEX syst-tabix.
ELSE.
chatab-charact = atnam.
chatab-value_neutral = value.
APPEND chatab.
ENDIF.
ENDCASE.
ENDFORM.
*---------------------------------------------------------------------*
* FORM bapi_change *
*---------------------------------------------------------------------*
FORM bapi_change.
* Apply the characteristics to the batch.
CALL FUNCTION 'BAPI_OBJCL_CHANGE'
EXPORTING
objectkey = w_object
objecttable = 'MCHA'
classnum = p_class
classtype = p_klart
TABLES
allocvaluesnumnew = numtab
allocvaluescharnew = chatab
allocvaluescurrnew = curtab
return = rettab.
ENDFORM.
*---------------------------------------------------------------------*
* FORM bapi_commit *
*---------------------------------------------------------------------*
FORM bapi_commit.
* commit the changes
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ENDFORM.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.