Hi there!
I've been working all day on a custom JavaScript expression in Survey123 and I feel like I'm SO close but I can't figure out what I'm missing.
The inspector we are working with wants to be able to see the TreeID of the last record submitted as they fill out the current survey. I'm using Ismael Chivite's template, 'getLastSubmittedRecord.js' which has been incredibly helpful, but I'm still getting an error result of "[object Object]" instead of a proper JSON response. I looked up this error and found it has something to do with requesting a JSON string instead of a Java Script object, so I'm confused why I am getting it when I'm requesting a JSON string.
See below for photos and script example
function getMyLastSubmittedRecord(username,token){
var featureLayer = "https://services.arcgis.com/3YWgO47sUznrJ4Fa/arcgis/rest/services/RCKC_Tree_Maintenance/FeatureServer";
var xmlhttp = new XMLHttpRequest();
var url = featureLayer + "/query?outFields=CreationDate&returnHiddenFields=false&returnGeometry=false&returnQueryGeometry=false&orderByFields=OBJECTID+DESC&resultRecordCount=1&f=json";
if (token && username){
url = url + "&token=" + token + "&where=Creator='" + username + "'";
} else {
return "You must be logged in";
}
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 responseJSON.error;
} else {
if (responseJSON){
return JSON.stringify(responseJSON);
}
else{
return "Never submitted before";
}
}
}
}
I noticed a difference in your code vs. Ismael's, starting in line 23...
Here's a screenshot comparing the two sections:
Did you intentionally change that section, or does changing that fix your issue?
Hi @Katie_Clark , thanks so much for your response!I think that did fix the issue!
Unfortunately, now I'm finding that there are some issues with my JS script. I'm getting {"code":498,"message":"Invalid token.","details":[]} instead of a proper response.
I tried following James' instructions in this post and created a proxy service to see if that would fix things, but now the token generated in my survey is for a different service and does not work, prompting another 498 error.
Just to see, I tried recreating the URLs that are being generated within the JS function to see what they looked like outside of S123.
1. When I manually create a URL (Feature Layer service URL+ query text + token + creator username) that references the layer associated with the survey and uses the token generated using the pulldata() function in S123 I get the response I'm looking for. However, when survey123 makes its request using the same URL and token, I get the 498 error saying my token is invalid.
2. When I reference the service created following James' steps in the aforementioned article but continue to use the token generated from the pulldata() function I get a response just saying:
{"layers":[]}
3. Finally I tried manually creating a URL that references the utility service but has no token (to see if the token being used was working properly) and I got this result:
According to ESRI's Survey123 documentation, an error of 498/Invalid token is "most often caused by a submission URL trying to pass a token to a public feature layer." However the feature layer associated with my survey is not public and shared on an organizational level so I am not sure what to make of this.
Below is a paste of my current JS script. I've made some small changes since Friday but don't see any of those issues being what's causing my issue.
function getMyLastSubmittedRecord(username, mytoken){
var featureLayer = "https://utility.arcgis.com/usrsvcs/servers/db32f6d4af2d4962bebd537ffd5350e4/rest/services/service_4febf86fb23f498aa83ff68ed6668727/FeatureServer";
var xmlhttp = new XMLHttpRequest();
var newurl = featureLayer + "/query?outFields=CreationDate&returnHiddenFields=false&returnGeometry=false&returnQueryGeometry=false&orderByFields=OBJECTID+DESC&resultRecordCount=1&f=json";
if (mytoken && username){
var url = newurl + "&token=" + mytoken + "&where=Creator=" + username;
} else {
return "You must be logged in";
}
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 {
if (responseJSON.features[0]){
return JSON.stringify(responseJSON.features[0]);
}
else{
return "Never submitted before";
}
}
}
}
Any suggestions? When I search other boards a lot of them reference enterprise which we do not have. So I'm not sure what direction to troubleshoot in.