Managing multiple journeys in Salesforce Marketing Cloud can be challenging, especially when it comes to tracking which Data Extensions are used in each journey. This task becomes even more cumbersome as the number of journeys grows. To streamline this process, you can use Server-Side JavaScript (SSJS) to automatically fetch and display the Data Extensions associated with each Journey Builder.
This SSJS script retrieves all Journey Builders and their associated Data Extensions, displaying the information in a table format. This can be especially useful for administrators and developers who need to quickly identify Data Extensions without manually inspecting each journey.
<script runat="server">
Platform.Load('Core', '1');
// Function to make HTTP GET requests with authorization header
function makeHttpRequest(url, authToken) {
try {
var status = [0];
var content = Platform.Function.HTTPGet(url, false, 0, ['Authorization'], ['Bearer ' + authToken], status);
if (status[0] !== 0) {
return { status: status[0], content: 'HTTP request failed with status code ' + status[0] };
}
return { status: status[0], content: content };
} catch (e) {
return { status: -1, content: 'Error: ' + e.message };
}
}
try {
var subdomain = '{{Subdomain}}';
var bearerToken = '{{Token}}';
var getJourneysEndpoint = "https://" + subdomain + ".rest.marketingcloudapis.com/interaction/v1/interactions";
var getJourneyDetailsEndpoint = "https://" + subdomain + ".rest.marketingcloudapis.com/interaction/v1/eventDefinitions/?name=";
// Fetch journey data
var journeysResponse = makeHttpRequest(getJourneysEndpoint, bearerToken);
if (journeysResponse.status !== 0) {
Platform.Response.Write('<p>Error Status: ' + journeysResponse.status + '<br>Message: ' + journeysResponse.content + '</p>');
return;
}
var journeysJson = Platform.Function.ParseJSON(journeysResponse.content);
var journeyNames = [];
for (var i = 0; i < journeysJson.items.length; i++) {
journeyNames.push(journeysJson.items[i].name);
}
// Fetch journey details
var journeyDetailsOutput = '<table border="1" cellpadding="5" cellspacing="0"><tr><th>Journey Name</th><th>Data Extension Name</th></tr>';
for (var k = 0; k < journeyNames.length; k++) {
var journeyName = journeyNames[k];
var journeyDetailsResponse = makeHttpRequest(getJourneyDetailsEndpoint + encodeURIComponent(journeyName), bearerToken);
if (journeyDetailsResponse.status !== 0) {
journeyDetailsOutput += '<tr><td colspan="2">Error Status for ' + journeyName + ': ' + journeyDetailsResponse.status + '<br>Message: ' + journeyDetailsResponse.content + '</td></tr>';
continue;
}
var journeyDetailsJson = Platform.Function.ParseJSON(journeyDetailsResponse.content);
if (journeyDetailsJson.items.length > 0) {
for (var l = 0; l < journeyDetailsJson.items.length; l++) {
var item = journeyDetailsJson.items[l];
if (item.dataExtensionName) {
journeyDetailsOutput += '<tr><td>' + item.name + '</td><td>' + item.dataExtensionName + '</td></tr>';
}
}
}
}
journeyDetailsOutput += '</table>';
Platform.Response.Write('<h2>Journey Details:</h2>' + journeyDetailsOutput);
} catch (ex) {
Platform.Response.Write('<p>Error Occurred: ' + ex.message + '</p>');
}
</script>
Authorization: The script uses a Bearer token to authorize API requests.
Fetch Journeys: It retrieves a list of all journeys.
Fetch Details: For each journey, it retrieves the associated Data Extensions.
Display Results: The results are formatted into a table for easy review.
This approach not only saves time but also helps ensure accuracy when managing your journeys and Data Extensions. Feel free to customize the script based on your specific requirements and environment.