⚙ Operations Hub

sam-g
Mitwirkender/Mitwirkende

Finding associations and then adding associated records to a workflow

lösung

Hey Guys,

 

I have a fair amount of experience with Python, but not a ton with JavaScript so I'm kind of muddling my way through this for the time being.

 

I'm trying to enroll a custom object record in a workflow, get the associated contacts, and then enroll those associated contacts into a contact based workflow.

 

So far I've been successful in enrolling the record, and finding the associated contacts.

 

Where I'm getting stuck is creating an array of the contact ids so that I can loop through them and add them all to the workflow.

 

Here is my code as of now (I'm not trying to address the workflow enrollment for the contacts yet):

 

 

    // const PROGRAM_NAME = "state";
    const hubspot = require('@hubspot/api-client');

    exports.main = (event, callback) => {
        callback(processEvent(event));
    };

    function processEvent(event) {
        const hubspotClient = new hubspot.Client({ apiKey: process.env.HAPIKEY });

        let engage_id = event['id'];
        // console.log(engage_id);

        hubspotClient.crm.objects.basicApi
            .getById(event.object.objectType, event.object.objectId, ["offer_label", "skupos_loyalty_status"])
            .then(results => {
                let engage_enroll = results.body;
                console.log(engage_enroll)

                hubspotClient.crm.objects.associationsApi
                    .getAll(
                        event.object.objectType,
                        event.object.objectId,
                        ["contacts"]
                    )
                    .then(results => {
                        let associations = results.body.results;
                        let obj_array = JSON.stringify(associations);
                        let contact_ids = obj_array.map(a => a.id);
                        console.log(obj_array);
                    }
                    )
            }
            )
    }

 

 

I get an error with the mapping, which I think is due to the promise (which is a new concept to me) not being completed yet?

 

I'm hoping to produce something like this, an array of contact ids:

 

 

["2361901", "2436301"]

 

 

The obj_array outputs the following:

 

 

[{"id":"2361901","type":"engage_enrollment_to_contact"},{"id":"2436301","type":"engage_enrollment_to_contact"}]

 

 

Which looks correct to me. Any advice on where I'm going wrong here?

1 Akzeptierte Lösung
sam-g
Lösung
Mitwirkender/Mitwirkende

Finding associations and then adding associated records to a workflow

lösung

If anyone needs to do this in the future here is the final code I used and tested:

 

 

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

exports.main = (event, callback) => {
    callback(processEvent(event));
};

function processEvent(event) {
    const hubspotClient = new hubspot.Client({ apiKey: process.env.HAPIKEY });

    let engage_id = event['id'];
    // console.log(engage_id);

    hubspotClient.crm.objects.basicApi
        .getById(event.object.objectType, event.object.objectId, ["offer_label", "skupos_loyalty_status"])
        .then(results => {
            let engage_enroll = results.body;
            console.log(engage_enroll)

            hubspotClient.crm.objects.associationsApi
                .getAll(
                    event.object.objectType,
                    event.object.objectId,
                    ["contacts"]
                )
                .then(results => {
                    let associations = results.body.results;
                    let contact_ids = associations.map(a => a.id);
                    console.log(associations, contact_ids);

                    contact_ids.forEach(function (entry) {
                        hubspotClient.apiRequest({
                            method: 'POST',
                            path: `/automation/v2/workflows/22041022/enrollments/contacts/${entry}?hapikey=${process.env.HAPIKEY}`
                        });
                    });
                }
                )
        }
        )
}

 

Lösung in ursprünglichem Beitrag anzeigen

6 Antworten
SFoltz88
Teilnehmer/-in

Finding associations and then adding associated records to a workflow

lösung

Hi, it's been a long time since this was posted but I'm not sure if this is still relevant today. The way we've been enrolling associated records in workflows now has been directly inside of the custom object's workflow itself by setting a property to a specific value on the associated record. The associated object's workflow just looks for this value as an enrollment trigger.

 

This is only tangentially related to what I came here looking for. HubSpot has very few ways to narrow down which associated record you want. For instance, if I want a specific record right now, I have to make a whole slew of association labels and hope that the other people in the organization use them correctly. If they don't, we can create a real mess.

 

We have other ways that we could narrow down the workflow-based search for a specific record so that we could pull specific information, but HubSpot doesn't allow you to filter a branch based on multiple record properties down to just a single record in any straightforward way. I get the sense that I could do this filtering with the API, but I am pretty unfamiliar with this. This topic seemed to be the closest thing I could find at a relatively cursory glance.

0 Upvotes
sam-g
Lösung
Mitwirkender/Mitwirkende

Finding associations and then adding associated records to a workflow

lösung

If anyone needs to do this in the future here is the final code I used and tested:

 

 

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

exports.main = (event, callback) => {
    callback(processEvent(event));
};

function processEvent(event) {
    const hubspotClient = new hubspot.Client({ apiKey: process.env.HAPIKEY });

    let engage_id = event['id'];
    // console.log(engage_id);

    hubspotClient.crm.objects.basicApi
        .getById(event.object.objectType, event.object.objectId, ["offer_label", "skupos_loyalty_status"])
        .then(results => {
            let engage_enroll = results.body;
            console.log(engage_enroll)

            hubspotClient.crm.objects.associationsApi
                .getAll(
                    event.object.objectType,
                    event.object.objectId,
                    ["contacts"]
                )
                .then(results => {
                    let associations = results.body.results;
                    let contact_ids = associations.map(a => a.id);
                    console.log(associations, contact_ids);

                    contact_ids.forEach(function (entry) {
                        hubspotClient.apiRequest({
                            method: 'POST',
                            path: `/automation/v2/workflows/22041022/enrollments/contacts/${entry}?hapikey=${process.env.HAPIKEY}`
                        });
                    });
                }
                )
        }
        )
}

 

VTorresRamos4
Teilnehmer/-in

Finding associations and then adding associated records to a workflow

lösung

Hello, does this work for me? 

 

My custom objects is: "Group"

 

I already have this associations:

Tickets --- Contacts

Contacts---Group (custom object)

 

I need: 

Tickets ---Group (custom object)

 

What happens:

In my conversation workflow, a ticket is created with the corresponding contact association. This contact has the objects "Company" and "Group" already associated on it.

The problem is that this created ticket is not associated with the custom object "Group".

 

What i'd like to have:

I'm not a developer, but what i need is a code or something that associates this ticket with my custom object "Group" (the "Group" is associated in my contact).

 

Can you help me to solve this problem, please?

0 Upvotes
SCelen
Teilnehmer/-in

Finding associations and then adding associated records to a workflow

lösung

Exactly what I needed. Thank you so much!

0 Upvotes
SaleProcessNerd
Teilnehmer/-in

Finding associations and then adding associated records to a workflow

lösung

My friend, thank you. This is awesome, Ive been wondering how to get an array list from a single objectId and the xxx.map and xxx.forEach looks to be the key here, so thank you for posting this.

10 points to sam-gindor

sam-g
Mitwirkender/Mitwirkende

Finding associations and then adding associated records to a workflow

lösung

Well, me not knowing JavaScript was definitely the culprit here, I just ended up removing the stringify prior to the map function and it works now:

 

 

let associations = results.body.results;
let contact_ids = associations.map(a => a.id);

 

 

Which outputs this for contact_ids:

 

 

[ '2361901', '2436301' ]

 

 

Which I swore I tried prior to posting this, but you know how it goes with that sometimes 😐