Survey with PullData custom JavaScript function returning [object Object]

711
2
09-02-2022 11:04 AM
slach
by
New Contributor II

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";
            }
        }
    }
}

slach_0-1662141805938.png

slach_1-1662141824324.png

 

0 Kudos
2 Replies
Katie_Clark
MVP Regular Contributor

I noticed a difference in your code vs. Ismael's, starting in line 23...

Here's a screenshot comparing the two sections:

Katherine_Clark_0-1662143096538.png

 

Did you intentionally change that section, or does changing that fix your issue?

 

Best,
Katie


“The goal is not simply to ‘work hard, play hard.’ The goal is to make our work and our play indistinguishable.”
- Simon Sinek
0 Kudos
slach
by
New Contributor II

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. 

slach_0-1662568584287.png

 

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.

jsonresponse.PNG

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":[]}

Capture.PNG

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:

otherresponse.PNG

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.

0 Kudos