Not Able to Run Code in Backendless Cloud Code

Hi Team,

I’m running a post request that creates a contact in HubSpot using the HubSpot contact API. The API format and request body is correct as expected from HubSpot. Also, this API works fine locally, on postman and creates a Contact in HubSpot. I’m using cloud code and creating a service to run this code. When I click the ‘Invoke’ button I get a 200 OK response but the body field is empty and no Contact is created in HubSpot. I don’t know if I can use cloud code to achieve this functionality or there is another way to do this. Will appreciate your support in providing an early response on this.

Please see screenshots attached here…


Regards,
Techloyce Dev Team

Hello @Kashif_Saleem,

the BackendlessRequest.post method is an async method, it returns a Promise, so you need to await for it. Try putting the async keyword before the createContact() and await before the BackendlessRequest.post:

const response = await BackendlessRequest.post( ... )

Also to set HTTP Headers, use the set method of BackendlessRequest:

const response = await BackendlessRequest.post(url, data)
  .set({ 
     'Authorization': `Bearer ${ accessToken}`, 
     'Content-Type': `application/json`, 
  })

Also, I guess specifying Content-Type may be redundant, you can try without it if it works.

Regards,
Stanislaw

Hey @stanislaw.grin,

I have tried using async/await and setting the headers using set method but still the same problem. I get a 200 OK response. But no contacts are being created on my hubspot account.

I’ve reviewed your logs and identified an error: Error: ReferenceError: BackendlessRequest is not defined

You’re receiving a 200 OK response because you’re catching errors in the catch block and not propagating them to the client. If you remove the try/catch block and only retain your logic, the error will be automatically sent in the response.

Regarding the error itself, I noticed that you’re using BackendlessRequest instead of Backendless.Request. Please try changing it to Backendless.Request and let me know if it works for you.

Best regards,
Stanislaw


Thanks, It’s working now. My next usecase is, upon any user creation in Backendless, it will trigger the creation of a Contact in HubSpot. My approach is creating a beforCreate eventhandler. In that event handler I’m calling the getCurrentuser method to retrieve the user in system data and then passing this data in createContact function. But the event isn’t working and no conatcs are created.

There is no current user in the beforeCreate handler. I recommend using afterCreate handler and use the res.result value which will contain the registered user.

Backendless.ServerCode.Persistence.afterCreate(async function(req, res) {
  // you code here
}); 

Also there is no need to create the contactService class instance. You can move your createContact method logic to a function and invoke it from inside the createContact method and from the afterCreate event handler:

async function createUserContact(userData) {
  const accessToken = '' 
  // add all creating logic here
}

class ContactService {
  createContact(userData) {
     return createUserContact(userData)
  }
}

Backendless.ServerCode.Persistence.afterCreate('Users', async (req, res) => {
  const userData = {
    email: res.result.email,
    name: res.result.name
  }

   await createUserContact(userData)
})


This is the updated code still no api call is being made to hubspot.

How do you perform the registration process? Do you use API or REST Console to create a user, or do you create a record directly in the Users table in console?

I’m creating a record directly is Users table

Hello @Kashif_Saleem

use API or REST Console to create a user. Handlers don’t react to console requests like creating a record directly in the table.

Regards