Select to view content in your preferred language

Survey123 add related record to existing feature

2145
7
Jump to solution
02-24-2022 01:47 PM
DarrylKlassen1
Frequent Contributor
Hello,
I have an existing feature service that has spatial data (buildings) and an inspections table related to it. I want to collect inspections against them using Survey123. Basically I would like to have a dropdown of all the building names - which would then find the globalid of that building - and populate the foreign key of the inspections so the relationship remains valid. I don't know how to do this in Survey123. I am able to populate my dropdown based on my feature layer using this in the appearance column:
autocomplete search("name?url=https://services1.arcgis.com/1acZBnEF9zjV0l5X/arcgis/rest/services/Test/FeatureServer/0")

But how do I populate my Foreign Key within the Survey in the related table automatically based on the building selection?

Thanks

1 Solution

Accepted Solutions
DarrylKlassen1
Frequent Contributor

@IsmaelChiviteI accomplished this using a piece of javascript and this in the calculation field:

pulldata("@javascript","get_globalid.js", "findCode", 'https://services1.arcgis.com/1acZBnEF9zjV0l5X/arcgis/rest/services/Building_Data_Test3/FeatureServer/0', string(${NAME}), 'GlobalID')

Thanks for your suggestion!

View solution in original post

7 Replies
IsmaelChivite
Esri Notable Contributor

Have you considered using the Inbox for this?

Through the Inbox, you will be able to display (and filter) all buildings as a list and also as a map. Selecting a building from the list or map will open the survey for that building. Adding a new row into the buildings inspection repeat will automatically add your inspection as a related record.

DarrylKlassen1
Frequent Contributor

Thanks @IsmaelChivite for the quick response.   I have never tried the Inbox before.  I don't really want to have the user select the building spatially, just from a drop down.   Do you have a link to any examples of how this can be done using an existing feature service and related tables? 

DarrylKlassen1
Frequent Contributor

@IsmaelChiviteI accomplished this using a piece of javascript and this in the calculation field:

pulldata("@javascript","get_globalid.js", "findCode", 'https://services1.arcgis.com/1acZBnEF9zjV0l5X/arcgis/rest/services/Building_Data_Test3/FeatureServer/0', string(${NAME}), 'GlobalID')

Thanks for your suggestion!

Olwyn_Bruce
Occasional Contributor

@DarrylKlassen1 , is there any way you could share the get_globalid.js code? I'm trying to do the *exact* same workflow! No worries if it's not possible.

0 Kudos
DarrylKlassen1
Frequent Contributor

Hi @Olwyn_Bruce , I have attached the code here:

/*
 * JavaScript functions for Survey123
 */
function findCode2(layerURL, userinput, field, token) {
	return "OUTVALUE";
}

function findCode(layerURL, userinput, field, token){

    // Output value. Initially set to an empty string (XLSForm null)

    let outValue = "";
    
    // Check to make sure both layerURL and location are provided

    if (layerURL == null || layerURL === "") {

        // The function can't go forward; exit with the empty value

    }


    // Set up query parameters

    let f = "f=pjson";

    let where = "where=NAME="+"'"+userinput+"'";

    let outFields = `outFields=`+field;

    let returnGeometry = "returnGeometry=false";

    let parameters = [f, where, outFields, returnGeometry].join("&");

    if (token) {

        parameters = parameters + `&token=${token}`;

    }

    let url = `${layerURL}/query?${parameters}`;



    // return url;

    // Create the request object

    let xhr = new XMLHttpRequest();

    // Make the request. Note the 3rd parameter, which makes this a synchronous request

    xhr.open("GET", url, false);

    xhr.send();

    let mylen = 0



    // Process the result

    // This an abbreviated version without being able to distinguish different types of errors

    if (xhr.status === 200) {

        let response = JSON.parse(xhr.responseText);

        if (!response.error) {

            if (response.features[0]) {

                outValue = response.features[0].attributes[field];
                mylen = 1

            }

        }

    }

    return outValue;

}

 

ShaibalAhmed
New Contributor

This is pure gold. Thank you so much @DarrylKlassen1

Olwyn_Bruce
Occasional Contributor

Brilliant thank you for saving me the time!! Much appreciated.

0 Kudos