Working with Business Objects in SAP获取BO方法及属性.docx
《Working with Business Objects in SAP获取BO方法及属性.docx》由会员分享,可在线阅读,更多相关《Working with Business Objects in SAP获取BO方法及属性.docx(19页珍藏版)》请在冰豆网上搜索。
WorkingwithBusinessObjectsinSAP获取BO方法及属性
BySwetabhShukla,Infosys
BusinessObjecttypecontainsmethods,attributesandeventswhichgiveacomponentbasedviewofanyBusinessProcess.
ForexamplewehaveaPurchaseOrderbusinessprocess.Thisprocessincludesvarioussmallfunctionalities.ItwillhaveprocessofchangingPurchaseordersbaseduponrequirements,functionalitytoretrievethedetailsofpurchaseorderetc.Eachofthesefunctionalitiesisencapsulatedinsideamethod.ThemethodcancallthefunctionmodulespresentinR3systemorcanhavethereowncodetoexecutesomefunctionality.SoifweconsiderPurchaseOrderasaBusinessObjectthenitwillbeidentifiedbykeyfieldPurchaseOrdernumber.Eachpurchaseorderbusinessobjectbaseduponkeyfieldpurchaseordernumberisdifferent.It’ssobecauseeachpurchaseorderisdifferentandwillcontaindifferentdetails.
Sothemethodscontainbusinessfunctionality.AttributesarejustlikepropertiesoftheBusinessobject. ForexampleforanypurchaseorderPurchasingGroup,PurchasingOrganizationetcareattributes.
Technicallywecansaythatbusinessobjecttypesarejustlikeanytemplate.AtruntimeweinstantiateorcreateruntimeobjectsforanyBO(BusinessObject)typebaseduponthekeyfieldswepass.TworuntimeBusinessobjectsofsameBOtypearedifferentfromeachotherbaseduponthekeyfieldswepass.SothekeyfieldisthedifferentiatingfactorfortwoormoreruntimebusinessobjectsofsameBOtype.
ToBrowseforBOopentcodeSWO2(Path->Tools->ABAPWorkbench->Overview->BusinessObjectBrowser).
YoucanexpandthenodesandcancheckvariousbusinessobjectsinBOR.YoucandoubleclickontheBOnodeanditwilltakeyoutoBOdisplay(SWO1).
1 WorkingwithBusinessObjectinourprograms
ForcreatingaBusinessObjectanditsattributestherearemanyalreadyexistingtutorials. SointhistutorialwewillnotdiscusscreationofBOanditsattributes.
1.1 TocreateinstanceofaBO
TocreateaninstanceofBOweneedtohavethekeyfieldsofthatBO.KeyfieldswillbeusedtouniquelyidentifytheBO.WecanusetheFM'SWO_CREATE'tocreateaninstanceofBOinanyreportprogram.
DATA:
i_objtypeTYPEswo_objtyp,
i_objkeyTYPEswo_typeid,
objectTYPEswo_objhnd.
i_objtype=
i_objkey=
CALLFUNCTION'SWO_CREATE'
EXPORTING
objtype=i_objtype
objkey=i_objkey
IMPORTING
object=object.
Thevariable‘object’willholdruntimeinstanceoftheobjecttype.
Theotherwayistousethemacrosdefinedininclude
INCLUDE.
DATA:
i_objtypeTYPEswo_objtyp,
i_objkeyTYPEswo_typeid,
objectTYPEswc_object.
*CreateinstanceofObjecttype
swc_create_objectobjecti_objtypei_objkey.
InternallythemacrocallstheFMSWO_CREATE.SoyoucaneithergofordirectFMcallorthemacrocall.
1.2 Container
BasicallythetermContainerisusedwithreferencetoBusinessObjectsandWorkflows.TheContaineractuallyholdsimportandexportparametersassociatedwithanymethodofBusinessObjectatruntime.WheneverwearecallinganymethodofBusinessObjectweneedtopopulatecontainerforimportparametersandafterthemethodgetsexecuteditreturnsthevalues(exportparameters)inContainer.
BusinessObjectcontaineristechnicallyoftypeSWCONTstructure.
Here‘ELEMENT’willbenameofthevariable/internaltablewhichcontainerholdsand‘VALUE’willhavecorrespondingvalue.Formultilinevariableorinternaltables,thecontainerwillholdmultiplevalueswithsameelementname.
1.2.1 LetstakeanexampletocheckwhatContaineris
Forexamplelet’screateaBusinessObject‘ZSWE1’withonlyonemethod‘READ’.WewilldiscussthecreationofBOinverybrief.
KeyfieldwillbePurchaseOrdernumber(EKKO_EBELN)forBO.
MethodREADwilltakePurchaseOrdernumberasinputandwillgiveallthePurchaseLineitemsinitinaninternaltable.
Step1:
CreateBOZSWE1fortestpurpose.Youcangiveitanyname.
Step2:
NowcreateamethodREAD
Savethemethod.Nowwewillcreatetheparametersofthemethod.
ImportParameters
PurchaseDocument
EKKO-EBELN
ExportParameter
ITEM
EKKO-EBELP(multiline)
PurchasingDocumentwillbeaimportparameteroftypeEKKO_EBELN.
ITEMwillbeaexportparameteroftypeEKKO_EBELPandwillbeamultilinevariable(internaltable)
SavethemethodandthenClickontheProgrambuttontoimplementthemethod.Inthemethodjustreadallebeln(Purchaselineitems)correspondingtoPOnumberintointernaltableITEMandpassitontocontainerinthemethod.Checkthesnapshotbelow.
CheckthewholecodeoftheBOprogramhere.
*****ImplementationofobjecttypeZSWE1*****
INCLUDE
BEGIN_DATAOBJECT."Donotchange..DATAisgenerated
*onlyprivatemembersmaybeinsertedintostructureprivate
DATA:
"beginofprivate,
"todeclareprivateattributesremovecommentsand
"insertprivateattributeshere...
"endofprivate,
BEGINOFKEY,
PURCHASINGDOCUMENTLIKEEKKO-EBELN,
ENDOFKEY.
END_DATAOBJECT."Donotchange..DATAisgenerated
BEGIN_METHODREADCHANGINGCONTAINER.
DATA:
PURCHASINGDOCUMENTTYPEEKKO-EBELN,
ITEMTYPEEKPO-EBELPOCCURS0.
SWC_GET_ELEMENTCONTAINER'PurchasingDocument'PURCHASINGDOCUMENT.
selectebelpintotableitemfromekpo
whereebeln=PURCHASINGDOCUMENT.
SWC_SET_TABLECONTAINER'Item'ITEM.
END_METHOD.
Step1:
SavethechangesandmakethestatusoftheBOtoimplementedfortestingpurpose.AlsogeneratetheBO.
Step2:
Nowlet’stesttheBO.ForthisputabreakpointinthemethodREADattheselectquery.AlsotakeanyPOnumberwhichwillhavemultiplelineitems.YoucancheckEKKOandEKPOtables.InmycaseIamtakingPO‘4200000017’.InmyR3systemthisparticularPOcontains4lineitemsinEKPOtable.Let’sputabreakpointandcheckthemethod.
Step3:
ExecutetheBOwithkey‘4200000017’
Executethemethodwithimportparameteras‘420000017’
Itwilltakeyoutodebuggingmode.Nowcheckthecontainer.
IndebugmodebeforetheselectstatementtheCONTAINERwillholdonlyimportvariable
NowexecutethelaststatementinthemethodandseethecontentsofCONTAINER(justbeforeexitingthemethod.
SowehaveseenhowtheCONTAINERholdsvalueswhileweareworkingwithBusinessObjectsatruntime.
1.2.2 SomecommonmacrosdefinedinincludeforworkingwithContainers
Functionality
Macro
TowriteasinglelinevariableinContainer
SWC_SET_ELEMENT
ToreadasinglelinevariablefromContainer
SWC_GET_ELEMENT
TowriteamultilinevariableorinternaltableinContainer
SWC_SET_TABLE
ToreadamultilinevariableorinternaltablefromContainer
SWC_GET_TABLE
Toclearthecontainer
SWC_CLEAR_CONTAINER
Furtheryoucanchecktheincludeformoremacros.Alsocheckthemacros.Thesemacroscallsomespecificfunctionmodulesthatyoucanusedirectlyinyourcode.
1.3 CallingaBOMethod/Attributeinreportprograms
WecancreateainstanceofBOmethodusingFM 'SWO_CREATE'ormacro‘SWC_CREATE_OBJECT’.
FirstofallletsconsideraBO'BUS1001006' (StandardMaterial).Wewillcallthemethod‘DISPLAY’.ForthiswewillinstantiatetheBOwithkey‘ZSHUKSWE20’(materialnumber).
TocallamethodorattributeofanyBOwecanusetheFM 'SWO_INVOKE'.WehavetotakecarewhilewecallthisFM.Supposeifwewanttocallanattributedefinedinthemethod,thenweneedtopopulatetheimportparameter ACCESS with ‘G’.IfweneedtocallthemethodoftheBOthenweneedtopopulatetheimportparameter ACCESS with ‘C’.
Letscreateareportprogramandcheckstepbystep.Wewillfetchdetailsofattribute“MATERIALTYPE”ofBO'BUS1001006'
*&---------------------------------------------------------------------*
*&ReportZSWET_BO1
*&
*&---------------------------------------------------------------------*
*&TogetattributesofBOinstanceinreport
*&
*&---------------------------------------------------------------------*
REPORTzswet_bo1.
PARAMETERS:
p_busobj(10)TYPEcDEFAULT'BUS1001006',
p_key(70)TYPEcDEFAULT'ZSHUKSWE20',
p_attr(32)TYPEcDEFAULT'MATERIALTYPE',
p_accessTYPEcDEFAULT'G'."Tocallmethodput'C'
DATA:
i_objtypeTYPEswo_objtyp,
i_objkeyTYPEswo_typeid,
i_elementTYPEswo_verb.
DATAobjectTYPEswo_objhnd.
DATAverbTYPEswo_verb.
DATAreturnTYPEswotreturn.
DATAlt_containerTYPESTANDARDTABLEOFswcont.
DATAlineTYPEswcont.
i_objtype=p_busobj.
i_element=p_attr.
i_objkey=p_key.
*Instantiatethebusinessobject.i.egiveitakeyandcreateit.
CALLFUNCTION'SWO_CREATE'
EXPORTING
objtype=i_objtype
objkey=i_objkey
IMPORTING
object=object.
*Returnattribute.
CALLFUNCTION'SWO_INVOKE'
EXPORTING
access=p_access
object=object
verb=i_element
IMPORTING
return=return
verb=verb
TABLES
container=lt_container.
*TheattributevalueisinthecontainerreturnedfromFM.
IFreturn-code=0.
LOOPATlt_containerINTOline.
WRITE:
/'AttributeMATERIALTYPEis:
',
line-value.
ENDLOOP.
ENDIF.
Letsexecutethereportandseetheoutput:
NowletsseehowtocallamethodofBO.TheMethodDISPLAYwilldisplaythematerialpassedinBOcontainer.
*&---------------------------------------------------------------*
*&ReportZSWET_BO1
*&
*&---------------------------------------------------------------*
*&Tocallmethodwithimportparameters
*&
*&---------------------------------------------------------------*
REPORTz