Expand my Community achievements bar.

SOLVED

Mass Updating URLs in Delivery Templates

Avatar

Level 1

Hi Team,

I need to fetch Delivery Templates and check the URLs used or incorporated within their content. My goal is to replace only the prefix part of the URL while keeping the rest of the URL unchanged. Additionally, I need to perform a mass update across multiple Delivery Templates.

Could you please guide me on the best approach to achieve this?

Thanks,
Ankita Vishe

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @AnkitaVi1,

Please try the below and let me know if this works do all the test inside a DEV instance first before running anything inside Prod.

Step 1: Create a Workflow to Fetch Delivery Templates

  • Create a New Workflow
  • Add a Query Activity:
    • Target the nms:delivery schema.
    • Filter for delivery templates by adding a condition like [@isModel] = 1 (this identifies templates).
    • Optionally, refine the query to target specific templates (e.g., by folder, label, or internal name) using conditions like [@folder-id] = <folder-id> or [@label] like '%some text%'.
    • Select relevant fields in the output: [@id], [@label], [content/html/source] (for HTML content), and [content/text] (for text content).

Step 2: Extract URLs Using JavaScript

  • Add a JavaScript Code Activity:
    • After the Query activity, add a JavaScript code activity to process the results.
    • Use the following script to extract URLs, identify the prefix, and prepare the replacement:
// Assuming the Query activity outputs to a variable called 'deliveryTemplates'
var deliveryTemplates = vars.target; // Adjust based on your Query output variable

// Define the old and new URL prefixes
var oldPrefix = "http://old.domain.com"; // Replace with your current prefix
var newPrefix = "https://new.domain.com"; // Replace with your desired prefix

// Regular expression to match URLs (basic example, refine as needed)
var urlRegex = /(https?:\/\/[^\/\s]+\/[^"\s]*)/g;

// Loop through each delivery template
for each (var delivery in deliveryTemplates) {
  var deliveryId = delivery.@id;
  var htmlContent = delivery.content.html.source.toString(); // HTML content
  var textContent = delivery.content.text.toString(); // Text content

  // Process HTML content
  if (htmlContent) {
    var updatedHtml = htmlContent.replace(urlRegex, function(match) {
      if (match.startsWith(oldPrefix)) {
        return newPrefix + match.substring(oldPrefix.length);
      }
      return match; // Leave unchanged if prefix doesn't match
    });

    // Update the delivery content if changes were made
    if (updatedHtml !== htmlContent) {
      delivery.content.html.source = updatedHtml;
      logInfo("Updated HTML content for delivery template ID: " + deliveryId);
    }
  }

  // Process Text content (if applicable)
  if (textContent) {
    var updatedText = textContent.replace(urlRegex, function(match) {
      if (match.startsWith(oldPrefix)) {
        return newPrefix + match.substring(oldPrefix.length);
      }
      return match;
    });

    if (updatedText !== textContent) {
      delivery.content.text = updatedText;
      logInfo("Updated Text content for delivery template ID: " + deliveryId);
    }
  }

  // Save the updated delivery template
  if (updatedHtml !== htmlContent || updatedText !== textContent) {
    xtk.session.Write(delivery);
  }
}

logInfo("URL prefix replacement completed.");​
    • Adjust oldPrefix and newPrefix to match your specific use case (e.g., http://example.com to https://secure.example.com).
    • The regex urlRegex is basic and may need refinement depending on your URL patterns (e.g., to handle query parameters, anchors, or edge cases).
    • This script assumes URLs are in the HTML (content/html/source) or text (content/text) fields. If URLs are in other fields (e.g., personalization blocks or scripts), you’ll need to extend the logic.

Step 3: Test the Workflow

  • Run on a Small Scale First:
    • Limit the Query to a few templates (e.g., add a condition like [@id] = 12345) to test the script.
    • Check the logs (logInfo) and verify the updated templates in the UI or database.
  • Validate Changes:
    • Open a few modified templates in the Campaign UI to ensure the URLs updated correctly and the rest of the content remains intact.

Step 4: Apply the changes across all targeted templates

Thanks

Sushant Trimukhe

View solution in original post

2 Replies

Avatar

Correct answer by
Level 4

Hi @AnkitaVi1,

Please try the below and let me know if this works do all the test inside a DEV instance first before running anything inside Prod.

Step 1: Create a Workflow to Fetch Delivery Templates

  • Create a New Workflow
  • Add a Query Activity:
    • Target the nms:delivery schema.
    • Filter for delivery templates by adding a condition like [@isModel] = 1 (this identifies templates).
    • Optionally, refine the query to target specific templates (e.g., by folder, label, or internal name) using conditions like [@folder-id] = <folder-id> or [@label] like '%some text%'.
    • Select relevant fields in the output: [@id], [@label], [content/html/source] (for HTML content), and [content/text] (for text content).

Step 2: Extract URLs Using JavaScript

  • Add a JavaScript Code Activity:
    • After the Query activity, add a JavaScript code activity to process the results.
    • Use the following script to extract URLs, identify the prefix, and prepare the replacement:
// Assuming the Query activity outputs to a variable called 'deliveryTemplates'
var deliveryTemplates = vars.target; // Adjust based on your Query output variable

// Define the old and new URL prefixes
var oldPrefix = "http://old.domain.com"; // Replace with your current prefix
var newPrefix = "https://new.domain.com"; // Replace with your desired prefix

// Regular expression to match URLs (basic example, refine as needed)
var urlRegex = /(https?:\/\/[^\/\s]+\/[^"\s]*)/g;

// Loop through each delivery template
for each (var delivery in deliveryTemplates) {
  var deliveryId = delivery.@id;
  var htmlContent = delivery.content.html.source.toString(); // HTML content
  var textContent = delivery.content.text.toString(); // Text content

  // Process HTML content
  if (htmlContent) {
    var updatedHtml = htmlContent.replace(urlRegex, function(match) {
      if (match.startsWith(oldPrefix)) {
        return newPrefix + match.substring(oldPrefix.length);
      }
      return match; // Leave unchanged if prefix doesn't match
    });

    // Update the delivery content if changes were made
    if (updatedHtml !== htmlContent) {
      delivery.content.html.source = updatedHtml;
      logInfo("Updated HTML content for delivery template ID: " + deliveryId);
    }
  }

  // Process Text content (if applicable)
  if (textContent) {
    var updatedText = textContent.replace(urlRegex, function(match) {
      if (match.startsWith(oldPrefix)) {
        return newPrefix + match.substring(oldPrefix.length);
      }
      return match;
    });

    if (updatedText !== textContent) {
      delivery.content.text = updatedText;
      logInfo("Updated Text content for delivery template ID: " + deliveryId);
    }
  }

  // Save the updated delivery template
  if (updatedHtml !== htmlContent || updatedText !== textContent) {
    xtk.session.Write(delivery);
  }
}

logInfo("URL prefix replacement completed.");​
    • Adjust oldPrefix and newPrefix to match your specific use case (e.g., http://example.com to https://secure.example.com).
    • The regex urlRegex is basic and may need refinement depending on your URL patterns (e.g., to handle query parameters, anchors, or edge cases).
    • This script assumes URLs are in the HTML (content/html/source) or text (content/text) fields. If URLs are in other fields (e.g., personalization blocks or scripts), you’ll need to extend the logic.

Step 3: Test the Workflow

  • Run on a Small Scale First:
    • Limit the Query to a few templates (e.g., add a condition like [@id] = 12345) to test the script.
    • Check the logs (logInfo) and verify the updated templates in the UI or database.
  • Validate Changes:
    • Open a few modified templates in the Campaign UI to ensure the URLs updated correctly and the rest of the content remains intact.

Step 4: Apply the changes across all targeted templates

Thanks

Sushant Trimukhe

Avatar

Administrator

Hi @AnkitaVi1,

Was the given solution helpful to resolve your query or do you still need more help here? Do let us know. In case the given solution was helpful, then kindly choose it as the 'Correct Reply'.

Thanks!



Sukrity Wadhwa