Updating Data Extract Activity in SFMC: Leveraging REST API Calls and SSJS Script

Uzumaki_Naruto_07
3 min readJan 16, 2024

--

While working on establishing a dynamic approach for updating export file names within SFMC automation, I utilized REST API calls with SSJS to update activities with filename patterns. Although I successfully identified the API endpoint for updating file transfer activities, I faced a hurdle in updating data extract activities due to the lack of official Rest API endpoint documentation. Nevertheless, I discovered a viable endpoint for updating data extract activities: https://{{et_subdomain}}.rest.marketingcloudapis.com/automation/v1/dataextracts. Kindly substitute {{et_subdomain}} with the relevant REST Base URI

The example below illustrates using SSJS to perform the update on the data extract activity

  1. For updating the data extension extract activity:
<script runat=server>
Platform.Load("core", "1");

// Set your Marketing Cloud credentials
var clientId = "clintid"; // updated it with your clientid
var clientSecret = "clientsecret";// updated it with your clientsecret
var subdomain = "mcdp1r1yXXXXXXXXXXXXXXX";// updated it with your clientsecret

// Function to obtain an access token using OAuth 2.0 Resource Owner Password Credentials (ROPC) grant type
function getAccessToken() {
var authUrl = "https://" + subdomain + ".auth.marketingcloudapis.com/v2/token";
var authPayload = {
"grant_type": "client_credentials",
"client_id": clientId,
"client_secret": clientSecret
};

var authResult = HTTP.Post(authUrl, "application/json", Stringify(authPayload));
var authResponse = Platform.Function.ParseJSON(authResult.Response[0]);

return authResponse.access_token;
}

// Obtain the access token
var accessToken = getAccessToken();
// Updating the data extract activity
var dataextractid = "3a9fcdc4–70c9–4cd7–8de1-a0f99b3xxxx"; // Update it with you data extract id you can fetch it from you activity URL
var dataextractendpoint = "https://" + subdomain + ".rest.marketingcloudapis.com/automation/v1/dataextracts/" + dataextractid;
// Set up the request payload for the PATCH request
var patchPayload_extract = {
"name": "Test Extract",
"key": "3A9FCXX4–70C9–4CD7–8DE1-A0F99B32XXXX",
"description": "Testing_API",
"fileSpec": "test04.csv",
"dataFields": [{
"name": "ColumnDelimiter",
"type": "string",
"value": ","
}, {
"name": "DECustomerKey",
"type": "string",
"value": "16929A7F-9236–4804–864C-C748E8692XXXX"
}, {
"name": "HasColumnHeaders",
"type": "bool",
"value": "True"
}, {
"name": "TextQualified",
"type": "bool",
"value": "True"
}, {
"name": "UsesLineFeed",
"type": "bool",
"value": "True"
}],
"dataExtractDefinitionId":dataextractid,
"dataExtractTypeId": "bb94a04d-9632–4623-be47-daabc3f588a6",// this a id for data extension extract activity
"intervalType": 0,
"extractTypeName": "Data Extension Extract"
}
// Convert payload to string
var patchPayload_extract_String = Stringify(patchPayload_extract);
var auth = 'Bearer ' + accessToken;
var req2 = new Script.Util.HttpRequest(dataextractendpoint); // Go though SFMC documentation on the Script.Util.HttpRequest function
req2.emptyContentHandling = 0;
req2.retries = 2;
req2.continueOnError = true;
req2.contentType = "application/json"
req2.setHeader("Authorization", auth);
req2.method = "PATCH";
req2.postData = patchPayload_extract_String;
var resp2 = req2.send();
Platform.Response.Write(resp2.content)
</script>

2. For updating the UTF16 to ASCII Converter extract activity:

// use the auth code from the above code block
// Updating the data extract conversion activity (UTF16 to ASCII)
var dataextractascii_id = "94fXX8d4-720e-4b10-8207-ea2de6f5XXXX"; // Update it with you data extract id you can fetch it from you activity URL
var dataextractendpoint_ascii = "https://" + subdomain + ".rest.marketingcloudapis.com/automation/v1/dataextracts/" + dataextractascii_id;
// Set up the request payload for the PATCH request
var patchPayload_extract_ascii = {
"name": "Test_SC2",
"key": "94FA38D4-720E-4B10-8207-EA2DE6XXXXX",
"description": "testing_API",
"fileSpec": "test4.csv",
"dataExtractDefinitionId":dataextractascii_id,
"dataExtractTypeId": "e7e14f95-b925-462d-b6b4-4a6b1dca7ca1",// this a id for conversion extract activity
"intervalType": 2
}
// Convert payload to string
var patchPayload_extract_ascii_String = Stringify(patchPayload_extract_ascii);

var req2 = new Script.Util.HttpRequest(dataextractendpoint_ascii);
req2.emptyContentHandling = 0;
req2.retries = 2;
req2.continueOnError = true;
req2.contentType = "application/json"
req2.setHeader("Authorization", auth);
req2.method = "PATCH";
req2.postData = patchPayload_extract_ascii_String;

var resp2 = req2.send();
Platform.Response.Write(resp2.content);

--

--

No responses yet