⚙ Operations Hub

TSweet2
Participant

Bulk Deleting Associations

Hello, 

I have recently gotten a notifciation that there is a contact in my account that is reaching the association limit for emails. I am trying to use a custom code action in workflows to possibly un-associate  emails from the contact. I currently have some code, but I am getting errors and am also at a loss of where to go next. I was wondering if it would be possible to get some input about where to go from here?

Thanks [Code below]

 

const hubspot = require('@hubspot/api-client');
const axios = require('axios');

exports.main = async (event, callback) => {


const hubspotClient = new hubspot.Client({
accessToken: process.env.PrivateAppKey
});

const objectType = 'contacts';
const objectId = event.inputFields['hs_object_id'];
const toObjectType = 'emails';
const after = undefined;
const limit = 500;

try {
const apiResponse = await hubspotClient.crm.associations.v4.basicApi.getPage(objectType, objectId, toObjectType, after, limit);
console.log(apiResponse.data);

} catch (err) {
console.error(err);
throw err;
}

callback({
outputFields: {
}
});
};

0 Upvotes
3 Replies 3
DianaGomez
Community Manager
Community Manager

Bulk Deleting Associations

Hi @TSweet2

 

Thanks for reaching out to the Community!

 

I understand that you have received the message reaching the limit on CONTACT_TO_EMAIL associations. If this is the case, I found similar cases and they have been able to easily complete this process by deleting these engagements using our public APIs. You can use the CRM Search API to find the emails you want to delete, and then use the Engagements API to delete them. 

 

Let's consult a few experts to get some insights on your code as well. Hello @albertsg, @ankitparmar09, @BarryGrennan - Would you kindly share any recommendations for @TSweet2?

 

Thanks!

Diana
 

 


¿Sabías que la Comunidad está disponible en Español?
¡Participa hoy en conversaciones en el idioma de tu preferencia,cambiando el idioma en tus configuraciones!

Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !


0 Upvotes
TSweet2
Participant

Bulk Deleting Associations

Hello @DianaGomez,

I have figured out a basic solution to this, but there is an issue for looping through the results. It seems that it is only allowing 100 actions at a time, and I was wondering if the looping would be an easier issue to fix instead. The updated code is below, it includes a filter for only disassociating a certain email from the contact record as well. Let me know if there are any questions. 

const hubspot = require('@hubspot/api-client');
const axios = require('axios');

exports.main = async (event, callback) => {


const hubspotClient = new hubspot.Client({
accessToken: process.env.PrivateAppKey
});

const exclusions = ['remove@email.com'];
const objectType = 'contacts';
const objectId = event.inputFields['hs_object_id'];
const toObjectType = 'emails';
const after = undefined;
const limit = 500;


try {
const allEmails = await hubspotClient.crm.associations.v4.basicApi.getPage(objectType, objectId, toObjectType, after, limit);
const allEmailsArray = allEmails.results.map(e => {return {"id": e.toObjectId.toString()}});
//console.log(allEmailsArray);

const emailRead = {inputs: allEmailsArray, properties: ["hs_email_from_email"] };
const archived = false;

try {
const apiResponse = await hubspotClient.crm.objects.emails.batchApi.read(emailRead, archived);
// console.log(JSON.stringify(apiResponse, null, 2));

const filteredEmails = apiResponse.results.filter(e => exclusions.includes(e.properties.hs_email_from_email));
// console.log(filteredEmails);

filteredEmails.forEach(async (e) => {
console.log(e.id);

try {
const deleteResponse = await hubspotClient.crm.associations.v4.basicApi.archive(objectType, objectId, toObjectType, e.id);
}
catch (err) {
console.error(err);
throw err;
}
});


} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}

} catch (err) {
console.error(err);
throw err;
}

callback({
outputFields: {
}
});
};

0 Upvotes
BSee
Member

Bulk Deleting Associations

Hi!

You can do this quite easily with Lutra. I've created a demo video to show you how that is done! (Disclaimer: I am one of the founders here at Lutra)This workflow that is created will actually run through all the email objects that you have associated with the contact and handle all the pagination for you.

If this is interesting to you, I love to chat to understand some of the problems that you have there and give you early access if that makes sense 🙂

Cheers!

Bo Zhi See

 

0 Upvotes