verified
Validate a Query ActivityLearn how to validate a Query Activity in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the REST API method.
Validate a Query
var query = 'SELECT TOP 10 SubscriberKey, EmailAddress FROM _Subscribers';
var payload = {
"Text": query
}
var endpoint = restInstanceUrl + "automation/v1/queries/actions/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 api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var query = 'SELECT TOP 10 SubscriberKey, EmailAddress FROM _Subscribers';
var payload = {
"Text": query
}
var endpoint = restInstanceUrl + "automation/v1/queries/actions/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>
{
"queryValid": true,
"errors": []
}
Validate a Query by Id
var request = api.retrieve("QueryDefinition", ["CustomerKey", "QueryText"], {
Property: "Name",
SimpleOperator: "equals",
Value: "MyNewQuery"
});
var customerKey = request.Results[0].CustomerKey;
var queryText = request.Results[0].QueryText;
var payload = {
"Text": queryText
}
var endpoint = restInstanceUrl + "automation/v1/queries/" + customerKey + "/actions/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 = payload;
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var name = "MyNewQuery";
var request = api.retrieve("QueryDefinition", ["CustomerKey", "QueryText"], {
Property: "Name",
SimpleOperator: "equals",
Value: name
});
var customerKey = request.Results[0].CustomerKey;
var queryText = request.Results[0].QueryText;
var payload = {
"Text": queryText
}
var endpoint = restInstanceUrl + "automation/v1/queries/" + customerKey + "/actions/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>
{
"queryValid": true,
"errors": []
}
Reference
Ressources and references related to the current methods.
SOAP object