cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to create sales order. web service method.

Former Member
0 Kudos

Hi guys, I have trouble creating an sales order. i have created a web service, data information are passed into this method but it did not process. Order is not created in database. Can anyone please help me with this?

Public Function CreateItemOrder2(ByVal OrderDataSet As DataSet) As String

Dim lngRetVal As Long

Dim strReturn As String = ""

Dim oDocument As SAPbobsCOM.Documents = Nothing

'Dim oUDF As SAPbobsCOM.UserFields

Dim dtOrdHeader As DataTable = Nothing

Dim dtOrdLines As DataTable = Nothing

Dim drOrdHeader As DataRow

Dim drOrdLines As DataRow

Dim oCompany As SAPbobsCOM.Company

Dim IngRow As Long = 0

Try

SetConnectionParam(***)

dtOrdHeader = OrderDataSet.Tables("OrderHeader")

dtOrdLines = OrderDataSet.Tables("ItemLines")

If ConnectToDatabase() Then

oCompany = Session("g_CompanyConnection")

oDocument = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)

oDocument.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Items

If dtOrdHeader.Rows.Count > 0 Then

drOrdHeader = dtOrdHeader.Rows(0)

'set values to the fields

'Set values to the fields

oDocument.CardCode = drOrdHeader("CardCode").ToString()

oDocument.DocDueDate = drOrdHeader("DocDueDate").ToString()

oDocument.Address2 = drOrdHeader("ShipTo").ToString()

oDocument.Address = drOrdHeader("BillTo").ToString()

oDocument.DocDate = Today

oDocument.NumAtCard = drOrdHeader("OrderType").ToString()

'oDocument.DocDate = Format(drOrdHeader("DocDate"), "yyyyMMdd")

oDocument.DocTotal = drOrdHeader("DocTotal").ToString()

oDocument.Comments = drOrdHeader("Comments").ToString()

oDocument.CardName = drOrdHeader("CardName").ToString()

oDocument.HandWritten = SAPbobsCOM.BoYesNoEnum.tNO

oUDF = oDocument.UserFields

oUDF.Fields.Item("U_Driver").Value = drOrdHeader,U_driver)

'oDocument.Lines.Add() 'auto add 1 row

lngRetVal = oDocument.Add

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

If dtOrdLines.Rows.Count > 0 Then

Dim i As Integer

i = 0

I suspect this while loop did not work at all..

While dtOrdLines.Rows.Count > i

drOrdLines = dtOrdLines.Rows(i)

'item lines - set value to the first line

oDocument.Lines.ItemCode = drOrdLines("ItemCode").ToString()

oDocument.Lines.LineTotal = drOrdLines("LineTotal").ToString()

oDocument.Lines.Desc = drOrdLines("Desc").ToString()

oDocument.Lines.Price = drOrdLines("Uprice").ToString()

oDocument.Lines.Quantity = drOrdLines("Quantity").ToString()

oDocument.Lines.Add()

i = i + 1

End While

lngRetVal = oDocument.Add

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

End If

Catch ex As Exception

Session("g_LastErrorMsg") = ex.Message

Finally 'no matter above is success or failed must execute this portion

oDocument = Nothing 'release the resource/obj variable

End Try

Return strReturn

End Function

Thank alot!

Kean

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

A field name was invaild thus the whole order could not be processed. Thank guys for the input!!

It should be:

oDocument.Lines.ItemDescription = drOrdLines("Desc").ToString()

Regard,

Kean

Edited by: kean tan on Jun 19, 2008 9:32 AM

Former Member
0 Kudos

Hi Kean,

I would like to add something here. when you are iterating through the loop to insert Order details, you should not write the following line when you come to the last record.


oDocument.Lines.Add()

that is, if you have 5 records to fill in details, in while loop, when you reach fifth record, it should not be followed by the above line, as it will add one more row which will not be filled. and you can't add an empty row.

regards,

Binita Joshi

Former Member
0 Kudos

Hi guys, thank for the reply. I have tried editing but still it did not work either.

<WebMethod(EnableSession:=True, Description:="Test Function : Use this method to create Item Invoice. ")> _

Public Function CreateItemOrder2(ByVal OrderDataSet As DataSet) As String

Dim lngRetVal As Long

Dim strReturn As String = ""

Dim oDocument As SAPbobsCOM.Documents = Nothing

'Dim oUDF As SAPbobsCOM.UserFields

Dim dtOrdHeader As DataTable = Nothing

Dim dtOrdLines As DataTable = Nothing

Dim drOrdHeader As DataRow

Dim drOrdLines As DataRow

Dim oCompany As SAPbobsCOM.Company

Dim IngRow As Long = 0

Try

SetConnectionParam("***")

dtOrdHeader = OrderDataSet.Tables("OrderHeader")

dtOrdLines = OrderDataSet.Tables("ItemLines")

If ConnectToDatabase() Then

oCompany = Session("g_CompanyConnection")

oDocument = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)

oDocument.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Items

If dtOrdHeader.Rows.Count > 0 Then

drOrdHeader = dtOrdHeader.Rows(0)

'set values to the fields

'Set values to the fields

oDocument.CardCode = drOrdHeader("CardCode").ToString()

oDocument.DocDueDate = drOrdHeader("DocDueDate").ToString()

oDocument.Address2 = drOrdHeader("ShipTo").ToString()

oDocument.Address = drOrdHeader("BillTo").ToString()

oDocument.DocDate = Today

oDocument.NumAtCard = drOrdHeader("OrderType").ToString()

'oDocument.DocDate = Format(drOrdHeader("DocDate"), "yyyyMMdd")

oDocument.DocTotal = drOrdHeader("DocTotal").ToString()

oDocument.Comments = drOrdHeader("Comments").ToString()

oDocument.CardName = drOrdHeader("CardName").ToString()

oDocument.HandWritten = SAPbobsCOM.BoYesNoEnum.tNO

'oUDF = oDocument.UserFields

'oUDF.Fields.Item("U_Driver").Value = drOrdHeader,U_driver)

'oDocument.Lines.Add() 'auto add 1 row

'lngRetVal = oDocument.Add '<----


removed

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

If dtOrdLines.Rows.Count > 0 Then

Dim i As Integer

i = 0

'I suspect this while loop did not work at all..

While dtOrdLines.Rows.Count > i

drOrdLines = dtOrdLines.Rows(i)

'item lines - set value to the first line

oDocument.Lines.ItemCode = drOrdLines("ItemCode").ToString()

oDocument.Lines.LineTotal = drOrdLines("LineTotal").ToString()

oDocument.Lines.Desc = drOrdLines("Desc").ToString()

oDocument.Lines.Price = drOrdLines("Uprice").ToString()

oDocument.Lines.Quantity = drOrdLines("Quantity").ToString()

i = i + 1

If Not i = dtOrdLines.Rows.Count Then Added this part

oDocument.Lines.Add()

End If

End While

lngRetVal = oDocument.Add '<----- the same document is added here, here is where the document should actually be added

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

End If

Catch ex As Exception

Session("g_LastErrorMsg") = ex.Message

Finally 'no matter above is success or failed must execute this portion

oDocument = Nothing 'release the resource/obj variable

End Try

Return strReturn

End Function

Thank You,

Kean

Former Member
0 Kudos

Kean,

what's the value of lngRetVal that you get? I mean, what is the error message?

Binita

Former Member
0 Kudos

Hi Binita, no error was generated.

Former Member
0 Kudos

Kean,

thats weird. do you want to say that you get to reach the following line:


If lngRetVal = 0 Then
Session("g_LastErrorMsg") = " "

and get lngRetVal = 0?

a little work on your part but would suggest you to do the following:

1) to ensure that services are running, try creating one sales order with hardcoded parameters.

2) if lngRetVal is zero then you must get some value for either GetNewObjectKey or GetNewObjectCode method.

3) if tht does not lead any where, last, try making a sales order in SAP B1 through UI with the same parameters. may be series, exchange rate or authorization could be the issue.

regards,

Binita

Former Member
0 Kudos

Hi Kean,

I noticed that in the code the document is added before the document_lines are added.

Please check the lines :

'oDocument.Lines.Add() 'auto add 1 row

lngRetVal = oDocument.Add

Probably the above lines can be avoided.

Further more, are you getting any errors and also please do let us know about the SAP Business One Version and als the SDK version.

Do try the following :

1. If you try the same code without the webservice, does it work through the DI.

2. try placing some checks and find out till when the code is working, you might want to use msgboxes for the same.

Let us know the results of the above to further know about the scenario.

REgards

Arshdeep

Former Member
0 Kudos

Hi Arshdeep, thank for the reply!

SBO version is 2005

Version PL5 of DIAPI

<WebMethod(EnableSession:=True, Description:="Test Function : Use this method to create Item Invoice. ")> _

Public Function CreateItemOrder2(ByVal OrderDataSet As DataSet) As String

Dim lngRetVal As Long

Dim strReturn As String = ""

Dim oDocument As SAPbobsCOM.Documents = Nothing

'Dim oUDF As SAPbobsCOM.UserFields

Dim dtOrdHeader As DataTable = Nothing

Dim dtOrdLines As DataTable = Nothing

Dim drOrdHeader As DataRow

Dim drOrdLines As DataRow

Dim oCompany As SAPbobsCOM.Company

Dim IngRow As Long = 0

Dim stro As String

stro = ""

Try

SetConnectionParam(**)

dtOrdHeader = OrderDataSet.Tables("OrderHeader")

dtOrdLines = OrderDataSet.Tables("ItemLines")

If ConnectToDatabase() Then

oCompany = Session("g_CompanyConnection")

oDocument = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)

oDocument.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Items

If dtOrdHeader.Rows.Count > 0 Then

drOrdHeader = dtOrdHeader.Rows(0)

'set values to the fields

'Set values to the fields

oDocument.CardCode = drOrdHeader("CardCode").ToString()

oDocument.DocDueDate = drOrdHeader("DocDueDate").ToString()

oDocument.Address2 = drOrdHeader("ShipTo").ToString()

oDocument.Address = drOrdHeader("BillTo").ToString()

oDocument.DocDate = Today

oDocument.NumAtCard = drOrdHeader("OrderType").ToString()

'oDocument.DocDate = Format(drOrdHeader("DocDate"), "yyyyMMdd")

oDocument.DocTotal = drOrdHeader("DocTotal").ToString()

oDocument.Comments = drOrdHeader("Comments").ToString()

oDocument.CardName = drOrdHeader("CardName").ToString()

stro = stro + " Pass" + drOrdHeader("CardName").ToString() + " " + drOrdHeader("DocDueDate").ToString() + " "

oDocument.HandWritten = SAPbobsCOM.BoYesNoEnum.tNO

'oUDF = oDocument.UserFields

'oUDF.Fields.Item("U_Driver").Value = drOrdHeader,U_driver)

'oDocument.Lines.Add() 'auto add 1 row

lngRetVal = oDocument.Add

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

If dtOrdLines.Rows.Count > 0 Then

Dim i As Integer

i = 0

stro = stro + " " + dtOrdLines.Rows.Count.ToString()

While dtOrdLines.Rows.Count > i

stro = stro + " In" <--- Code till here work fine.

drOrdLines = dtOrdLines.Rows(i)

'item lines - set value to the first line

oDocument.Lines.ItemCode = drOrdLines("ItemCode").ToString()

oDocument.Lines.LineTotal = drOrdLines("LineTotal").ToString()

oDocument.Lines.Desc = drOrdLines("Desc").ToString()

oDocument.Lines.Price = drOrdLines("Uprice").ToString()

oDocument.Lines.Quantity = drOrdLines("Quantity").ToString()

stro = stro + " abc " <-------not displayed

i = i + 1

End While

lngRetVal = oDocument.Add

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

End If

Catch ex As Exception

Session("g_LastErrorMsg") = ex.Message

Finally 'no matter above is success or failed must execute this portion

oDocument = Nothing 'release the resource/obj variable

End Try

strReturn = stro

Return strReturn

End Function

Edited by: kean tan on Jun 13, 2008 6:53 AM

Edited by: kean tan on Jun 13, 2008 7:05 AM

Former Member
0 Kudos

Hi Kean,

Please see the comments in Bold

*****************************************

Public Function CreateItemOrder2(ByVal OrderDataSet As DataSet) As String

Dim lngRetVal As Long

Dim strReturn As String = ""

Dim oDocument As SAPbobsCOM.Documents = Nothing

'Dim oUDF As SAPbobsCOM.UserFields

Dim dtOrdHeader As DataTable = Nothing

Dim dtOrdLines As DataTable = Nothing

Dim drOrdHeader As DataRow

Dim drOrdLines As DataRow

Dim oCompany As SAPbobsCOM.Company

Dim IngRow As Long = 0

Try

SetConnectionParam(***)

dtOrdHeader = OrderDataSet.Tables("OrderHeader")

dtOrdLines = OrderDataSet.Tables("ItemLines")

If ConnectToDatabase() Then

oCompany = Session("g_CompanyConnection")

oDocument = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)

oDocument.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Items

If dtOrdHeader.Rows.Count > 0 Then

drOrdHeader = dtOrdHeader.Rows(0)

'set values to the fields

'Set values to the fields

oDocument.CardCode = drOrdHeader("CardCode").ToString()

oDocument.DocDueDate = drOrdHeader("DocDueDate").ToString()

oDocument.Address2 = drOrdHeader("ShipTo").ToString()

oDocument.Address = drOrdHeader("BillTo").ToString()

oDocument.DocDate = Today

oDocument.NumAtCard = drOrdHeader("OrderType").ToString()

'oDocument.DocDate = Format(drOrdHeader("DocDate"), "yyyyMMdd")

oDocument.DocTotal = drOrdHeader("DocTotal").ToString()

oDocument.Comments = drOrdHeader("Comments").ToString()

oDocument.CardName = drOrdHeader("CardName").ToString()

oDocument.HandWritten = SAPbobsCOM.BoYesNoEnum.tNO

oUDF = oDocument.UserFields

oUDF.Fields.Item("U_Driver").Value = drOrdHeader,U_driver)

'oDocument.Lines.Add() 'auto add 1 row

lngRetVal = oDocument.Add <----


Once the document is added here , try removing this line

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

If dtOrdLines.Rows.Count > 0 Then

Dim i As Integer

i = 0

I suspect this while loop did not work at all..

While dtOrdLines.Rows.Count > i

drOrdLines = dtOrdLines.Rows(i)

'item lines - set value to the first line

oDocument.Lines.ItemCode = drOrdLines("ItemCode").ToString()

oDocument.Lines.LineTotal = drOrdLines("LineTotal").ToString()

oDocument.Lines.Desc = drOrdLines("Desc").ToString()

oDocument.Lines.Price = drOrdLines("Uprice").ToString()

oDocument.Lines.Quantity = drOrdLines("Quantity").ToString()

oDocument.Lines.Add()

i = i + 1

End While

lngRetVal = oDocument.Add <----- the same document is added here, here is where the document should actually be added

'Check the result

If lngRetVal = 0 Then

Session("g_LastErrorMsg") = " "

oCompany.GetNewObjectCode(strReturn) 'get last doc

Else

Session("g_LastErrorMsg") = oCompany.GetLastErrorDescription

End If

Else

Session("g_LastErrorMsg") = "No record found in data set"

End If

End If

Catch ex As Exception

Session("g_LastErrorMsg") = ex.Message

Finally 'no matter above is success or failed must execute this portion

oDocument = Nothing 'release the resource/obj variable

End Try

Return strReturn

End Function

***************************************************

Regards

Arshdeep