Hi community,
We recently added a custom object for our referral & commission partners. This way, if a lead comes in via their specific trial links (we then capture their “partner_id” on contact level); we can link it to the custom object & it’s clear for our internal sales team that there is already an association to a parter (so the internal sales doesn’t have to work on the contact)
So: When a contact gets a value for “partner_id” by filling in a trial request; it will be triggered in a workflow. I then want a custom coded action to associate the contact to the right custom Partner object (based on a match of the partner_id property)
I found this custom code to associate a conact to a company --> This custom code snippet can be used to associate a contact to a company based on the company name property that is stored at a contact level. It's particularly useful if your customers are using a freemail address and aren't supplying a company website - just the name of their company. If no company is found a new record will be created in the CRM and an association will be made/ · GitHub from @jackcoldrick but am trying to modify it to my needs.
I was wondering if anyone would be able to help me out. @BenLerner-Paio , could you help me out again by any chance?
So, I have been able to find documentation & did some get requests to get to my object type Id & the type Id of the association of contact to partner object.
My custom object (referral_partner) had the custom object type id “2-18324712” and the type Id for the association should be 59
How do I modify the code from associateContactToCompany
in order for it to match the contents of the contact property “partner_id” to the contents of the custom object property “partner_id” & if there’s a match → associate the contact to the custom object?
I gave it a try by looking at the code & some other community stuff but it doesn’t make sense.
// Import HubSpot Client Library and instantiate client with private app token
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({
accessToken: process.env.HannePartnerTest // Access token associated with your private app (also be sure to include token in secrets)
});
// Helper function to get the associated Object ID of a specified Object Type
async function getAssociatedObjectId(objectType, objectId, toObjectType) {
let obj = await hubspotClient.crm.objects.associationsApi.getAll(objectType, objectId, toObjectType);
return obj.results[0].id;
}
// Helper function to associate a custom object to a contact
async function associateCustomObjectToContact(objectTypeId, customObjectId, contactId) {
const AssociationSpec = [{
"associationCategory": "USER_DEFINED",
"associationTypeId": 59, // Add a comma here
"label": "Referral Contact"
}];
hubspotClient.apiRequest({
method: 'put', // Use lowercase 'put'
path: `/crm/v4/objects/${objectTypeId}/${customObjectId}/associations/contact/${contactId}`,
body: AssociationSpec
});
}
exports.main = async (event) => {
// Define variables
const objectTypeId = '2-18324712'; // Custom Object "objectTypeId" (this will be different for you)
const objectId = event.object.objectId // Currently enrolled Custom Object Record Id
// Storing the partnerID into a const
const partnerID = event.inputFields['partner_id'];
// Create search criteria
const filter = { propertyName: 'partner_id', operator: 'EQ', value: partnerID };
const filterGroup = { filters: [filter] };
const sort = JSON.stringify({ propertyName: 'partner_id', direction: 'DESCENDING' });
const properties = ['partner_id'];
const limit = 1;
const after = 0;
const searchCriteria = {
filterGroups: [filterGroup],
sorts: [sort],
properties,
limit,
after
};
// Search the CRM for custom objects matching "partnerID" variable defined earlier
hubspotClient.crm.objects.searchApi.doSearch(objectTypeId, searchCriteria).then(searchResponse => {
// console.log("RESULTS: " + searchResponse.body.total); // - FOR DEBUG
// Continue with the rest of your code...
});
};
// if MATCH FOUND - ASSOCIATE partner object to CONTACT
if(// console.log("PartnerID " + partnerId + " FOUND: ASSOCIATE RECORDS"); // - FOR DEBUG
//Associate Company with Contact
hubspotClient.crm.objects.associationsApi.create(searchResponse.body.results[0].id,'contacts', event.object.objectId,'associateCustomObjectToContact');
}
});
callback({outputFields: {}});
})
.catch(err => {
console.error(err);
});
})
