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
Solved! Go to Solution.
Views
Replies
Total Likes
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
// 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.");
Thanks
Sushant Trimukhe
Views
Replies
Total Likes
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
// 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.");
Thanks
Sushant Trimukhe
Views
Replies
Total Likes
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!
Views
Replies
Total Likes
Views
Likes
Replies