# Custom code
# Overview
Custom code lets you run your own business logic while a customer performs certain actions in the Portal. Chargebee invokes the function you upload at the relevant point in the flow, passes it the current subscription and catalog data, and uses the value your function returns to control what the customer sees and how the change is applied.
Custom code is currently supported in the Portal's Edit Subscription flow through two functions:
fetchAllowedPlanConfig— control which plans and addons a customer can select, auto-attach addons to a plan, and set quantity constraints.fetchChangeSubscriptionOptions— control whether a subscription change takes effect immediately or at the end of the term.
# Prerequisites
- Contact support (opens new window) to enable custom code for your site.
- To apply quantity constraints through
fetchAllowedPlanConfig, enable the Customize plan/addon quantity based on meta configuration setting in your Checkout and Portal settings. - After the feature is enabled, manage your custom code at
https://YOUR_SUBDOMAIN.chargebee.com/custom_codes, where you upload, run, and publish it. For the end-to-end workflow, see Execute your custom code (opens new window).
# How it works
- Write your function in JavaScript so that it runs in a Node.js v6.0 environment.
- Chargebee invokes the function and passes it the resources for the current subscription. These resources use the same format as the output of the Node.js client library (opens new window). To retrieve additional resources, call the client library from within your function.
- Your function returns a JavaScript object through the
callback. Chargebee validates the object against the expected schema and applies it to the Portal flow.
Multi-decimal quantities
If multi-decimal support (opens new window) is enabled for your site, quantities are provided in the *_in_decimal fields as strings, and the corresponding integer fields (such as plan_quantity) are null. Read plan_quantity_in_decimal and addon quantity_in_decimal instead of plan_quantity and quantity, and wrap the values in Number(...) before you compare them. For details, see fetchChangeSubscriptionOptions.
# Supported functions
| Function | Use it to |
|---|---|
fetchAllowedPlanConfig | Restrict the plans and addons a customer can select, auto-attach addons when a plan is selected, and set quantity constraints for a plan or addon. |
fetchChangeSubscriptionOptions | Decide whether each subscription change applies immediately or at the end of the term. For example, apply upgrades immediately and defer downgrades to the end of the term. |