Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards

Insert HTML code in templates with JavaScript

Avatar

Level 4

Hi,

I would like to update several delivery templates in Adobe Campaign that are currently missing some required HTML code in the email body. Specifically, I want to insert the following code into the body section of each template:

<p><%@ include view='logo'%></p> <p><a href="<%@ include view='MirrorPageUrl' %>">Web version</a></p> <p>Dummy</p>


Is it technically possible to use JavaScript to programmatically populate the body (HTML content) of some delivery templates with this code? If so, could you provide guidance or best practices on how to achieve this? I can populate fields with mass update or JS but I want to write that code in the body of a delivery template without doing it manually (opening XML-code and pasting it).

I want to write the code in the body as below:


<source><![CDATA[<!DOCTYPE html>
<html>
<head>
</head>
<body>

<p><%@ include view='logo'%></p> <p><a href="<%@ include view='MirrorPageUrl' %>">Web version</a></p> <p>Dummy</p>

</body>
</html>]]></source>
</html>

 

Thank you in advance for your help!

1 Reply

Avatar

Level 3

Hi @A_B_SE,

 

Yes, you can absolutely update multiple delivery templates programmatically with JavaScript like in your code above. This script loops through an array of template internal names, fetches each template, and replaces its HTML content with the new HTML you provide.

Just update the 'templateInternalNames' array with your templates and set 'newHtmlSource' to your desired HTML (including your include Tags).

 

// Define the schema for delivery templates.
var templateSchema = "nms:delivery";

// Define an array of internal names of the templates you want to update.
var templateInternalNames = ["DM122", "DM123", "DM124"]; // Add more as needed

// Define the new HTML source you want to update in each template.
var newHtmlSource = "<html><body><h1>Updated Email Content</h1></body></html>";

// Loop through each internal name
for each (var templateInternalName in templateInternalNames) {
    // --- Query logic ---
    var query = xtk.queryDef.create(
        <queryDef schema={templateSchema} operation="getIfExists">
            <select>
                <node expr="@id"/>
                <node expr="@label"/>
                <node expr="[content/html/source]" alias="@cont"/>
            </select>
            <where>
                <condition expr={"@internalName = '" + templateInternalName + "'"}/>
            </where>
        </queryDef>
    );

    var template = query.ExecuteQuery();

    if (template.@id != undefined) {
        logInfo("Template found with internal name: " + templateInternalName);
        logInfo("Old source:");
        logInfo(template.@cont);

        // Prepare the DOM structure to update the delivery.
        var deliveryToUpdate = <delivery xtkschema="nms:delivery" id={template.@id}>
            <content>
                <html>
                    <source>{newHtmlSource}</source>
                </html>
            </content>
        </delivery>;

        // Perform the update
        xtk.session.Write(deliveryToUpdate);

        logInfo("Template updated successfully: " + templateInternalName);
    } else {
        logWarning("Template not found with internal name: " + templateInternalName);
    }
}

 

Hope this helps!

 

Thanks,