Hi everyone,
We currently use an integration called Subskribe to manage all of our subscriptions (SaaS).
The Subscriptions custom object information is:
objectTypeId: 2-21331959
internal name: subskribe_subscription
fullyQualifiedName: p8086137_subskribe_subscription
I have created a workflow with a custom coded step that counts the number of associated subscriptions with:
1. Less than 365 days remaining and
2. Has the EP Subscription Product (internal name: ep_product) property set to “EP for School”.
The property that contains the remaining subscription duration is “Remaining Subscription Duration” (internal name: remaining_subscription_duration) and it is on the Subscription object.
The property I want to populate is on the native Company object and is called “Count of EP Subs < 1 Yr” (internal name: count_of_subs_max_1yr). I have a step in the workflow after the custom coded action that is populated with the output from the script.
My API secret is called “API_KEY” and all options are selected in the private app area.
When I test my workflow with a customer, it appears to be working but when I manually enroll the customer into the workflow, it’s not updating the Count of EP Subs < 1 Yr property.
The log when I test the script produces the following:
2024-12-02T08:34:32.241Z INFO Received companyId: 5376244591
2024-12-02T08:34:32.293Z INFO Fetching associated subscriptions for company: 5376244591
Here is the script:
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ apiKey: "API_KEY" });
// The main function should be async
exports.main = async (event) => {
const companyId = event.object.objectId; // Correct reference to companyId
console.log('Received companyId:', companyId); // Log to confirm
try {
if (!companyId) {
return {
output: {
message: 'Error: Company ID is not available in the event object.'
}
};
}
console.log(`Fetching associated subscriptions for company: ${companyId}`);
const associatedSubscriptions = await hubspotClient.crm.associations.getAll(
'company',
companyId,
'subskribe_subscription'
);
console.log('Associated Subscriptions:', associatedSubscriptions); // Log the associated subscriptions
let count = 0;
for (const association of associatedSubscriptions) {
const subscriptionId = association.id;
const subscription = await hubspotClient.crm.objects.basicApi.getById(
'2-21331959', // subscription objectTypeId
subscriptionId
);
console.log('Fetched subscription properties:', subscription.properties);
const remainingSubscriptionDuration = subscription.properties.remaining_subscription_duration;
const epProduct = subscription.properties.ep_product;
if (remainingSubscriptionDuration < 365 && epProduct === "EP for School") {
count++;
}
}
console.log(`Count of matching subscriptions: ${count}`);
await hubspotClient.crm.objects.basicApi.update(
'companies',
companyId,
{
properties: {
count_of_subs_max_1yr: count.toString() // Ensure count is a string
}
}
).then(response => {
console.log('Update response:', response); // Log the response for debugging
}).catch(error => {
console.log('Error updating property:', error.message); // Log any error during the update
});
return {
output: {
message: `Successfully updated 'Count of EP Subs < 1 Yr' to ${count} for Company ID: ${companyId}`
}
};
} catch (error) {
return {
output: {
message: `Error occurred: ${error.message}`
}
};
}
};
I’m at a loss as to what is going wrong and hoping someone can assist! Thanks in advance for the help!
