7 14, 2021 11:19 AM - 編集済み 7 14, 2021 2:50 PM
Operations Hubの新機能であるカスタムコードを使用して、取引レコードを毎月生成する方法をご紹介します。サブスクリプションビジネスをされている方には特に役立つ情報だと思いますので、ぜひこちらのビデオでチェックしてみてください。(カスタムコードとは?)
<毎月取引レコードを生成するカスタムコード>
*あくまでサンプルコードですので、動作に関してHubSpotは責任を負いません。あらかじめご了承ください。
const hubspot = require('@hubspot/api-client')
/**
* HubSpot will display an error when you paste this code, but it actually
* works if you ignore it and just save it.
* Workflow action to create a new deal on the same close date every month
* The date has to be 1st - 28th.
* If the date is 29th - 31st, the dates will be off at some point.
*
* Also, watch your API limit. This custom action calls the API 3 times everytime it runs.
*/
exports.main = async(event, callback) = > {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
const dealId = event.object.objectId
const results = await hubspotClient.crm.deals.basicApi.getById(dealId, ['closedate', 'amount', 'dealname', 'dealstage', ])
const deal = results.body.properties
const closeDate = new Date(deal.closedate)
closeDate.setMonth(closeDate.getMonth() + 1)
closeDate.setMilliseconds(0)
closeDate.setSeconds(0)
closeDate.setMinutes(0)
closeDate.setHours(0)
const newDealData = {
properties: {
closedate: closeDate.getTime(),
amount: deal.amount,
dealname: deal.dealname,
dealstage: deal.dealstage,
}
}
const res = await hubspotClient.crm.deals.basicApi.create(newDealData)
// Associate the new deal with the company that was originally associated with
const newDealId = res.body.id
const associations = await hubspotClient.crm.deals.associationsApi.getAll(dealId, 'companies')
if (associations.body.results.length > 0) {
await hubspotClient.crm.deals.associationsApi.create(newDealId, 'companies', associations.body.results[0].id, 'deal_to_company')
}
callback({
outputFields: {}
})
}
この投稿にはまだ回答が寄せられていません。しばらく経ってからもう一度ご確認ください。また、役に立つ情報をご存じの場合はぜひ回答を投稿してください。
投稿に回答する回答の仕方についてご不明な場合は、コミュニティーのガイドラインをご覧ください。