verified
Validate a script for a Script ActivityLearn how to validate a Script Activity in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the REST API method.
REST API
var script = '<script runat="server">Platform.Load("core", "1");Write("Hello World");<script>';
var payload = {
script: script
};
var endpoint = restInstanceUrl + "automation/v1/scripts/validate/";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "POST";
request.contentType = "application/json";
request.encoding = "UTF-8";
request.postData = Stringify(payload);
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
<script runat="server">
Platform.Load("core", "1");
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var script = '<script runat="server">Platform.Load("core", "1");Write("Hello World");<script>';
var payload = {
script: script
};
var endpoint = restInstanceUrl + "automation/v1/scripts/validate/";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "POST";
request.contentType = "application/json";
request.encoding = "UTF-8";
request.postData = Stringify(payload);
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"scriptValid": true
}