When a subscription is created in HubSpot—often via Stripe, Chargebee or a custom integration—you want the related deal to update itself: correct amount, stage and descriptive name. The custom code action below pulls price and plan details from the Subscription record’s Line Item and syncs them to the associated deal, then links the subscription to that deal for clear reporting.
- Moves deals to the right pipeline stage the moment a subscription is detected.
- Sets deal amount from the subscription’s price so forecasts stay accurate.
- Renames the deal with the plan name and customer for instant context.
- Associates the Subscription object to the deal to keep CRM relationships tidy.
Typical Use Cases
Scenario | Why It Matters |
---|---|
Free-trial to paid conversion | Automatically pushes the deal into the “Free Trial” stage when a trial subscription starts. |
SaaS teams using Stripe and HubSpot | Ensures every new subscription updates the deal amount and name without manual entry. |
Account managers tracking expansion revenue | Captures plan upgrades as new line-item prices and keeps deal values current. |
Step-by-Step Implementation
1. Prerequisites
- Create a Private App token with
crm.objects.contacts.read
,crm.objects.deals.write
andcrm.objects.line_items.read
scopes. Save it as a secret namedMyEngagements
. - Know the internal ID of the pipeline stage you want—here
189440338
represents “Free Trial”.
2. Insert the Custom Code Action
- Build a Contact-based workflow that enrolls when the subscription ID or deal ID becomes known.
- Add a Custom Code action and reference the secret
MyEngagements
. - Paste the script below.
const hubspot = require('@hubspot/api-client');
exports.main = async (event) => {
const hubspotEmails = new hubspot.Client({
accessToken: process.env.MyEngagements
});
// 1. Pull deal, subscription and customer details from the contact
const res1 = await hubspotEmails.crm.contacts.basicApi.getById(
event.object.objectId,
['saas_subscription_deal_id','saas_subscription_id','saas_subscription_name','full_name']
);
const dealId = res1.body.properties.saas_subscription_deal_id;
const subscriptionId = res1.body.properties.saas_subscription_id;
const subscriptionNm = res1.body.properties.saas_subscription_name;
const customerName = res1.body.properties.full_name;
// 2. Get the Line Item linked to this subscription
const res2 = await hubspotEmails.apiRequest({
method:'GET',
path:`/crm/v3/objects/subscription/${subscriptionId}/associations/line_items`
});
const lineItemId = res2.body.results[0].id;
// 3. Fetch price and plan name from the Line Item
const res3 = await hubspotEmails.apiRequest({
method:'GET',
path:`/crm/v3/objects/line_items/${lineItemId}?properties=price,name`
});
const price = res3.body.properties.price;
const plan = res3.body.properties.name;
// 4. Update the deal: stage, amount, and deal name
await hubspotEmails.apiRequest({
method:'PATCH',
path:`/crm/v3/objects/deals/${dealId}`,
body:{
properties:{
dealstage:'189440338',
amount:price,
dealname:`${plan} - ${customerName}`
}
}
});
// 5. Associate the subscription to the deal for reporting
await hubspotEmails.apiRequest({
method:'PUT',
path:`/crm/v4/objects/deals/${dealId}/associations/default/subscriptions/${subscriptionId}`
});
};
3. Test Thoroughly
- Create a test subscription and contact with a dummy plan in your sandbox.
- Ensure the contact record stores the subscription and deal IDs.
- Enroll the contact and confirm:
- The deal moves to “Free Trial” stage.
- Deal amount equals the line-item price.
- Deal name shows
Plan Name - Customer
. - The deal’s “Associated Subscriptions” card lists the new subscription.
Results and Benefits
- Deals always reflect the correct stage and revenue straight from subscription data.
- Account and finance teams no longer reconcile plan names or prices by hand.
- Revenue dashboards stay accurate as soon as a customer starts or upgrades a plan.
Wrapping Up
If your SaaS business relies on HubSpot’s Subscription object, aligning deals with real-time billing data is critical. This workflow bridges the gap by pulling plan and price details from the subscription’s Line Item and updating the deal automatically. Tweak the stage ID, properties or naming convention to match your pipeline, and enjoy smoother hand-offs between sales, finance and success teams.