verified
Start a Query ActivityLearn how to start (run, perform) a Query Activity in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include Core, WSProxy and REST API method.
Core
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var qd = QueryDefinition.Init(customerKey);
var result = qd.Perform();
<script runat="server">
Platform.Load("core", "1");
try {
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var qd = QueryDefinition.Init(customerKey);
var result = qd.Perform();
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
"QueryDefinition perform called successfully"
WSProxy
var api = new Script.Util.WSProxy();
var objectId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var props = {
"ObjectID": objectId
}
var result = api.performItem("QueryDefinition", props, "Start", {});
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
var request = api.retrieve("QueryDefinition", ["ObjectID"], {
Property: "Name",
SimpleOperator: "equals",
Value: "MyNewQuery"
});
var objectId = request.Results[0].ObjectID;
var props = {
"ObjectID": objectId
}
var result = api.performItem("QueryDefinition", props, "Start", {});
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"Status": "OK",
"StatusMessage": "",
"RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"Results": [
{
"Object": {
"Name": null,
"ObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"QueryText": null,
"TargetType": null,
"DataExtensionTarget": null,
"TargetUpdateType": null,
"FileSpec": null,
"FileType": null,
"Status": null,
"CategoryID": 0,
"InteractionObjectID": null,
"Description": null,
"Keyword": null,
"Client": null,
"PartnerKey": null,
"PartnerProperties": null,
"CreatedDate": "0001-01-01T00:00:00.000",
"ModifiedDate": null,
"ID": 0,
"CustomerKey": null,
"Owner": null,
"CorrelationID": null,
"ObjectState": null,
"IsPlatformObject": false
},
"Task": {
"StatusCode": "OK",
"StatusMessage": "OK",
"OrdinalID": 0,
"ErrorCode": 0,
"ID": "124811914",
"TblAsyncID": 124811914,
"InteractionObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
},
"ProgramActivityInstanceId": null,
"StatusCode": "OK",
"StatusMessage": "QueryDefinition perform called successfully",
"OrdinalID": 0,
"ErrorCode": 0,
"RequestID": null,
"ConversationID": null,
"OverallStatusCode": null,
"RequestType": "Synchronous",
"ResultType": null,
"ResultDetailXML": null
}
]
}
REST API
var queryDefinitionId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var endpoint = restInstanceUrl + "automation/v1/queries/" + queryDefinitionId + "/actions/start/";
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";
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", ["ObjectID"], {
Property: "Name",
SimpleOperator: "equals",
Value: name
});
var queryDefinitionId = request.Results[0].ObjectID;
var endpoint = restInstanceUrl + "automation/v1/queries/" + queryDefinitionId + "/actions/start/";
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";
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
"\"OK\""
Reference
Ressources and references related to the current methods.
Official documentation
SOAP object