Question
We are looking to automate the process of pausing, saving (or publishing), and resuming an API Event journey when the transaction-specific content within an email template is updated.
Our email templates consist of a standard header and footer, while the center panel contains dynamic content specific to transactions. This center panel is managed by the development team and deployed using the Asset Management API via POST or PATCH requests to /asset/v1/content/assets. The center panel is referenced dynamically in the email template using ContentBlockByID("nn") or ContentBlockByName("abc").
Since this content requires frequent updates, we need to automate the entire deployment, including pausing the journey before making changes, publishing the updates, and then reactivating the journey.
However, I couldn’t find any documentation or forum discussions on how to accomplish this. Specifically, there don’t appear to be any REST APIs or SSJS scripts that can be triggered from Automation Studio to handle this process.
Is this automation feasible? If so, how can we implement it? Any references, examples, or best practices would be greatly appreciated.
Answer
Yes, automating the deployment of an API Event journey after an email template update is completely feasible.
First, let’s clarify a misunderstanding: you can run SSJS scripts via the SSJS Script Activity in Automation Studio, and those scripts can make API calls. Additionally, the Content Builder REST API allows you to update email assets, and WSProxy can be used to manage Triggered Send Definitions (which are used in journeys).
Step 1: Update the Email Template Using the Content Builder API
You can use the Content Builder API to update the email asset using a PATCH request. Here’s an SSJS function to handle this update:
function updateAssetCall(assetId, payload, accessToken) {
var endpoint = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/asset/v1/content/assets/" + assetId;
var method = "PATCH";
var contentType = "application/json;charset=UTF-8";
var headerName = "Authorization";
var headerValue = "Bearer " + accessToken;
return sendApiCall(endpoint, method, contentType, payload, headerName, headerValue);
}
function sendApiCall(endpoint, method, contentType, payload, headerName, headerValue) {
var req = new Script.Util.HttpRequest(endpoint);
req.method = method;
req.setHeader(headerName, headerValue);
req.contentType = contentType;
if (method == "POST" || method == "PATCH") {
req.postData = typeof payload === 'object' ? Stringify(payload) : payload;
}
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
try {
var resp = req.send();
return Platform.Function.ParseJSON(String(resp.content));
} catch (e) {
return e;
}
}Code Explaination
This JavaScript code, written in Server-Side JavaScript (SSJS) for Salesforce Marketing Cloud (SFMC), provides a way to update an email asset in Content Builder using the REST API.
It consists of two functions: updateAssetCall() and sendApiCall(). The updateAssetCall() function is responsible for constructing and sending an API request to modify an existing asset in Content Builder.
It takes three parameters: assetId (the unique ID of the asset to be updated), payload (the new data replacing the existing asset details), and accessToken (a valid OAuth token for authentication).
It constructs the API endpoint using the provided assetId and then calls sendApiCall() with the appropriate HTTP method (PATCH), content type, authorization headers, and payload.
This function returns the API response after attempting the update.
The sendApiCall() function is a reusable helper function that executes an HTTP request to the given endpoint. It takes multiple parameters, including the URL, HTTP method, content type, payload, and authorization headers. Inside the function, an HTTP request object is created using Script.Util.HttpRequest(endpoint), and its method is set to PATCH, POST, or any specified type.
Headers, including the Authorization token, are added, and for POST or PATCH requests, the request body (postData) is assigned. The function is designed to handle API failures by implementing retry logic (req.retries = 2) and error handling within a try...catch block. If successful, the response is parsed using Platform.Function.ParseJSON(), allowing for further processing of the returned data.
In a successful API call, the function will return an object containing details of the updated asset, including its id, name, and updated views content. If an error occurs, such as an invalid authentication token, it will return an error message, such as "Unauthorized", with an appropriate HTTP status code. This approach streamlines asset management in SFMC by automating updates without manual intervention.
The updateAssetCall() function is specific to Content Builder updates, whereas sendApiCall() is a general-purpose function that can be used for various API interactions within SFMC. With built-in retry mechanisms, JSON parsing, and flexible HTTP method support, these functions provide a robust and efficient way to interact with the SFMC REST API.
Step 2: Pause, Publish, and Resume the Journey Using WSProxy
For journeys that use Triggered Send Definitions, you can use WSProxy to pause, refresh, and restart the send definitions associated with the journey emails. Here’s how:
Platform.Load("core", 1);
var prox = new Script.Util.WSProxy();
function republishTS(data) {
var dataArray = Platform.Function.ParseJSON(data);
for (var i = 0; i < dataArray.length; i++) {
var dataObject = dataArray[i];
var customerKey = dataObject.customerKey;
var businessUnit = dataObject.businessUnit;
var triggeredSendName = dataObject.triggeredSendName;
prox.resetClientIds();
prox.setClientId({ "ID": businessUnit });
var resultPause = prox.updateItem("TriggeredSendDefinition", {
"CustomerKey": customerKey,
"TriggeredSendStatus": "Inactive"
});
var resultPublish = prox.updateItem("TriggeredSendDefinition", {
"CustomerKey": customerKey,
"RefreshContent": true
});
var resultRestart = prox.updateItem("TriggeredSendDefinition", {
"CustomerKey": customerKey,
"TriggeredSendStatus": "Active"
});
}
}Code Explaination
This Server-Side JavaScript (SSJS) code is designed to pause, refresh, and restart a Triggered Send Definition (TSD) in Salesforce Marketing Cloud (SFMC) using WSProxy, an optimized API wrapper for interacting with the SOAP API. The script first loads the Core library (Platform.Load("core", 1)) and initializes a WSProxy instance (var prox = new Script.Util.WSProxy();), which facilitates efficient interaction with SFMC objects.
The republishTS(data) function takes a JSON string (data) as input, which contains an array of objects, each representing a Triggered Send Definition with attributes: customerKey (unique identifier for the TSD), businessUnit (specific SFMC Business Unit ID where the TSD exists), and triggeredSendName (name of the triggered send). The function parses the JSON data into a JavaScript array (dataArray) and then iterates through each object.
For every TSD, the function first resets and sets the Business Unit context using prox.resetClientIds(); and prox.setClientId({ "ID": businessUnit });. This ensures that updates are applied to the correct Business Unit, especially in multi-BU environments. Then, it performs three sequential updates using prox.updateItem():
- Pauses the Triggered Send by setting
"TriggeredSendStatus": "Inactive". This prevents emails from being sent while updates are applied. - Refreshes the Content by setting
"RefreshContent": true, ensuring that any updates to email assets or content blocks are applied. - Reactivates the Triggered Send by setting
"TriggeredSendStatus": "Active", allowing it to resume sending emails.
This script is useful in scenarios where an email template or content block within a Triggered Send Definition is updated, and the changes need to be reflected without manual intervention. By automating this process, it ensures that any content modifications take effect without disrupting the email-sending process. The use of WSProxy makes this approach efficient and less resource-intensive compared to traditional SOAP API calls.
Final Thoughts
updateAssetCall()is specific to updating Content Builder assets in SFMC.sendApiCall()is a reusable function that can make any API request.- These functions help automate content updates in SFMC without manual intervention.
- The approach follows best practices such as error handling, retries, and JSON parsing.
Real-Time Project-Based Salesforce Training to Kick Start Your Career
Our Salesforce course is designed to give you a comprehensive understanding of the Salesforce platform, equipping you with the key skills needed to excel in the CRM industry. Covering essential modules such as Salesforce Admin, Developer, and AI, the program combines theoretical learning with hands-on application. Through real-world projects and practical exercises, you’ll gain the expertise to tackle complex business challenges using Salesforce solutions. Our experienced instructors ensure you develop both technical proficiency and industry knowledge to succeed in the Salesforce ecosystem.
In addition to technical training, our Top 10 Salesforce training institutes in Hyderabad provides personalized mentorship, certification support, and interview coaching to enhance your career opportunities. With access to in-depth study materials, hands-on project experience, and dedicated guidance, you’ll be well-prepared for certification exams and real-world scenarios. Start your Salesforce journey with us and open the door to exciting career possibilities. Sign up for a Free Demo today!

Leave a Reply
You must be logged in to post a comment.