I'm trying to create a link to launch a survey123 survey with attributes passed from the selected feature in field maps and then use the callback to return to field maps and update an attribute on the feature that the survey was launched from. I've been reading ESRI's documentation at https://doc.arcgis.com/en/survey123/reference/integratewithotherapps.htm and https://doc.arcgis.com/en/field-maps/ios/help/deploy-your-map.htm along with posts here in the community but can't get the attributes to update with the callback and not sure what I'm missing. I'm using the Arcade content within the popup to add the link and I've attached my Arcade code below. It launches Survey123 and populates attributes in the Survey as I want and returns to field maps when the survey is submitted, but I can't get the attribute to update on the callback.
featureSourceURL is the url to the hosted feature service and the layer within that service is layer 0. I'm thinking this might be part of the problem as if I past the url into the browser I get a token required page, but I've tried generating a token and adding the token to the url and that broke the callback.
var S123 = "arcgis-survey123://?itemID=###"
var S123_Attributes = "&field:Stand_Habitat_Type={Stand_Habitat_Type}&field:incoming={GlobalID}"
var Callback = "&callback="
var CallbackFM = UrlEncode("https://fieldmaps.arcgis.app/?referenceContext=updateFeature")
var callbackID = UrlEncode("&itemID=###")
var callbackFeatureSource = UrlEncode("&featureSourceURL=https://services1.arcgis.com/###/arcgis/rest/services/AspenSurvey/FeatureServer/0")
var callbackFeatureID = UrlEncode("&featureID=")
var callbackFeatureGlobalID = UrlEncode($feature.GlobalID)
var callbackFeatureAttributes = UrlEncode('featureAttributes={"ConditionSurvey":"Yes"}')
var fullCallback = CallbackFM + callbackID + callbackFeatureSource + callbackFeatureID + callbackFeatureGlobalID + callbackFeatureAttributes
var fullURL = S123 + S123_Attributes + Callback + fullCallback
return {
type : 'text',
text : '<a href=' + fullURL + '>Open Survey with Callback</a>'
}
Solved! Go to Solution.
Post edited after I sorted out most of my own issues by undoing other supposed "fixes" (see comments above about the MID function and globalID's).
I have managed to get my code working almost perfectly now. The only spot that I was getting stuck at and for which I've implemented a workaround, is passing back a date value to Field Maps instead of a text value to update the selected features attribute as done (instead of saying "Complete", it states when it was completed). Breif explanation of my code below, as I've extended it to use multiple fields from the source feature.
I couldn't for the life of me though figure out how to get the updateFeature section (Line 80) to work with a date value/field in Field Maps. I've change the fields to text and have now passed a string formatted as "my_username @ 2024-05-14 09:59 17.510" (Lines 45-47):
Below is the full code I have implemented.
// Calculate inspection number
var Inspection = IIF(IsEmpty($feature.insp_2_weeks)==True,"2 weeks",
IIF(IsEmpty($feature.insp_4_weeks)==True,"4 weeks",
IIF(IsEmpty($feature.insp_6_weeks)==True,"6 weeks",
IIF(IsEmpty($feature.insp_8_weeks)==True,"8 weeks",
IIF(IsEmpty($feature.insp_10_weeks)==True,"10 weeks",
IIF(IsEmpty($feature.insp_12_weeks)==True,"12 weeks",
IIF(IsEmpty($feature.insp_4_months)==True,"4 months",
IIF(IsEmpty($feature.insp_5_months)==True,"5 months",
IIF(IsEmpty($feature.insp_6_months)==True,"6 months",
IIF(IsEmpty($feature.insp_9_months)==True,"9 months",
IIF(IsEmpty($feature.insp_12_months)==True,"12 months",
IIF(IsEmpty($feature.insp_15_months)==True,"15 months",
IIF(IsEmpty($feature.insp_18_months)==True,"18 months",
IIF(IsEmpty($feature.insp_21_months)==True,"21 months","24 months"))))))))))))))
// Get matching inspection number field name
var InspFieldName = IIF(Inspection=="2 weeks","insp_2_weeks",
IIF(Inspection=="4 weeks","insp_4_weeks",
IIF(Inspection=="6 weeks","insp_6_weeks",
IIF(Inspection=="8 weeks","insp_8_weeks",
IIF(Inspection=="10 weeks","insp_10_weeks",
IIF(Inspection=="12 weeks","insp_12_weeks",
IIF(Inspection=="4 months","insp_4_months",
IIF(Inspection=="5 months","insp_5_months",
IIF(Inspection=="6 months","insp_6_months",
IIF(Inspection=="9 months","insp_9_months",
IIF(Inspection=="12 months","insp_12_months",
IIF(Inspection=="15 months","insp_15_months",
IIF(Inspection=="18 months","insp_18_months",
IIF(Inspection=="21 months","insp_21_months","insp_24_months"))))))))))))))
// Get centroid Lat and Long
var originShift = 2.0 * PI * 6378137.0 / 2.0;
var lon = (Centroid($feature).X / originShift) * 180.0;
var lat = (Centroid($feature).Y / originShift) * 180.0;
lat = 180.0 / PI * (2.0 * Atan( Exp(lat * PI / 180.0)) - PI / 2.0);
// Generate callback attribute
var user = GetUser($layer).username
var date = left(Date(),10)
var cbvalue = user + " on " + date
// Source: https://community.esri.com/t5/arcgis-survey123-questions/help-with-survey123-link-from-field-maps-with/td-p/1306591
// To launch survey123 from map
// EDIT THIS LINK TO MATCH YOUR SURVEY LINK (Only edit AFTER itemID= and BEFORE &
var urlsource ="arcgis-survey123://?itemID=b40c1e4d99084cbca8170ff273c74977&"
//Format globalid to "link" survey point to map data -- MUST have globalid field for this script to work. DO NOT EDIT THIS LINE
var fglobalid = Mid($feature.GlobalID, 1,36)
//Generate other url parameters to pass to survey123. -- These are the values you wish to transfer from Field Maps to your Survey123 form.
// Edit or delete fields as necessary. The line containing $feature.GlobalID MUST NOT BE REMOVED, but the field to the left of $feature.GlobalID may
// be changed to reflect the field in your Survey to store the GlobalID.
var params = {
"field:plantation": $feature.Plantation,
"field:years_planted": $feature.PlantingYear,
"field:tree_species": $feature.Species,
"field:tenure": $feature.RENDER_LABEL,
"field:inspection_number": Inspection,
};
// Get map center coordinates
var center = concatenate([lat,lon],",")
// Create callback portion of link using itemID (map ID) and featureSourceURL (services link to the layer containing the feature to be edited following survey submission)
// EDIT THIS LINK FOR MAP AND FEATURE SERVICE. updateFeature&itemID=<your maps ID>&featureSourceURL=<link to the feature class to be edited>
var callback = {
callback:"https://fieldmaps.arcgis.app/?referenceContext=updateFeature&itemID=##############&featureSourceURL=https://services3.arcgis.com/############/arcgis/rest/services/#################/FeatureServer/0",
//var callback = {callback:"https://fieldmaps.arcgis.app",
}
// // Add featureID (must be var fglobalid) portion of callback link
// THIS LINE MUST NOT BE CHANGED. This line is what tells the script which point in the feature class will be edited
var callbackid = "featureID=" + fglobalid
var callbackattribute = 'featureAttributes={"' + InspFieldName + '":"' + cbvalue + '"}'
// Return the entire link/URL. This is what the FieldMaps app is following when you click the link to enter survey data. This should not be changed unless there is no callback
// attribute to pass back to Field Maps
/// return urlsource + UrlEncode(params) + "&" + UrlEncode(callback) + "%26" + UrlEncode(callbackid) + "%26" + UrlEncode(callbackattribute)
return urlsource + UrlEncode(params) + "¢er=" + UrlEncode(center) + "&" + UrlEncode(callback) + "%26" + UrlEncode(callbackid) + "%26" + UrlEncode(callbackattribute)
// For use in the pop-up for your featureclass, simply enter the expression name in the link field (i.e. {expression/expr0})