Getting Syntax Error

I am getting a Syntax error in this code.

It says Syntax Erros End Sub.

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 

Looks like you have pasted two bits of code here.

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

It looks like you are trying to define a method inside a method which is not allowed.

1 Like

Would you mind helping with the code on what I am meant to do; I am new to XOJO and so not sure what I am meant to do

Thanks
Chris

From the first line to this one

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.

Ok I am relly sorry for being so dumb. WHat must i do to get it working, can someone help me with with writing the correct code here

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.

Did you post your actual username and password? You might want to edit the original post or change at least the password.

The credentials didn’t look valid, but I removed them anyway. Thanks for pointing it out, @Beatrix_Willius .

Yes the creds were fake. I made them all up

Thanks Jeff. I am new to Xojo so did not understand what you meant. But it makes sense now.

Its working ok now thank you

When you want to add a Method, go to the Insert Menu → Method.

If this is a Function, in the Method definition, you can set a Return value: this defines the Function.

And, you can add Methods to:
App,
Window,
Module.

The documentation is your friend.

Hi all . Thnak you for your help. I thought this was working but not now.

It gives this error
CleanShot 2023-04-18 at 12.05.52

This is the line it stops on.

I do have the method now set up (as recommended)

What is wrong here,

I am new to XOJO and programming and this is my first attempt at doing this kind of thing so any help would be greatly appreciated

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?

What should I be using instead?

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?