⚙ Operations Hub

kaious
Top Contributor

Associate a note with a custom object in a workflow custom code action?

Hi, I'm using the below code in a custom action which I have adapted from a HubSpot supplied example to attach a note to a contact.

 

I want to create a note on a custom object instead of a contact, so have updated the following:
- end point on line 46 changed /contacts/ to the custom object type ID (

2-10586829)
- type in inputs (line 56) changed to 
note_to_2-10586829
- updated input to send the ID of a custom object, not contact ID.

 

When I ran this before altering, it ran as expected for the contact.

After updating, it returns no API errors, returns a valid note ID, but fails to attach that note to the custom object in HubSpot (I've updated the input to send the object ID of a custom object of the correct type).

So I'm thinking there must be something wrong with the way I'm addressing the custom object in the association call, however, can't figure out exactly what ...any ideas?

 

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

    /**
     * @name getPortalInfo
     * @desc Grab the portal id and various other infos
     * @returns {promise}it returns an axios object
     */
    const getPortalInfo = async () => {
        const endpoint = `https://api.hubapi.com/integrations/v1/me`;

        return axios.get(endpoint, axiosConfig);
    }
    
    const createNote = (properties) => {

        const endpoint = `https://api.hubapi.com/crm/v3/objects/notes`;

        return axios.post(endpoint, { "properties": properties }, axiosConfig);
    }


    if (!event.inputFields.hubspot_owner_id) throw new Error(' event.inputFields.hubspot_owner_id is not set, are you sure you added the hubspot_owner_id in the "property to include in code" option ? ');

    if (!event.inputFields.latestNote) throw new Error(' event.inputFields.latestNote is not set, are you sure you added the latestNote in the "property to include in code" option ? ');

    if (event.inputFields.latestNote === "") throw new Error(" event.inputFields.latestNote is empty, we can't create empty notes ");

    const noteCreated = await createNote({
        "hs_timestamp": new Date(),
        "hs_note_body": event.inputFields.notes_next_action,
        "hubspot_owner_id": event.inputFields.hubspot_owner_id
    });

    const { id } = noteCreated.data;

    const associateNoteWithContact = async (noteId, toObjectID) => {

        const endpoint = "https://api.hubapi.com/crm/v3/associations/notes/2-10586829/batch/create";

        return await axios.post(endpoint, {
            "inputs": [{
                "from": {
                    "id": noteId
                },
                "to": {
                    "id": toObjectID
                },
                "type": "note_to_2-10586829"
            }]
        }, axiosConfig);
    }


    if (!event.inputFields.hs_object_id) throw new Error(' event.inputFields.contactId is not set, are you sure you added the contactId in the "property to include in code" option ? ');

    const res = await associateNoteWithContact(id, event.inputFields.hs_object_id);

    const { status } = res.data;

    console.log(id);

    callback({
        outputFields: {
            id
        }
    });

 

0 Upvotes
1 Reply 1
kvlschaefer
Community Manager
Community Manager

Associate a note with a custom object in a workflow custom code action?

Hi @kaious,

 

Thank you for reaching out!

 

I want to tag some of our experts -  @Teun@Anton@LMeert do you know what's happening here? 

Do you have any advice for @kaious?

 

Thank you!

Kristen


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