Can't Update custom contact property

JYam
Member

Hi, I've been trying to update a contact's custom property through the use of the method here: https://developers.hubspot.com/docs/api/crm/contacts 

 

But I keep getting an error like so:

ApiException [Error]: HTTP-Code: 0
Message: An error occurred.
Body: {"status":"error","message":"Object not found.  objectId are usually numeric.","correlationId":"c3d704f9-8c59-4c90-ac79-da9e1ac0065a","context":{"id":["[object Promise]"]},"category":"OBJECT_NOT_FOUND"}

My code looks like this:

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

const hubspotClient = new hubspot.Client({"apiKey":"Myapikey"});

// Extract the latest contact ID
const contactId = async () => {
    try{
        const contact = await request({
            method:'GET',
            url: "https://api.hubspot.com/contacts/v1/lists/all/contacts/recent",
            qs:
                {
                    hapikey: "Myapikey",
                    count: "1",
                    formSubmissionMode: "newest"
                },
            json:true
        });
        const client = contact.contacts[0].vid.toString(); // returns the contact ID
        return client;
    } catch(e){
        console.log("error: ",e);
    }
};



const properties = {
    "ruid": "44"
};

const SimplePublicObjectInput = { properties };

// Update contact property "ruid"
async function update(){
    try{
        const apiResponse = await hubspotClient.crm.contacts.basicApi.update(contactId(), SimplePublicObjectInput); // Replacing contactId() with the actual contact Id  works
        console.log(JSON.stringify(apiResponse.body, null, 2));
        } catch (e) {
        e.message === 'HTTP request failed'
        ? console.error(JSON.stringify(e.response, null, 2))
        : console.error(e)
        }
};

The whole purpose of the code is to update a custom property of every new client. If I manually replace contactId() with the actual contact id of the newest client, update() would work. 

 

I suspect the issue lies in how the API handles promises. I'm not so sure, would appreciate it if someone could help! 

0 Upvotes
1 Accepted solution
JBeatty
Solution
Guide | Diamond Partner
Guide | Diamond Partner

Hi @JYam,

 

By calling an async function a promise is inherently returned. So you are passing a promise to the update method, which expectes a string. I believe this could be fixed by changing the main line of code to:

await hubspotClient.crm.contacts.basicApi.update(await contactId(), SimplePublicObjectInput);

If this does not work I would recommend adding a console.log of the contactId to see what is actually being passed to update function.

 

Best,

✔️ Was I able to help answer your question? Help the community by marking it as a solution.

Joshua Beatty
Software Developer with Pearagon

Still have questions? Let's Talk

View solution in original post

2 Replies 2
JBeatty
Solution
Guide | Diamond Partner
Guide | Diamond Partner

Hi @JYam,

 

By calling an async function a promise is inherently returned. So you are passing a promise to the update method, which expectes a string. I believe this could be fixed by changing the main line of code to:

await hubspotClient.crm.contacts.basicApi.update(await contactId(), SimplePublicObjectInput);

If this does not work I would recommend adding a console.log of the contactId to see what is actually being passed to update function.

 

Best,

✔️ Was I able to help answer your question? Help the community by marking it as a solution.

Joshua Beatty
Software Developer with Pearagon

Still have questions? Let's Talk

Jaycee_Lewis
Thought Leader

Hi @JYam 👋


Great question 😊  Thanks for including all those details.

 

I am going to loop in some of our community experts. Hey, @JBeatty and @dannio  do you have any insight here?

 

Thanks for taking a look!

– Jaycee





loop


Loop Marketing is a new four-stage approach that combines AI efficiency and human authenticity to drive growth.

Learn More




0 Upvotes