Developing CodeFusion Script

CodeFusion scripts can be used as APIs to dynamically interact with Active Content. These scripts accept input parameters, process data, and return structured responses, making them powerful tools for automation, integration, and customization.

A CodeFusion script consists of a function, which is created using the object’s name. This function name is also used when publishing the API.

For more information on developing the script in the CodeFusion IDE of Active Content, see Creating CodeFusion Script.

CodeFusion provides the following built-in objects for scripting:

Object

Description

Console

For logging the output to console. Useful in debugging while developing the script.

API

Call external REST APIs using GET and POST methods.

DME

Query and retrieve data from Data Model Extensions (DME).

Using Console Logging

The console object in CodeFusion lets you write messages to the execution log while developing and testing scripts. Console logging is a valuable tool for troubleshooting, tracking data flow, and validating output at key points in your script logic.

Syntax:

Copy
console.log(message);
console.info(message);

console.log

  • Use console.log() to print messages or variable values when testing CodeFusion scripts.

  • Messages from console.log() appear in the CodeFusion test console.

  • Ideal for general debugging and quick checks during development.

Eample:

Copy
console.log("Request URL: " + url);
console.log("API response object:", apiResponse);

console.info

  • Use console.info() to log information that is captured in the Active Content execution logs, with a content length limit.

  • Particularly useful for debugging nested queries—such as when passing values from one API response to another query—or when you want to print only a subset of a large response for easier analysis.

Eample:

Copy
console.info("Fetched userId: " + userId);
console.info("Top 2 recommendations: " + JSON.stringify(recommendations.slice(0, 2)));

Best Practices:

  • Use console.log() for general test-time debugging.

  • Use console.info() for concise, targeted logging that may be referenced in production execution logs.

  • Limit logging to relevant data points for readability and performance.

Note: All messages logged with console.log() and console.info() can be viewed in the CodeFusion test console and, where applicable, in the Active Content execution logs. Use these logs to verify script behavior and troubleshoot issues effectively.

Using the API Object

The API object is used to call external REST APIs using the GET and POST methods.

Typical use cases include pulling product data, promotions, inventory, or personalization from external services.

GET API

POST API

Using the DME Object

The DME object allows querying and retrieving DME data within a CodeFusion script.

Use the DME object to interact with internal data sources, such as customer attributes, recommendations, or transactional records stored in Algonomy’s DME.

DME API