2007 Dec 19 12:26 PM
I have created to short vb.net program to test the goods movement bapi. The code returns a material document and a year but this does not exist in SAP.
Do I need a commit or have I made a different error?
John.
my code:
Imports SAPBAPIControlLib
Imports SAPTableFactoryCtrl
Module Module1
Dim oGoodsmvtHeader As Object
Dim oGoodsmvt As Object
Dim oGoodsmvtCode As Object
Dim oGoodsmvtItem As Object
Dim oGoodsmvtSerialnumber As Object
Dim oTestrun As Object
Dim oGoodsmvtHeadret As Object
Dim oReturn As Object
Dim oBAPICtrl As Object
Dim boMove As Object
Sub Main()
Call axm()
End Sub
Sub axm()
Dim oBAPICtrl As Object
Dim oLogonCtrl As Object
Dim strTID As String
Console.WriteLine("Create Objects")
oBAPICtrl = CreateObject("SAP.BAPI.1")
oLogonCtrl = CreateObject("SAP.Logoncontrol.1")
oBAPICtrl.Connection = oLogonCtrl.NewConnection
Console.WriteLine("open logon screen")
If oBAPICtrl.Connection.Logon() = False Then
MsgBox("R/3 Connection failed")
End
End If
Console.WriteLine("connected")
oGoodsmvt = oBAPICtrl.GetSAPObject("GoodsMovement")
oGoodsmvtHeader = oBAPICtrl.DimAs(oGoodsmvt, "CreateFromData", "GoodsmvtHeader")
oGoodsmvtCode = oBAPICtrl.DimAs(oGoodsmvt, "CreateFromData", "GoodsmvtCode")
oGoodsmvtItem = oBAPICtrl.DimAs(oGoodsmvt, "CreateFromData", "GoodsmvtItem")
oGoodsmvtSerialnumber = oBAPICtrl.DimAs(oGoodsmvt, "CreateFromData", "GoodsmvtSerialnumber")
oReturn = oBAPICtrl.DimAs(oGoodsmvt, "CreateFromData", "Return")
oGoodsmvtHeadret = oBAPICtrl.DimAs(oGoodsmvt, "CreateFromData", "GoodsmvtHeadret")
oReturn = oBAPICtrl.DimAs(oGoodsmvt, "CreateFromData", "Return")
Console.WriteLine("Objects defined")
oGoodsmvtHeader.Value("PSTNG_DATE") = "20071218"
oGoodsmvtHeader.Value("DOC_DATE") = "20071218"
oGoodsmvtHeader.Value("PR_UNAME") = "FNGJ"
oGoodsmvtHeader.Value("HEADER_TXT") = "BAPI test"
oGoodsmvtCode.Value("GM_CODE") = "04"
oGoodsmvtItem.Rows.Add()
oGoodsmvtItem.Value(1, "MATERIAL") = "ZE0512-550"
oGoodsmvtItem.Value(1, "PLANT") = "1111"
oGoodsmvtItem.Value(1, "STGE_LOC") = "0000"
oGoodsmvtItem.Value(1, "MOVE_TYPE") = "301"
oGoodsmvtItem.Value(1, "ENTRY_QNT") = 1
oGoodsmvtItem.Value(1, "QUANTITY") = 1
oGoodsmvtItem.Value(1, "MOVE_PLANT") = "1111"
oGoodsmvtItem.Value(1, "MOVE_STLOC") = "0001"
'oGoodsmvtItem.value(1, "MOVE_REAS") = "Test"
Console.WriteLine("Values Assigned")
oGoodsmvt.CreateFromData(GoodsmvtHeader:=oGoodsmvtHeader, _
GoodsmvtCode:=oGoodsmvtCode, _
goodsmvtItem:=oGoodsmvtItem, _
GoodsmvtSerialnumber:=oGoodsmvtSerialnumber, _
Testrun:="", _
GoodsmvtHeadret:=oGoodsmvtHeadret, _
Return:=oReturn)
Console.WriteLine("Doc id: " + oGoodsmvtHeadret.value("MAT_DOC"))
Console.WriteLine("Doc yr: " + oGoodsmvtHeadret.value("DOC_YEAR"))
'For Each boReturn In oReturn
' Console.WriteLine("Return Type:" + boReturn.value("TYPE"))
' Console.WriteLine(boReturn.value("MESSAGE_V1"))
' Console.WriteLine(boReturn.value("MESSAGE_V2"))
' Console.WriteLine(boReturn.value("MESSAGE_V3"))
' Console.WriteLine(boReturn.value("MESSAGE_V4"))
' Console.WriteLine(boReturn.value("PARAMETER"))
' Console.WriteLine(boReturn.value("FIELD"))
' Console.WriteLine(boReturn.value("ROW"))
'Next
strTID = oBAPICtrl.CreateTransactionID
Console.WriteLine(strTID)
Console.ReadLine()
oBAPICtrl.Connection.Logoff()
End Sub
End Module
2007 Dec 19 1:25 PM
Hi John,
I'm not sure exactlty which BAPI you are calling (I don't understand vb.net and can't see a recognisable BAPI name in your code!) but I'm 99% positive that yes you were correct, you will need to call a commit after the BAPI call. The BAPI will work correctly without this and pass a material document as you are seeing but won't actually commit the data to the DB.
You have 2 options:-
1. Get an ABAPer to create a custom BAPI that includes the standard goods movement BAPI followed by BAPI_TRANSACTION_COMMIT all packaged up so you still only have 1 object to call.
2. Call the BAPI_TRANSACTION_COMMIT straight after the goods movement call.
Hope this helps,
Gareth.
2007 Dec 20 8:58 AM
Hi All,
Thanks for your input Gareth. The bapi that is being called is 'BAPI_GOODSMVT_CREATE'. The object exposed is GoodsMovement and the method is CreateFromData.
I am probably missing something simple as this my first attempt to do this. I have not seen an example of how to send a commit instruction from VB.
All help appreciated.
Regards,
John.
2007 Dec 20 9:27 AM
Hi John,
I suspected it might be that BAPI - to confirm then, it does NOT perform a commit itself so you will have to do so manually in the call to SAP. On the SAP system there is another BAPI called BAPI_TRANSACTION_COMMIT which should be called directly after your goods movment call assuming there were no errors returned from it. I don't know how to call multiple BAPI's via VB (I don't know anything about VB in fact!) so can only suggest searching or seeing if there is anything on help.sap.com sorry. The other alternative (if you have a friendly ABAPer handy) is to create a custom BAPI that wraps both the goods movement and the commit calls together into one and performs the commit if the goods movement is created without errors. This means just one call to SAP and is a solution I often use when calling SAP from Java.
Hope this helps,
Gareth.
2007 Dec 20 9:52 AM
Hi Gareth,
Do you have some sample code showing how you call the commit proceedure from Java. I should be able to translate this into VB or C#.
If possible I would like to keep all my code in one place.
Thanks,
John.
2007 Dec 20 3:47 PM
Hi All,
I have not yet figured out how to commit my transaction if anybody can help.
Regards,
John.
2007 Dec 20 4:25 PM
I eventually found the solution. For those who are interested the object is BapiService and the method is TransactionCommit.
Regards,
John.