[英]HubspotClient - Update contact by email id is not working
在 NodeJS 中,我使用“@hubspot/api-client”:“^7.1.2”。
使用 accessToken 創建 hubspot 客戶端,如下所示
const hubSpotClient = new hubspot.Client({ accessToken });
當我嘗試使用 email 更新聯系人時,它會拋出錯誤
要求:
const idProperty = 'email';
const response = await hubSpotClient(store).crm.contacts.basicApi.update(email, idProperty, contact);
回復:
ERROR {
"statusCode": 404,
"body": {
"status": "error",
"message": "Object not found. objectId are usually numeric.",
"correlationId": "71e911d3...",
"context": {
"id": [
"testemail@..."
]
},
"category": "OBJECT_NOT_FOUND"
}
此客戶端的創建聯系人工作正常,但通過 email 進行更新不起作用。 傳遞 idProperty 時有什么不合適的地方或語法錯誤嗎?
問題出在您的實現中,因為您似乎沒有正確使用 Hubspot API。
如果檢查 basicApi.update 的 function basicApi.update
public async update(contactId: string, simplePublicObjectInput: SimplePublicObjectInput, idProperty?: string, _options?: Configuration): Promise<RequestContext> {
基本上,您需要傳遞一個contactId,然后傳遞一個simplePublicObjectInput
, 它基本上是一個代表您的更新的object。
您的代碼應如下所示:
import { Client } from "@hubspot/api-client";
const hubspotClient = new Client({ accessToken: YOUR_ACCESS_TOKEN });
const contactID = 1234;
const response = await hubspotClient.crm.contacts.basicApi.update(contactID, {
properties: { email: 'my-new-email@gmail.com' },
})
請記住,Hubspot 始終嘗試遵循與端點相同的准則。 如果您檢查端點規范,您將看到以下內容:
可以將 Hubspot 節點客戶端視為一些 http 客戶端的抽象,但最終與實現中描述的端點完全相同。
出於這個原因,在您的實現中,Hubspot 會返回一個適當的錯誤,因為您沒有在第一個參數中提供 contactId,Hubspot 會告訴您:“找不到對象。objectId 通常是數字。” 因為確實聯系人 ID 是數字,而您使用的是 email --string-- 的值。
如果您想“通過電子郵件更新”,我認為沒有直接的方法可以做到這一點,您可能需要通過 email 對聯系人進行先前的搜索。 您可以使用searchApi
。
在獲得 id 后,只需運行更新。
const searchResponse = await hubspotClient.crm.contacts.searchApi.doSearch({
filterGroups: [
{
filters: [
{
value: 'email-to-search@gmail.com',
propertyName: 'email',
operator: 'EQ',
},
],
},
],
sorts: [],
properties: [],
limit: 1,
after: 0,
});
// Off course you need to improve a lot the error handling here and so on.
// This is just an example
const [contactID] = searchResponse.results;
const contactUpdateResponse = await hubspotClient.crm.contacts.basicApi.update(contactID, {
properties: { email: 'my-new-email@gmail.com' },
})
我希望這可以幫助你!
您可以使用 email 作為 hubspot/api-client 的 idProperty 獲取聯系人 function,但它僅在您填寫 idProperty 之前的所有其他查詢字段時才有效,即使它們未定義。
這是我在 Node 中使用 api-client 將 getContactByEmail 作為 Google Cloud Function 的示例,效果很好!
exports.getContactByEmail = functions.https.onCall(async (data, context) => { const email = data.email; const contactId = email; const properties = ["firstname", "lastname", "company"]; const propertiesWithHistory = undefined; const associations = undefined; const archived = false; const idProperty = "email"; try { const apiResponse = await hubspotClient.crm.contacts.basicApi.getById( contactId, properties, propertiesWithHistory, associations, archived, idProperty ); console.log(JSON.stringify(apiResponse.body, null, 2)); return apiResponse.properties; } catch (error) { error.message === "HTTP request failed"? console.error(JSON.stringify(error.response, null, 2)): console.error(error); return error; } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.