Serverless delete rows batch

Hi Everyone, I’m a bit struggle on this point, I want to remove all the row from one table, I already know how to do it one by one but hubspot documentation has a line to remove all the row by providing a list of rows. POST/cms/v3/hubdb/tables/{tableIdOrName}/rows/draft/batch/purge

on Accounts Dashboard | HubSpot

I use the serverless function, so I provide the list of the rows I want to remove like that:

“69668001298”,“69668001299”,“69668001300”

then I set up the requestBody :

requestBody[‘inputs’] = “69668001298”,“69668001299”,“69668001300”;

I also try when send it to the serverless with the JSON.stringify.

then I call the post:

Requires HubDB table with name donations. Columns: name (text), amount (number)

const API_HUBDB_BASE = 'https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxx/rows/draft/batch/purge’;

const DELETE_TABLE_API_URI =

API_HUBDB_BASE + ‘?hapikey=’ + apiKey;

Use axios to make a delete request to the API

axios

.post(DELETE_TABLE_API_URI, requestBody)

.then(function(response) {

No matter the way I provide the format I get Invalid input json

Someone got an idea on how to use this function?

Thanks for the help you can provide.

Kind regards.

Joan

@joan1 ,

Would you be able to put the code together so we can see the entirety of it.

Maybe @Mark_Ryba could help once we see what your code looks like in a more readable way

Hi @dennisedson ,

Yes, I didn’t send all but keep the main function I used on the different part(vue.js and serverless)
Vue js part:

mounted(){

let requestRowToDelete =

https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxxx/rows?hapikey=” +APIKEY ;

;

axios.all([requestRowToDelete, requestRowToUpdate])

.then(axios.spread((…responses) => {

this.dataToDeleteJson= responses[0].data.results;

}

var dataToDeleteString = “”;

  for (const dataof this.dataToDeleteJson){

      dataToDeleteString += '"' + data.id + '",';

  }

dataToDeleteString = dataToDeleteString.slice(0, -1));

this.dataToDeleteFormatted = dataToDeleteString;

},

methods: {

onUpdate: function(){

   const data = {

            "inputs": this.dataToDeleteFormatted

          };

          const requestOptions = {

            'method': 'POST',

            'headers': {

              'Content-Type': "application/json",

            },

            "body": JSON.stringify(data),

          };

      fetch("/\_hcms/api/delete-rows-batch", requestOptions)

          .then(response => response.text())

          .then(result =>{

            var parsedResult = JSON.parse(result);

            console.log(result);

                    }

          })

          .catch(error => console.log('error update', error));

},
serverless.json

{
“runtime”: “nodejs12.x”,
“version”: “1.0”,
“environment”: {},
“secrets”: [“API_KEY”],
“endpoints”: {
“delete-rows-batch”: {
“method”: “POST”,
“file”: “delete-rows-batch.js”
},
And finally the delete-rows-batch.js function:

// Require axios library to make API requests
const axios = require(‘axios’);

// This function is executed when a request is made to the endpoint associated with this file in the serverless.json file
exports.main = ({ body }, sendResponse) => {

const apikey = process.env.API_KEY;
let requestBody = {};
requestBody[“inputs”] = body.inputs;

const API_HUBDB_BASE = ‘https://api.hubapi.com/cms/v3/hubdb/tables/xxxxxxx/rows/draft/batch/purge’;
const DELETE_TABLE_API_URI =
API_HUBDB_BASE + ‘?hapikey=’ + apiKey;

// Use axios to make a delete request to the API
axios
.post(DELETE_TABLE_API_URI, requestBody)
.then(function(response) {
console.log(‘HubDB rows deleted’);
console.log(response.data);
console.log(response.status);// sendResponse is what you will send back to services hitting your serverless function
sendResponse({
body: {
response: ‘Delete Successful’,
success: true
},
statusCode: 200,
});
})
.catch(function(error) {
// Handle error
console.log(‘Failed to delete HubDB rows data’);
console.log(error.response.data);
console.log(error.response.status);

sendResponse({
body: {
error: error.message,
success: false
},
statusCode: 500 });
});
};

My Appologize I didn’t embedded to the code because it seems to be rejected by hubspot control.

Hi @dennisedson ,

So first point, you was right, by spliting the array it can work:
for (const value of values){

table[count] = value.id

}

and send the table on the value “inputs” to the fetch make the job.

For the apiKey, thanks to mention it, i’m doing a migration from this to serverless function .

kind regards

There’s a lot going on here, but I think the issue you’re running into is the string actually being sent to the endpoint. It should be a single object with an ‘inputs’ key and an array of values, like this:

{
 "inputs": [
 "12345, 12346, 12347"
 ]
}

Also, it looks like you’re using an exposed API key in your Vue module, so you may want to reconsider how that’s being used in conjunction with your serverless functions.

I see, I will try this morning to set it up as an array of value but if i’m remember well I tried and it was not successfull too.
I will let you know.