Like 'Cascade Choice filter' while using JavaScript to pulldata from feature service

1601
8
12-10-2020 06:52 AM
ChrisMaclaurin
New Contributor III

Further to my Re: JS script for dummy! post, which pulled data from the feature service layer to return a Max value using JS script kindly helped by Ismael. While this serves for a single batch of survey data. I am hoping to use the the same survey and hosted layer to collect the same data at several different site locations (a choice question, rather than geopoint). Thus I have now realised that i need to filter the attribute Max value based on another field selected on the form (such as place name). 

Is it possible to use the previous JS script to help provide a Max value based on a selection from a previous question (place name) response on the survey form? If so how would this be done? 

Update - I am also wondering about JavasScript to potentially return the 'latest' submitted value of a field from a feature layer? So this would cross reference submission date/time I suppose.

Whilst I have embarked on an online JavaScript course from Codecademy I do feel it will take some time to get to the proficiency to understand how to write code to achieve this. Any pointers would help tremendously.

Best Regards Chris

0 Kudos
8 Replies
ZacharySutherby
Esri Regular Contributor

Hello @ChrisMaclaurin

In the sample that Ismael provided in that other post he's querying the feature service's REST endpoint. 

In his sample he's using 1=1 for the `where` parameter which is searching all fields and attributes. You can change that `where` parameter to be where=<PlaceNameField>=<PlaceNameValue>. 

As for returning the latest submitted value you can specify the `orderByFields` parameter to sort by submitted date or last edit date. 

Please use this link for reference regarding the query feature/layer operation: https://developers.arcgis.com/rest/services-reference/query-feature-service-layer-.htm

Thank you,

Zach

Thank you,
Zach
ChrisMaclaurin
New Contributor III

Thanks Zach, this is a better way than I was going down, I didn't appreciate the script 1=1 so thanks for pointing that out. I'll still need to wrap my head around the `orderByFields` syntax, and also seems a more concise way again. Thanks for the link too! Appreciated cheers.

0 Kudos
SteveRoth
New Contributor II

Hi Chris, were you able to get the JS to pull a value from the latest submitted survey?  I have a survey form that I'd like to set the default values to the prior submitted value.  Thanks!

-Steve

0 Kudos
ChrisMaclaurin
New Contributor III

Hi Steve, Yes I managed to sucessfully get a returned value using a JS function in the script editor. Though I am not sure about if the XML 'pulldata' works in the default field , I'm pretty sure it only works in the calculation field. Also I am not sure if you could indirectly populate another question type by referencing another field in the defualt coulmn?

I just returned the value to a 'note' type question which pretty much works like a default anyway.  I later decided to use that value again in the calculation field in a later question which prepopulates like a default, except it updates eachtime a relevant field is submitted to the hosted layer! Capture.JPG

Here the waypoint number 4 is the returned vaue from the hosted layer and the following question is a caluation field populated with int(${lastWayPoint})+1. So in a way it acts like a default value. Not sure if that's what you're after? hope it helps.

0 Kudos
SteveRoth
New Contributor II

Thanks Chris.  I've been able to manipulate a pulldata JS to prepopulate weather data in my form by placing the pulldata in the calculated field which gets the job done.  Would you be willing to share your JS?  While I've been able to work through other JS, I'm a total newbie and would appreciate the sample code.

-Steve

0 Kudos
ChrisMaclaurin
New Contributor III

Hi Steve, No problem. I am by  no means an expert, I had to do a whole Javascript course on CodeCademy (started in January) to work it out (after initial help from @IsmaelChivite ... so there's probabaly much that could be done better (like @ZacharySutherby  mentioned above for querrying the feature layer) but below worked so far for me... (the token argument to the querry was needed as the layer is private and there's an extra line of XML to get that  - pulldata("@property","token") saved as a hidden question type, name of ${token}.

function getWaypoint(token, Site) {

	var fl ='https://services1.arcgis.com/JZM7qJpmv7vJ0Hzx/arcgis/rest/services/..../FeatureServer/0'; // use your layer here //
	var field = "waypoint"; 
	var secondField = "Site"
	var URL = fl + '/query?where=1=1&outFields=' + field + ',' + secondField + '&f=json'
	var siteArray = []
	var xmlhttp = new XMLHttpRequest();
	
	if (token){
		URL = URL + "&token=" + token;
	}
	xmlhttp.open("GET",URL,false);
	xmlhttp.send();

	if (xmlhttp.status!==200){
		return xmlhttp.status;
	} else {
		var responseJSON=JSON.parse(xmlhttp.responseText)
	}
	if (responseJSON.error){
		return JSON.stringify(responseJSON.error);
	} else {
  	for (let i = 0; i < responseJSON.features.length; i += 1) {
        if (responseJSON.features[i].attributes.Site === Site) {
        siteArray.push(responseJSON.features[i].attributes.waypoint);
		}
	}
		var lastItem = siteArray.pop();
		return lastItem	
	}  
}

 I hope this helps

0 Kudos
ChrisMaclaurin
New Contributor III

Hi @ZacharySutherby 


Finally got around to exploring your suggestion... (please bare with me, I have only started learning about API's and JavaScript in January).


I see that this is generally a far more efficient way overall. The returning json results appear to be the same, not surprised as these are the outFields specified, then the rest of the function takes care of the sorting. If I narrow down to the one field in the quey then I can not cross-reference with another field which I need to be garanteed returning the deired result


As I am also cross-referencing another field (in this case Saltmarsh field) my query needs to have both in outfields. convieniently enough I am finding the order of the output appears to always be in ascending CreationDate value without me specifying orderByFields.


Please correct me if I'm wrong, my understding is that the query part 'pulls' the data locally as json? and the rest of the function works with it to return the value desired?


I am probably missing the overall point of your suggestion, can only the one desired value be obtained in the returning json from the REST endpoint in such a case as this?


Thanks for helping 🙂 Cheers
Chris

0 Kudos
ZacharySutherby
Esri Regular Contributor

Hello @ChrisMaclaurin

That is correct the result of the query request will be a JSON response that can then be worked with in the function. What I would suggest is having the JavaScript function return the full JSON response and then use pulldata("@json") in the survey to extract specific pieces for specific questions. 

If you are looking for only one result you can use the resultRecordCount parameter to specify how many features are returned. If you are looking for all features that match your where clause you wouln't need to specify a resultRecordCount. 

Please check out the JavaScript sample in Survey123 Connect for an example showing the pulldata("@json") function. 

You can also use an API development software like Postman to tinker with the query request in order to return exactly what you are looking for and then use the code snippet section to view how the request is formatted in a JavaScript XHR format. 

Thank you, 

Zach

Thank you,
Zach
0 Kudos