Modifying CodeFusion Script

You can update any draft or published CodeFusion script to accommodate new business requirements or correct logic.

Modifying scripts allows you to refine your data handling, update API integrations, or implement new personalization logic without starting from scratch. For published scripts, CodeFusion enforces a draft-and-publish model, ensuring changes are safe and reviewable before going live.

To modify the CodeFusion script:

  1. On the CodeFusion page, locate the script you want to edit.

  2. Hover over the script file and click to make the required changes. The Edit script window opens.

    o If the script is published, you’ll see a warning message:

    You are trying to modify the script <script_name>.js that has already been published. A draft version will be created for you to modify and publish again. Are you sure you want to continue?

  3. Click Confirm to proceed.

  4. Make your required changes in the script editor.

  5. Click Test code to validate your changes in the interactive console.

  6. If your tests pass, click Save.

  7. When ready to update production, click Publish. The revised script is now active in your Active Content flows.

Example: Modifying the Logic of an Existing Script

Original Script:

The following script fetches a user profile by user ID from an external API.

Copy
function getUserProfile_main(userId) {
  var output = {};
  var apiUrl = "https://api.example.com/users/" + encodeURIComponent(userId);

  try {
    var response = api.get(apiUrl)
      .putHeader("Accept", "application/json")
      .withReqTimeOut(2)
      .enableCache(30)
      .invokeForJson();

    output = response.json || {};
  } catch (error) {
    console.log("[getUserProfile_main] API call failed:", error);
    console.log("[getUserProfile_main] API call failed:"+ error);
    output = {
      userId: userId,
      name: "Unknown User",
      status: "unavailable"
    };
  }
  return output;
}

Modified Script:

Suppose you want to enhance the script to log the user's name if it exists, and add a lastFetched timestamp in the output.

Copy
function getUserProfile_main(userId) {
  var output = {};
  var apiUrl = "https://api.example.com/users/" + encodeURIComponent(userId);

  try {
    var response = api.get(apiUrl)
      .putHeader("Accept", "application/json")
      .withReqTimeOut(2)
      .enableCache(30)
      .invokeForJson();

    output = response.json || {};

    // Enhancement: Log the user's name if present
    if (output.name) {
      console.log("[getUserProfile_main] User name found:"+ output.name);
    }

    // Enhancement: Add a lastFetched timestamp
    output.lastFetched = new Date().toISOString();

  } catch (error) {
    console.log("[getUserProfile_main] API call failed:"+ error);
    output = {
      userId: userId,
      name: "Unknown User",
      status: "unavailable",
      lastFetched: new Date().toISOString()
    };
  }
  return output;
}

Key Points

  • Always keep the function name unchanged to maintain integration compatibility.

  • You can enhance logic, update error handling, or add new output fields as needed.

  • Use the test console to validate modifications before publishing.

  • All changes should be documented clearly in script comments for future maintainers.