I'm attempting to update the "Used for sending" value for all DEs in a select folder and include the DEs in all the child folders.
I've written the code below that passes valideation, but I get the following fail error when I run the script activity.
ERROR: A runtime error occurred while the SSJS Activity executed the SSJS script. Please review the SSJS activity to ensure that errors are handled properly.
<script runat="server">
Platform.Load("core", "1.1.1");
// Set the Marketing Cloud credentials
var authUrl = "https://subdomain-here.auth.marketingcloudapis.com/v2/token";
var clientId = "XXXXXXCLIENTIDXXXXX";
var clientSecret = "XXXXXCLIENTSECRETXXXXX";
var username = "USERNAME";
var password = "PASSWORD";
// Set the root folder ID for the folder structure you want to update
var rootFolderId = "XXXXXXXXX";
// Get the access token from the Marketing Cloud API
var payload = {
"grant_type": "password",
"client_id": clientId,
"client_secret": clientSecret,
"username": username,
"password": password
};
var response = HTTP.Post(authUrl, "application/json", Stringify(payload));
var accessToken = Platform.Function.ParseJSON(response.Response[0]).access_token;
// Set the headers for the Marketing Cloud API requests
var headers = {
"Authorization": "Bearer " + accessToken,
"Content-Type": "application/json"
};
// Recursively traverse the folder structure and update the "Used for sending" attribute for all data extensions
function updateFolder(folderId) {
// Get a list of all data extensions in the specified folder
var deUrl = "https://subdomain-here.rest.marketingcloudapis.com//data/v1/customobjectdefinitions?$filter=ParentFolder.Id eq " + folderId;
var response = HTTP.Get(deUrl, headers);
var dataExtensions = Platform.Function.ParseJSON(response.Response[0]).items;
// Update the "Used for sending" attribute for each data extension
for (var i = 0; i < dataExtensions.length; i++) {
var deId = dataExtensions[i].customerKey;
var deUrl = "https://subdomain-here.rest.marketingcloudapis.com//data/v1/customobjectdefinitions/" + deId;
var payload = {
"isSendable": false
};
var response = HTTP.Patch(deUrl, "application/json", Stringify(payload), headers);
Platform.Response.Write(deId + ": " + response.StatusCode + "<br>");
}
// Recursively update all subfolders
var folderUrl = "https://subdomain-here.rest.marketingcloudapis.com//assets/v1/content/assets/" + folderId + "/children";
var response = HTTP.Get(folderUrl, headers);
var folderContent = Platform.Function.ParseJSON(response.Response[0]);
for (var i = 0; i < folderContent.items.length; i++) {
var contentItem = folderContent.items[i];
if (contentItem.contentType == "application/vnd.etmc.folder") {
var subfolderId = contentItem.id;
updateFolder(subfolderId);
}
}
}
// Start the folder traversal from the root folder ID
updateFolder(rootFolderId);
</script>