Last active
November 11, 2023 15:24
-
-
Save zuzannamj/b1c50bc6c5704fd630755ed113816422 to your computer and use it in GitHub Desktop.
Below code pulls all Import and Query activities from Automation Studio along with the names of target Data Extensions which get updated by those activities. See prerequisites in comments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--–– Copyright © 2020 Zuzanna Jarczynska http://sfmarketing.cloud ––--> | |
<script runat="server"> | |
Platform.Load("Core", "1"); | |
var authEndpoint = 'https://mcxxxxxxxxxxxxxx.auth.marketingcloudapis.com/', //provide API endpoint | |
client_id = 'xxxxxxxxxxx', //pass Client ID | |
client_secret = 'xxxxxxxxxx'; //pass Client Secret | |
var targetDE = 'xxxx-xxxx-xxxx-xxxx-xxxxx'; //pass external key of the target DE | |
//initiate WSProxy | |
var prox = new Script.Util.WSProxy(); | |
//initiate and clear data extension | |
var logDE = DataExtension.Init(targetDE); | |
var props = { | |
CustomerKey: targetDE | |
}; | |
try { | |
var data = prox.performItem("DataExtension", props, "ClearData"); | |
} catch (error) { | |
Write(Stringify(error)); | |
} | |
//authenticate to get access token | |
var payload = { | |
client_id: client_id, | |
client_secret: client_secret, | |
grant_type: 'client_credentials' | |
}; | |
var url = authEndpoint + '/v2/token'; | |
var contentType = 'application/json'; | |
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload)); | |
if (accessTokenRequest.StatusCode == 200) { | |
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]); | |
var accessToken = tokenResponse.access_token; | |
var rest_instance_url = tokenResponse.rest_instance_url; | |
} | |
var headerNames = ["Authorization"]; | |
var headerValues = ["Bearer " + accessToken]; | |
//api call to get all imports | |
function getAllImports(accessToken, headerNames, headerValues) { | |
var pagesFetched = 0; | |
var pagesCount = 1; | |
do { | |
try { | |
var page = pagesFetched + 1; | |
var importsUrl = rest_instance_url + 'automation/v1/imports?$page=' + page; | |
var getImports = HTTP.Get(importsUrl, headerNames, headerValues); | |
if (Platform.Function.ParseJSON(getImports.Status) == 0) { | |
var importContent = Platform.Function.ParseJSON(String(getImports.Content)); | |
for (var i = 0; i < importContent.items.length; ++i) { | |
var importName = importContent.items[i].name, | |
importDestinationObjectTypeId = importContent.items[i].destinationObjectTypeId, | |
importDestinationObjectId = importContent.items[i].destinationObjectId; | |
if (importDestinationObjectTypeId == 310) { | |
var cols = ["Name", "CustomerKey", "ObjectID"]; | |
var filter = { | |
Property: "ObjectID", | |
SimpleOperator: "equals", | |
Value: importDestinationObjectId | |
}; | |
var data = prox.retrieve("DataExtension", cols, filter); | |
if (data.Results[0].Name) { | |
logDE.Rows.Add({ | |
DEName: data.Results[0].Name, | |
DEKey: data.Results[0].CustomerKey, | |
ImportName: importName | |
}); | |
} | |
} | |
} | |
} else { | |
Write("Error"); | |
} | |
pagesFetched++; | |
} catch (error) { | |
Write("Exception Error: " + Stringify(error)); | |
pagesFetched = pagesCount + 1; | |
} | |
} while (pagesFetched < pagesCount); | |
} | |
//api call to get all queries | |
function getAllQueries(accessToken, headerNames, headerValues) { | |
var pagesFetched = 0; | |
var pagesCount = 1; | |
do { | |
try { | |
var page = pagesFetched + 1; | |
var queriesUrl = rest_instance_url + 'automation/v1/queries?$page=' + page; | |
var getQueries = HTTP.Get(queriesUrl, headerNames, headerValues); | |
if (Platform.Function.ParseJSON(getQueries.Status) == 0) { | |
var queryContent = Platform.Function.ParseJSON(String(getQueries.Content)); | |
for (var i = 0; i < queryContent.items.length; ++i) { | |
var queryName = queryContent.items[i].name, | |
targetKey = queryContent.items[i].targetKey, | |
targetName = queryContent.items[i].targetName; | |
logDE.Rows.Add({ | |
DEName: targetName, | |
DEKey: targetKey, | |
QueryName: queryName | |
}); | |
} | |
} else { | |
Write("Error"); | |
} | |
pagesFetched++; | |
} catch (error) { | |
Write("Exception Error: " + Stringify(error)); | |
pagesFetched = pagesCount + 1; | |
} | |
} while (pagesFetched < pagesCount); | |
} | |
if (accessToken != null) { | |
getAllImports(accessToken, headerNames, headerValues); | |
getAllQueries(accessToken, headerNames, headerValues); | |
} | |
</script> |
Hi Zuzanna,
Thanks for this solution, this helped me in solve one of my use case. I have a query, Instead of storing the data which we are looking for in a data extension by adding external key, Is it possible to build a custom solution using cloud page to fetch list of SQL query Activities which uses my data extension in Select statements. Something like https://cloud.e.abc.com/Traget_DE?DEName=ABC by adding my target DE name in cloud page should return result.
Thanks in advance.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a prerequisite, create a Data Extension to store results of the script. The Data Extension needs to have the following structure:
Field: DEName / Text / 250
Field: DEKey / Text / 250
Field: ImportName / Text / 250
Field: QueryName / Text / 250
Note, that above script will only return the names of Data Extensions updated by the two types of Automation Studio activities. To pull a full list of all Data Extensions in the account, see here: https://gortonington.com/data-extension-inventory-for-your-sfmc-business-unit/