Dim apiUsername As String = "removed"
Dim apiPassword As String = "removed"
Dim integrationCode As String = "removed"
Dim apiEndpoint As String = "https://webservices16.autotask.net/atservicesrest/V1.0/Companies"
Dim request As New HTTPSocket
request.SetRequestContent("", "application/json")
request.SetRequestHeader("ApiIntegrationCode", integrationCode)
request.SetRequestHeader("UserName", apiUsername)
request.SetRequestHeader("Secret", apiPassword)
If TextField1.Text = "" Or TextField2.Text = "" Or TextField3.Text = "" Or TextField4.Text = "" Or TextField5.Text = "" Or TextField6.Text = "" Then
MsgBox("Please fill in all fields.")
Return
End If
Dim requestBody As New Dictionary
requestBody.Value("companyType") = 1
requestBody.Value("ownerResourceID") = 4
requestBody.Value("companyName") = TextField1.Text
requestBody.Value("Phone") = TextField2.Text
requestBody.Value("WebAddress") = TextField3.Text
requestBody.Value("City") = TextField4.Text
requestBody.Value("State") = TextField5.Text
requestBody.Value("CountryID") = TextField6.Text
Dim requestBodyJson As Text = Xojo.Data.GenerateJSON(requestBody)
request.SetRequestContent(requestBodyJson, "application/json")
request.Post(apiEndpoint, AddressOf HandleResponse)
Sub HandleResponse(responseText As Text)
Dim responseJson As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(responseText)
If responseJson.HasKey("itemId") Then
MsgBox("Customer created successfully!")
Else
MsgBox("Error: " + responseJson.Value("message"))
End If
End Sub
What am I Doing wrong
Remove the code shown above.
That will get rid of the error
Then add a new method called HandleResponse
Give it a parameter of responseText as Text
then paste into that
Dim responseJson As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(responseText)
If responseJson.HasKey("itemId") Then
MsgBox("Customer created successfully!")
Else
MsgBox("Error: " + responseJson.Value("message"))
End If
I dont think I understand when you say I have posted 2 bits of code?
There is only one code there?
Are you saying to get rid of the whole thing of
Sub HandleResponse(responseText As Text)
Dim responseJson As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(responseText)
If responseJson.HasKey(āitemIdā) Then
MsgBox(āCustomer created successfully!ā)
Else
MsgBox("Error: " + responseJson.Value(āmessageā))
End If
End Sub
Not sure i understand what you are saying it looks the same to me
you wrote the contents of a sub. Xojo adds Subā¦End Sub and asks you to provide hat is in between. Buts after that line at the start of my answer you have
that is you define another Sub. That means once Xojo adds Sub⦠End Sub, there are two End Sub, hence the error message.
This code represents a method that must be separate from the rest of the code. Select it, cut it, select the module or class youāre editing in the Navigator and Paste.
Step by step, both I and now Greg have explained what to do.
Remove the chunk of code that starts with Sub and ends with End Sub. (copy it first)
Then click on the module or window or class you are working on (not inside any visible code), and choose āPasteā
or
Right click, āadd methodā, and recreate that subroutine by typing it in.
Why do you create the Json with Xojo.Data.GenerateJson? The first thing to check is the data in the TextFields. Is there anything in there that needs to be escaped?
Blockquote
Why do you create the Json with Xojo.Data.GenerateJson? The first thing to check is the data in the TextFields. Is there anything in there that needs to be escaped?
I donāt see any value in using a dictionary and then converting it to json here. Just use the json. This is straight out of the documentation:
dim person As New JSONItem
// This object is manipulated like a dictionary
person.Value(āNameā) = āJohn Doeā
person.Value(āAgeā) = 32
person.Value(āMarriedā) = True
person.Value(āSpouseā) = āJane Doeā
Ok, thanks. I really donāt know what I am doing, I am still learning so thought that may have been the correct way. i will try your approach though ⦠Thank you
Do I still need the rest of the code then?
I changed it to this but still fails on the following line
Dim requestBodyJson As Text = Xojo.Data.GenerateJSON(requestBody)
Dim requestBody as New JSONItem
requestBody.Value("companyType") = 1
requestBody.Value("ownerResourceID") = 4
requestBody.Value("companyName") = TextField1.Text
requestBody.Value("Phone") = TextField2.Text
requestBody.Value("WebAddress") = TextField3.Text
requestBody.Value("City") = TextField4.Text
requestBody.Value("State") = TextField5.Text
requestBody.Value("CountryID") = TextField6.Text
Dim requestBodyJson As Text = Xojo.Data.GenerateJSON(requestBody)
request.SetRequestContent(requestBodyJson, "application/json")
Do I still need the last 2 lines ? Or is there a better way to do this
That makes no sense. Looks like you just donāt know what youāre doing, not learning. First learn, then do it, from there you see āmistakesā and have few founded doubts, and wonāt say āI have no clue what Iām doingā.
Where did you get this code that you have no clue how it works?