verified
Retrieve a Query Activity's statusLearn how to retrieve a Query Activity's status in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include WSProxy and REST API methods.
WSProxy
var api = new Script.Util.WSProxy();
var name = "MyNewQuery";
var filter = {
Property: "Name",
SimpleOperator: "equals",
Value: name
};
var request = api.retrieve("QueryDefinition", ["Status"], filter);
var result = request.Results[0].Status;
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
var name = "MyNewQuery";
var filter = {
Property: "Name",
SimpleOperator: "equals",
Value: name
};
var request = api.retrieve("QueryDefinition", ["Status"], filter);
var result = request.Results[0].Status;
Write(Stringify(result));
} catch (error) {
Write(Stringify(error));
}
</script>
"Active"
REST API
var queryDefinitionId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var endpoint = restInstanceUrl + "automation/v1/queries/" + queryDefinitionId + "/actions/isrunning/";
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 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 queryDefinitionId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var endpoint = restInstanceUrl + "automation/v1/queries/" + queryDefinitionId + "/actions/isrunning/";
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 result = Platform.Function.ParseJSON(String(results.content));
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"queryDefinitionId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"isRunning": false
}
WARNING
REST API can only indicate if the Query is running or not.
Reference
Ressources and references related to the current methods.
SOAP object