0

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>

1 Answer 1

1

Speaking of your code:

  • First of all, it is not recommended to use undocumented API endpoints, since they might be changed/removed/disabled at any point in time, thus making the solution in a long run unreliable;
  • Secondly, I am not sure how you discovered this particular endpoint, since it is not shown under data/v1/rest and I have tried accessing and got no other results than Error 404;
  • Lastly, in order to use other methods besides GET and POST in Marketing Cloud, you would need to use Script.Util.HttpRequest function instead of HTTP.Patch that you have.

Anyway, here is one way how you can update "isSendable" parameter under a Data Extension:

  1. Retrieve all Data Extensions under the specified folder by filtering CategoryID (Reference)
  2. Parse the response to get the CustomerKeys
  3. For each CustomerKey iterate the update of both "IsTestable" and "IsSendable" parameters ("IsTestable" has to be also false to switch off "IsSendable")

For the third step, you can use this WSProxy code snippet:

<script runat="server">
var prox = new Script.Util.WSProxy();
var customerKey = "xxx";
var config = {
 "CustomerKey": customerKey,
 "IsTestable": 0,
 "IsSendable": 0
};
var res = prox.updateItem("DataExtension", config);
</script>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .