require(["esri/tasks/QueryTask"], function(QueryTask) { /* code goes here */ });
Class: esri/tasks/QueryTask
Inheritance: QueryTask Task Accessor
Since: ArcGIS API for JavaScript 4.0

Executes a Query operation on a layer. The most common method used in this class is execute(), which executes the query as defined in the Query object that is passed as a parameter to the function. QueryTask.execute() returns a Promise that resolves to a FeatureSet, which contains the features in the layer that satisfy the Query.

With QueryTask, you can also obtain the number of features that satisfy the query, the extent of the features, or the featureIds of the features.

For example, when working with a feature layer of world cities, to obtain a FeatureSet of cities with a population greater than one million people, use the following code:

require(["esri/tasks/QueryTask", "esri/tasks/support/Query"], function(QueryTask, Query){
  var citiesLayerUrl = " ... "; // Represents the REST endpoint for a layer of cities.
  var queryTask = new QueryTask({
    url: citiesLayerUrl
  });
  var query = new Query();
  query.returnGeometry = true;
  query.outFields = ["*"];
  query.where = "POP > 1000000";  // Return all cities with a population greater than 1 million
  // When resolved, returns features and graphics that satisfy the query.
  queryTask.execute(query).then(function(results){
    console.log(results.features);
  });

  // When resolved, returns a count of the features that satisfy the query.
  queryTask.executeForCount(query).then(function(results){
    console.log(results);
  });

});
See also:

Constructors

new QueryTask(properties)

Parameter:
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
NameTypeSummaryClass
String

The name of the class.

more details
more detailsAccessor
String

Specify the geodatabase version to display.

more details
more detailsQueryTask
Object

The options to be used for data requests.

more details
more detailsTask
String

The ArcGIS Server REST service URL (usually of a Feature Service Layer or Map Service Layer) for use in a task.

more details
more detailsTask

Property Details

declaredClassStringreadonly inherited

The name of the class. The declared class name is formatted as esri.folder.className.

gdbVersionString

Specify the geodatabase version to display. Requires ArcGIS Server service 10.1 or greater.

requestOptionsObject inherited

The options to be used for data requests. These options can also be controlled through the requestOptions method parameter.

The ArcGIS Server REST service URL (usually of a Feature Service Layer or Map Service Layer) for use in a task.

Method Overview

NameReturn TypeSummaryClass
Promise<FeatureSet>

Executes a Query against an ArcGIS Server map layer.

more details
more detailsQueryTask
Promise<Number>

Gets a count of the number of features that satisfy the input query.

more details
more detailsQueryTask
Promise<Object>

Gets the extent of the features that satisfy the input query.

more details
more detailsQueryTask
Promise<Number[]>

Executes a Query against an ArcGIS Server map layer.

more details
more detailsQueryTask
Promise<FeatureSet>

Executes a RelationshipQuery against an ArcGIS Server map layer (or table).

more details
more detailsQueryTask

Method Details

execute(params, requestOptions){Promise<FeatureSet>}

Executes a Query against an ArcGIS Server map layer. The result is returned as a FeatureSet, which can be accessed using the .then() method. A FeatureSet contains an array of Graphic features, which can be added to the map. This array will not be populated if no results are found.

Parameters:
params Query

Specifies the attributes and spatial filter of the query. This object may be autocast.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise< FeatureSet>When resolved, a FeatureSet containing an array of graphic features is returned.
Example:
require([
  "esri/tasks/support/Query", "esri/tasks/QueryTask", ...
], function(Query, QueryTask, ... ) {
  var query = new Query();
  var queryTask = new QueryTask( ... );
  query.where = "STATE_NAME = 'Washington'";
  query.outSpatialReference = { wkid:102100 };
  query.returnGeometry = true;
  query.outFields = [ "CITY_NAME" ];
  queryTask.execute(query).then(function(results){
    // Results.graphics contains the graphics returned from query
  });
  ...
});

executeForCount(params, requestOptions){Promise<Number>}

Gets a count of the number of features that satisfy the input query. Valid only for layers published using ArcGIS Server 10 SP1 or greater. Layers published with earlier versions of ArcGIS Server return an error to the errback param of .then().

Parameters:
params Query

Specifies the attributes and spatial filter of the query. This object may be autocast.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise< Number>When resolved, the result is the number of features that satisfy the input query.
Example:
require([
  "esri/tasks/QueryTask", ...
], function(QueryTask, ... ) {
  var queryTask = new QueryTask( ... );
  queryTask.executeForCount({  // autocasts as new Query()
     where: "POP90_SQMI &lt; 100"
  }).then(function(count){
    console.log(count, " features matched the input query");
  }, function(error){
       console.log(error); // Will print error in console if unsupported layers are used
     });
});

executeForExtent(params, requestOptions){Promise<Object>}

Gets the extent of the features that satisfy the input query. The count of features that satisfy the input query is returned upon resolution as well. Valid only for hosted feature services on arcgis.com and for ArcGIS Server 10.3.1 and later.

Parameters:
params Query

Specifies the attributes and spatial filter of the query. This object may be autocast.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise< Object>When resolved, returns the extent and count of the features that satisfy the input query. See the object specification table below for details.
PropertyTypeDescription
countnumberThe number of features that satisfy the input query.
extentExtentThe extent of the features that satisfy the query.

executeForIds(params, requestOptions){Promise<Number[]>}

Executes a Query against an ArcGIS Server map layer. The result is an array of the object IDs of features that satisfy the input query.

Parameters:
params Query

Specifies the attributes and spatial filter of the query. This object may be autocast.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise< Number[]>When resolved, the result is an array of object IDs for features that satisfy the input query.
Example:
require([
  "esri/tasks/QueryTask", ...
], function(QueryTask, ... ) {
  var queryTask = new QueryTask( ... );
  queryTask.executeForIds({  // autocasts as new Query()
     where: "region = 'Southern California'"
  }).then(function(results){
    console.log(results);  // an array of object IDs
  });
  ...
});

executeRelationshipQuery(params, requestOptions){Promise<FeatureSet>}

Executes a RelationshipQuery against an ArcGIS Server map layer (or table). If the query is successful, the result is returned as a FeatureSet, which can be accessed using the .then() method.

Parameters:

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
TypeDescription
Promise< FeatureSet>When resolved, the results of this operation are and array of FeatureSet grouped by source layer (or table) object IDs. Each FeatureSet contains an array of Graphic features including the values of the fields requested by the user. This array will not be populated if no results are found.

API Reference search results

NameTypeModule

There were no match results from your search criteria.