Querying one feature layer to use to query another layer (problem grabbing result of first layer)

1920
12
Jump to solution
07-06-2021 01:36 PM
Jackson_CountyMissouri
New Contributor III

I have a web map with a search widget. I have two different layers I use for the search widget - an address points layer and a parcels layer. The user will pull down the search widget to select searching either an address from the address points layer, or a parcel number from the parcels layer. However, all parcels need to be selected/highlighted with the parcel number because it is that parcel that needs to be highlighted/selected, and not the address point. So I need to "convert" the addresses from the address points layer to its corresponding parcel number. The parcels layer has an incomplete set of addresses, so I can't use that layer for address-searching.

This is OK because the address points layer also has the parcel number, but in the search widget I only want to show the address. So I can't "directly" grab the parcel number from the address points layer in the search widget, and instead need to get its parcel number via a query on that address point layer. I then need to do another query of the parcels layer to highlight the parcel with the parcel number gotten from the address points layer.

My problem is that I'm having a hard time figuring out how to grab the results of the first (address points) layer query in order to do the 2nd query on the parcels layer. Here is what I have. The following function is called from my search widget handler:

 

	function selectParcel()
	{
        // portion to query address points layer to get parcel # from address points
		var ptsQuery = addressPtsLayerSearch.createQuery();
        var parcelnum;
		ptsQuery.where = "FULLADDR = '" + searchWidget.searchTerm + "'";
		ptsQuery.outFields = ["PARCELNUM"];
		addressPtsLayerSearch.queryFeatures(ptsQuery).then(function(response)
		{
			var feat = response.features[0].attributes;
			parcelnum = feat;
		});

        // portion to select/highlight parcels layer
		var parcelsQuery = parcelsLayer.createQuery();
		parcelsQuery.where = "Name = '" + parcelnum + "'";
		parcelsLayer.queryFeatures(parcelsQuery).then(function(response)
		{
			theView.whenLayerView(parcelsLayer).then(function(layerView)
			{
				var feature = response.features[0];
				layerView.highlight(feature.attributes["OBJECTID"]);
			});

		});
	}

 

The ptsQuery.where works fine - it correctly grabs the address.

PARCELNUM is the name of the field in the address points layer that contains the parcel #.

addressPtsLayerSearch is the name of the address points layer used in the search widget.

My problem is that I've been unable to grab the PARCELNUM from the first query. I've tried every iteration I could find online for the correct syntax to grab the result of my first query, but nothing has worked. The version I'm showing above looks like the most "correct" version according to the ESRI API for FeatureLayer.queryFeatures(), but it still doesn't work. I've also tried the new esri/rest/support/Query library (which is basically the same thing, it looks like) but it doesn't work. (that is, I do: var ptsQuery = new Query() ).

 I also know that the parcelsQuery.where part works below that because if I hard-code a parcel number in place of the parcelnum variable, it correctly highlights the parcel. So I just need to grab the result of the first query correctly.

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Are you sure you have your where clause set up correctly? Have you tested this directly in the service by using the query page?

ptsQuery.where = "FULLADDR = '" + searchWidget.searchTerm + "'";

View solution in original post

0 Kudos
12 Replies
BlakeTerhune
MVP Regular Contributor

Try changing line 11 to include the field name you are trying to get (PARCELNUM)

 

parcelnum = feat.PARCELNUM;

Also, it looks like you're only getting the PARCELNUM of the first record from the first query and the second query is only highlighting the first record. Is that correct?

 

0 Kudos
KenBuja
MVP Esteemed Contributor

What's happening is the first query hasn't returned its results before you start your second query. You should move the second query inside the execution of the first query. Also, you have to specify the field from response.features[0].attributes.

One way to do this would be like this:

 

function selectParcel() {
  var ptsQuery = addressPtsLayerSearch.createQuery();
  var parcelnum;
  ptsQuery.where = "FULLADDR = '" + searchWidget.searchTerm + "'";
  ptsQuery.outFields = ["PARCELNUM"];
  addressPtsLayerSearch.queryFeatures(ptsQuery).then(function(response) {
    parcelnum = response.features[0].attributes["PARCELNUM"];
    var parcelsQuery = parcelsLayer.createQuery();
    parcelsQuery.where = "Name = '" + parcelnum + "'";
    parcelsLayer.queryFeatures(parcelsQuery).then(function(newresponse) {
      theView.whenLayerView(parcelsLayer).then(function(layerView) { 
        var feature = newresponse.features[0]; 
        layerView.highlight(feature.attributes["OBJECTID"]);
      });
    });
  });
}

 

 

Jackson_CountyMissouri
New Contributor III

That didn't work. I even stuck an alert popup at various points in that code and it never even got invoked. I also tried: parcelnum = response.features[0].attributes.ADDPTKEY; per Blake's suggestion above to no avail.

0 Kudos
KenBuja
MVP Esteemed Contributor

In your original code, did you get a response from the first query?

addressPtsLayerSearch.queryFeatures(ptsQuery).then(function(response)

 

0 Kudos
Jackson_CountyMissouri
New Contributor III

No. If I stick a popup in there with something like: alert(response.features[0]); I get "undefined" on the popup. If I do: alert(response.features[0].attributes["PARCELNUM"]); nothing comes up at all, not even a blank popup. Not sure what else to try (I like using popups for debugging).

0 Kudos
Jackson_CountyMissouri
New Contributor III

Actually, I was testing that on the code you gave me. In my original code nothing comes up at all in that section. If I do ...

 

		addressPtsLayerSearch.queryFeatures(ptsQuery).then(function(response)
		{
			alert(response.features[0]);
			var feat = response.features[0].attributes;
			parcelnum = feat.PARCELNUM;
		});
		alert("parcelnum is " + parcelnum);

 

... the parcelnum popup comes first (undefined) and then the popup with response.features[0] comes up second as undefined also.

0 Kudos
KenBuja
MVP Esteemed Contributor

Just as a side note, the parcelnum popup shows up first because of the timing issue. It takes a little while to resolve the query, but the code has already moved on to the alert line. That's why it's undefined (and would be undefined even if your query returned the proper result).

Also, where did "ADDPTKEY" come from?

 

0 Kudos
Jackson_CountyMissouri
New Contributor III

Sorry, ADDPTKEY is PARCELNUM. I changed the name in the code here for clarity but forget to do so in the last post. I just fixed it here.

0 Kudos
KenBuja
MVP Esteemed Contributor

Are you sure you have your where clause set up correctly? Have you tested this directly in the service by using the query page?

ptsQuery.where = "FULLADDR = '" + searchWidget.searchTerm + "'";
0 Kudos