Learn how to replace the wrapper for the `BAPI_PR_CREATE` function module with the released API `I_PurchaseRequisitionTp` business object interface and maintain the related authorization defaults.
You have completed the previous tutorial in this group, integrated your wrapper in your RAP BO and provided authorizations to users for non-released authorization objects.
The complete scenario showing how to create a RAP BO and integrate a released purchase requisition API is presented in the tutorial group Develop an SAP Fiori App to Trigger Purchase Requisitions API. This tutorial specifically focuses on the transition from using the wrapper to using the released API.
In the previous tutorials of this group you learnt how to mitigate the case of a missing released API to create purchase requisitions, by creating a wrapper and integrating it into your shopping cart RAP business object (RAP BO).
When SAP provides a released API, then it is recommended to use that one.
Therefore, in this tutorial you will learn how to replace the wrapper for the BAPI_PR_CREATE function module with the released I_PurchaseRequisitionTp business object interface.
Throughout this tutorial, wherever ### appears, use a number (e.g. 000). This tutorial is done with the placeholder 000.
When the createpurchrqnbapisave action is used, the shopping cart orders are marked (similar to a checkbox) using the OverallStatus field, and then the actual purchase requisition creation is handled in the unmanaged save implementation in thesave_modified method.
Since in this tutorial we want to use the released API to create purchase requisitions (rather than the BAPI) you will now adapt the createpurchrqnbapisave method and thesave_modified method to replace the call to the BAPI wrapper with an EML call to the released API.
Connect to the system via ADT and navigate to the package Z_PURCHASE_REQ_### containing the RAP BO.
You will first declare some new needed data types and then you will modify the createpurchrqnbapisave method to handle the creation of a purchase requisition for a given shopping cart item using the released purchase requisition API.
Open the global class of the behavior implementation ZBP_SHOPCARTTP_### and add the following code snippet in thepublic section:
You just defined a new table type, which will be used in the behavior implementation to store information on the purchase requisition of a given shopping cart entry. This information consist of a pid, or preliminary key (the released API uses late numbering, so when being called by the application it will only return this preliminary key which is converted into the final key in a later step), a cid (which is needed in the case in which the purchase requisition gets updated), a pur_req (which will store the actual purchase requisition number that will be created), and the order_uuid (which is the id of the given shopping cart entry for a which a purchase requisition number will be created).
Your global class should now look as follows:
Global Class
Save it. Do NOT activate it yet.
The data type you just created will be used in the behavior implementation.
Open the lhc_shopcart class of the behavior implementation and navigate to the createpurchrqnbapisave method. Add the following code snippet after the first READ entities statement.
ABAP
DATA:purchase_requisitionsTYPE TABLEFORCREATEI_PurchaserequisitionTP,purchase_requisitionTYPE STRUCTUREFORCREATEI_PurchaserequisitionTP,purchase_requisition_itemsTYPE TABLEFORCREATEi_purchaserequisitionTP\_PurchaseRequisitionItem,purchase_requisition_itemTYPE STRUCTUREFORCREATEi_purchaserequisitiontp\\purchaserequisition\_purchaserequisitionitem,delivery_dateTYPE I_PurchaseReqnItemTP-DeliveryDate,nTYPE i.LOOP AT onlineordersINTODATA(onlineorder)WHEREOverallStatus=c_overall_status-new.delivery_date=cl_abap_context_info=>get_system_date()+14.n+=1."purchase requisition
DATA(cid)=onlineorder-OrderID&&'_'&&n.purchase_requisition=VALUE#(%cid=cidpurchaserequisitiontype='NB').APPENDpurchase_requisitionTOpurchase_requisitions."purchase requisition item
purchase_requisition_item=VALUE#(%cid_ref=cid%target=VALUE#((%cid=|My%ItemCID_{n}|plant='1010'"Plant 01 (DE)
accountassignmentcategory='U'"unknown
* PurchaseRequisitionItemText = . "retrieved automatically from maintained MaterialInforequestedquantity='1'purchaserequisitionprice='100'purreqnitemcurrency='EUR'Material='D001'materialgroup='A001'purchasinggroup='001'purchasingorganization='1010'DeliveryDate=delivery_date"delivery_date "yyyy-mm-dd (at least 10 days)
CreatedByUser=OnlineOrder-CreatedBy))).APPENDpurchase_requisition_itemTOpurchase_requisition_items.ENDLOOP.IFkeysIS NOT INITIAL."purchase reqn
MODIFYENTITIESOFi_purchaserequisitiontpENTITYpurchaserequisitionCREATEFIELDS(purchaserequisitiontype)WITHpurchase_requisitions"purchase reqn item
CREATEBY\_purchaserequisitionitemFIELDS(plant* purchaserequisitionitemtextaccountassignmentcategoryrequestedquantitybaseunitpurchaserequisitionpricepurreqnitemcurrencyMaterialmaterialgrouppurchasinggrouppurchasingorganizationDeliveryDate)WITHpurchase_requisition_itemsREPORTEDDATA(reported_create_pr)MAPPEDDATA(mapped_create_pr)FAILEDDATA(failed_create_pr).ENDIF.IFmapped_create_prIS NOT INITIAL.LOOP AT onlineordersINTODATA(onlineorder1)WHEREOverallStatus=c_overall_status-new.LOOP AT mapped_create_pr-purchaserequisitionINTODATA(purchaserequisition_details).IFonlineorder1-OrderID=substring_before(val=purchaserequisition_details-%cidsub='_').APPENDVALUE#(cid=purchaserequisition_details-%cidpid=purchaserequisition_details-%pidorder_uuid=onlineorder1-OrderuuID)TOzbp_shopcarttp_###=>purchase_requisition_details.DELETE ADJACENT DUPLICATES FROMzbp_shopcarttp_###=>purchase_requisition_detailsCOMPARINGpid.ENDIF.ENDLOOP.ENDLOOP.ENDIF.
The createpurchrqnbapisave method handles the creation of a purchase requisition for a given shopping cart entry via released API. In the case of mass creation of multiple purchase requisitions, the purchaserequisition_details table handles the mapping between purchase requisition number and its corresponding shopping cart entry.
The createpurchrqnbapisave method should now look like this:
Implementation Code
You will now navigate to the save_modified method, remove the call to the BAPI wrapper and insert the following code snippet with an EML call to the released API:
ABAP
METHODsave_modified.IFcreate-ShoppingCartIS NOT INITIAL.INSERTzashopcart_###FROMTABLE@create-ShoppingCartMAPPINGFROMENTITY.ENDIF.IFupdateIS NOT INITIAL.DATA(creation_date)=cl_abap_context_info=>get_system_date().LOOP AT update-ShoppingCartINTODATA(OnlineOrder1)WHERE%control-OverallStatus=if_abap_behv=>mk-on.LOOP AT zbp_shopcarttp_###=>purchase_requisition_detailsINTODATA(purchase_reqn_via_eml)WHEREpidIS NOT INITIALANDorder_uuid=onlineorder1-OrderUUID.CONVERTKEYOFi_purchaserequisitiontpFROMpurchase_reqn_via_eml-pidTODATA(ls_pr_key1).UPDATEzashopcart_###SETpurchase_requisition=@ls_pr_key1-PurchaseRequisition,overall_status='Submitted / Approved',pr_creation_date=@creation_dateWHEREorder_uuid=@purchase_reqn_via_eml-Order_UUID.ENDLOOP.ENDLOOP.ENDIF.LOOP AT delete-ShoppingCartINTODATA(shoppingcart_delete)WHEREOrderUUIDIS NOT INITIAL.DELETEFROMzashopcart_###WHEREorder_uuid=@shoppingcart_delete-OrderUUID.DELETEFROMzdshopcart_###WHEREorderuuid=@shoppingcart_delete-OrderUUID.ENDLOOP.ENDMETHOD.
As previously explained, the released API I_PurchaserequisitionTP uses late numbering and so, when being called by the application, it will only return a preliminary key %pid. In order to retrieve the final key you have to use the CONVERT KEY keyword.
The save_modified method should now look as follows:
save_modified implementation
Save and activate it.
Step 2Remove `checkPurchaseRequisition` method
+
As explained in a previous tutorial Integrate the Wrapper into the Shopping Cart Business Object it is recommended to use the BAPI test mode as a validation in your RAP BO to check the input data before the entity is modified and the purchase requisition is created. However, this is not needed any longer since the check is done implicitly by the released API.
You will now remove the checkPurchaseRequisition method: open the lhc_shopcart class of your behavior implementation and navigate to the checkpurchaserequisiton method. Comment it out:
Comment out checkpurchaserequisiton method implementation
Comment out the validation in the behavior definition, too:
Save and activate it.
Step 3Switch to strict(2) mode
+
Since you now removed the checkpurchaserequisiton method, no authorization checks are invoked during the save sequence anymore. This means that the RAP BO can be switched to strict(2) mode.
Open the behavior definition ZR_SHOPCARTTP_### and change to strict(2) mode:
Behavior Definition Strict 2
Save and activate it.
Open the behavior definition ZC_SHOPCARTTP_### and change to strict(2) mode there as well:
Behavior Definition Strict 2
Save and activate it.
Step 4Run SAP Fiori Elements Preview
+
In ADT, open the Service Binding ZUI_SHOPCART_O4_### and click on the Preview button to start a preview of the UI of your RAP BO. You will be prompted to login. Create a new entry and then click on the button Create PR via BAPI in SAVE to create the purchase requisition via released API:
In the case of a released API, the authorization objects are released as well and so the mitigation is no longer necessary. In this case you can follow the standard procedure: you will first identify the necessary authorization objects, then you will maintain the corresponding authorization defaults.
The first step is to find out the needed authorization objects for the released API: logon to ADT and click on Navigate->Open ABAP Development Object and search for the purchase requisition behavior definition I_PURCHASEREQUISITIONTP. Open it. From the Properties tab navigate to API State: from here you can open the Authorization Default Values and get information on the needed authorization objects and relative field values (for more information check the Purchase Requisition API documentation). In this case, the needed authorization objects and field values are:
Object
Field Name
Value
M_BANF_BSA
ACTVT
01
02
03
08
BSART
M_BANF_EKG
ACTVT
01
02
03
08
EKGRP
$EKGRP
M_BANF_EKO
ACTVT
01
02
03
08
EKORG
$EKORG
M_BANF_WRK
ACTVT
01
02
03
08
WERKS
$WERKS
Logon to the system via SAP GUI, start transaction SU22 and select SAP Gateway OData V4 Backend Service Group and Assignments from the dropdown menu of the Type of Application field. In the Object Name field input your Service Binding name (ZUI_SHOPCART_WRAPPER_O4_###) and click on the Execute button.
In this step switch to edit mode and click on Object–>Add Objects (Manually). In the pop-up window add the needed authorization objects and click on Continue. The authorization objects will be added but you still need to maintain the relative Authorization Default Values (for more information, please refer to the Purchase Requisition API documentation). Do so and then save it. The warning, which is shown, indicates that Full authorization for authorization object M_BANF_BSA field BSART is granted. It can be ignored in the context of this tutorial. (For more explanation see the long text). The service binding should now look like this:
Service Binding Authorizations
Start transaction SU24 and select SAP Gateway OData V4 Backend Service Group and Assignments from the dropdown menu of the Type of Application field. In the Object Name field input your Service Binding name (ZUI_SHOPCART_WRAPPER_O4_###) and click on the Execute button. Switch to edit mode and click on the SAP Data icon (1), mark the needed authorization objects (2), and then click on Copy SAP Data to SU24 icon (3) in the Maintenance Status for Authorization Objects tab. This will copy the authorization objects, but you still need to copy the authorization defaults values for each object.
Maintain SU24 DataTo do this, click on the Synchronize with SAP data icon (1) for all the authorization objects or click on the Copy SAP Data to SU24 icon (2) in the Authorization Default Values tab.
Maintain Authorization Default ValuesSave it and select a suitable transport request (or create a new one if needed).
Start transaction PFCG, open the role ZR_SHOPCART_### in Edit mode, navigate to the Authorizations tab and click on Expert Mode for Profile Generation and in the pop-up select Read old status and merge with new data and click on execute. You will see that all the authorization objects are automatically added and all the field values are set. Save (1) and then click on the Generate (2) icon to generate the authorization profile.
Generate Profile
Now the Z_USER_### user has the role assigned which contains the service binding and the necessary authorizations to create a purchase requisition via the released API.
You can test it: open the service binding using the Z_USER_### user credentials (we suggest to open it in incognito mode, so that you will be prompted to log in) and try to create a purchase requisition by clicking on the Create PR via BAPI in SAVE button, it should work without errors.
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 6
1. Adapt the Behavior Implementation2. Remove `checkPurchaseRequisition` method3. Switch to strict(2) mode4. Run SAP Fiori Elements Preview5. Maintain Authorization Defaults6. Test yourself
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.