# 📹 How to Use Custom Code \<Generate Transaction Records Monthly\>

**URL:** <https://community.hubspot.com/t/topic/40700>\
**Category:** 💬 RevOps Discussions\
**Tags:** javascript, professional, enterprise\
**Created:** [July 14, 2021, 2:19am UTC](https://community.hubspot.com/t/topic/40700 "2021-07-14T02:19:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![natsumimori](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/natsumimori/32/39807_2.png) [@natsumimori](https://community.hubspot.com/u/natsumimori)\
**Post date:** [July 14, 2021, 2:19am UTC](https://community.hubspot.com/t/topic/40700/1 "2021-07-14T02:19:48Z")

</div>

Operations Hubの新機能であるカスタムコードを使用して、取引レコードを毎月生成する方法をご紹介します。サブスクリプションビジネスをされている方には特に役立つ情報だと思いますので、ぜひこちらのビデオでチェックしてみてください。([カスタムコードとは？](https://knowledge.hubspot.com/jp/workflows/use-custom-code-actions-in-workflows))

https://www.youtube.com/embed/Uz0cV8Pu2X0?feature=oembed&wmode=opaque

＜毎月取引レコードを生成するカスタムコード＞

＊あくまでサンプルコードですので、動作に関してHubSpotは責任を負いません。あらかじめご了承ください。

＊[GitHubリンクはこちら](https://gist.github.com/dshukertjr/eaf6edf8d27d7939e2a518edd8ca9881#file-subscription_deal-js)

＊[都道府県名を書式統一するカスタムコード例はこちら](https://community.hubspot.com/t5/RevOps-%E3%82%AA%E3%83%9A%E3%83%AC%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3/%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0%E3%82%B3%E3%83%BC%E3%83%89%E6%B4%BB%E7%94%A8%E6%96%B9%E6%B3%95-%E9%83%BD%E9%81%93%E5%BA%9C%E7%9C%8C%E5%90%8D%E3%82%92%E6%9B%B8%E5%BC%8F%E7%B5%B1%E4%B8%80%E3%81%99%E3%82%8B%E3%82%B3%E3%83%BC%E3%83%89%E4%BE%8B/m-p/439322)

`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: {} })}`
