verified
Retrieve all Script ActivitiesLearn how to retrieve all Script Activities in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the REST API method.
REST API
var pagesize = 300,
page = 1,
fields = [
'name',
'description',
'categoryId',
'script',
'ssjsActivityId',
'status'
];
var filter = "name%20like%20''";
var queryString = "$fields=" + fields.join(",");
queryString += "&$filter=" + filter;
queryString += "&$pagesize=" + pagesize;
var result = [],
hasMore = true;
while(hasMore) {
var endpoint = restInstanceUrl + "automation/v1/scripts?" + queryString + "&$page=" + page;
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "GET";
request.contentType = "application/json";
request.encoding = "UTF-8";
var results = request.send();
var content = Platform.Function.ParseJSON(String(results.content));
var items = content.items;
if(items == null || items.length == 0) {
hasMore = false;
}
for(var i in items) {
result.push(items[i]);
}
page++;
}
<script runat="server">
Platform.Load("core", "1");
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var pagesize = 300,
page = 1,
fields = [
'name',
'description',
'categoryId',
'script',
'ssjsActivityId',
'status'
];
var filter = "name%20like%20''";
var queryString = "$fields=" + fields.join(",");
queryString += "&$filter=" + filter;
queryString += "&$pagesize=" + pagesize;
var result = [],
hasMore = true;
while(hasMore) {
var endpoint = restInstanceUrl + "automation/v1/scripts?" + queryString + "&$page=" + page;
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "GET";
request.contentType = "application/json";
request.encoding = "UTF-8";
var results = request.send();
var content = Platform.Function.ParseJSON(String(results.content));
var items = content.items;
if(items == null || items.length == 0) {
hasMore = false;
}
for(var i in items) {
result.push(items[i]);
}
page++;
}
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
[
{
"ssjsActivityId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"name": "MyScript",
"key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"description": "",
"script": "",
"categoryId": 1234,
"createdDate": "2021-08-18T02:43:51.6",
"modifiedDate": "2021-08-18T06:25:49.807"
},
{
"ssjsActivityId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"name": "MyOtherScript",
"key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"description": "",
"script": "",
"categoryId": 1234,
"createdDate": "2021-08-18T07:25:32.993",
"modifiedDate": "2021-08-18T07:26:18.157"
}
]