Need Help with Arcade Script for Insert Trigger in Survey123 and ArcGIS Enterprise

569
9
Jump to solution
11-09-2023 06:21 AM
JuneAcosta
Occasional Contributor III

Hello ArcGIS Community,

I am currently working on an Arcade script for an Insert trigger, and I'm facing some challenges. Your expertise and guidance would be immensely valuable to help me troubleshoot and refine my script. 

I look forward to your responses and appreciate your time and support.

Thank you!

Objective:

I am developing a Survey123 app that points to an enterprise layer named `BCCpointclass` within ArcGIS Enterprise, not a hosted layer in AGOL. Each time a new point is created in the survey, I want to use an attribute rule on BCCpointclass to create a duplicate of a newly created point and all its attributes in another enterprise point feature class named `BCC_JuneSnapShot`.

Issue:

From the script below in the expression builder I get the error. "Invalid expression. Error on line 20. Identifier expected.

I put a script together from the example in this 2019.

This is my script:

// Get the global ID of the point feature being inserted
var globalId = $feature.globalid;

// Find the corresponding feature in the BCC_JuneSnapShot feature class
var snapshotFeatureClass = FeatureSetByName($datastore, "BCC_JuneSnapShot");
var filteredFeatures = FeatureSet(
Filter(snapshotFeatureClass, "pointGuid = @globalId")
);

// If no matching feature is found, create a new feature
var newFeature;

if (Count(filteredFeatures) == 0) {
// Create a new feature in BCC_JuneSnapShot
newFeature = snapshotFeatureClass.createFeature();

// Set the geometry and attributes from BCCpointclass to BCC_JuneSnapShot
Set(newFeature, 'geometry', $feature.geometry);
for (var field in $feature) {
if (field !== 'geometry') {
newFeature[field] = $feature[field];
}
}

// Save the new feature in BCC_JuneSnapShot
snapshotFeatureClass.addFeature(newFeature);
} else {
// If a matching feature exists, exit without making changes
return $feature.field;
}

// Return the value of the field 'Field' without change
return $feature.field;

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MikeMillerGIS
Esri Frequent Contributor

I am on 3.2.  Looks like Dictionary on Feature was not added till 3.2.  Change the line to 

var copy_feature = dictionary(Text($feature));

 

Dictionary(inputFeature) -> Dictionary

Since version 1.23

View solution in original post

9 Replies
MikeMillerGIS
Esri Frequent Contributor

When posting code, please use the code snippet.

 

This should get you closer:

// Get the global ID of the point feature being inserted
var globalId = $feature.globalid;

// Find the corresponding feature in the BCC_JuneSnapShot feature class
var snapshotFeatureClass = FeatureSetByName($datastore, "BCC_JuneSnapShot");
var filteredFeatures = Filter(snapshotFeatureClass, "pointGuid = @globalId");

if (Count(filteredFeatures) != 0) {
    return $feature.field;
}

var copy_feature = dictionary($feature);
copy_feature['geometry'] = $feature.geometry
return {
      "result": $feature.field,
       "edit": [  
           {  
               "className" : "BCC_JuneSnapShot",
               "adds" : [copy_feature ]
            }
     ]
}
0 Kudos
JuneAcosta
Occasional Contributor III

Thanks for responding. I get the error Invalid expression. Error on Line 12 . Invalid JSON. I tried to troubleshoot and change the script a bit but still get the same error.

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Can you share a fgdb with your source and target classes?  Would be easier to troubleshoot.

Here is a good sample to follow too - https://www.esri.com/arcgis-blog/products/arcgis-pro/data-management/advanced-gdb-attribute-rules-ed...

0 Kudos
JuneAcosta
Occasional Contributor III
// Get the global ID of the point feature being inserted
var globalId = $feature.globalid;

// Find the corresponding feature in the BCC_JuneSnapShot feature class
var snapshotFeatureClass = FeatureSetByName($datastore, gisdata.sdeowner.JuneBackup");
var filteredFeatures = Filter(snapshotFeatureClass, "BCCGuid = @globalId");

if (Count(filteredFeatures) != 0) {
    return $feature.field;
}

var copy_feature = dictionary($feature);
copy_feature['geometry'] = $feature.geometry
return {
      "result": $feature.field,
       "edit": [  
           {  
               "className" : "JuneBackup",
               "adds" : [copy_feature ]
            }
     ]
}

I'm working against an enterprise geodatabase- above shows how I reference the data in the script. I have also attached the schema for my layers.

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

This works in the gdb you sent.  I added a function that will copy the attributes from the input feature, just remove the fields you want to persist.  and then set line 25 to use the filtered_atts variable 

 

// Get the global ID of the point feature being inserted
var globalId = $feature.globalid;

// Find the corresponding feature in the BCC_JuneSnapShot feature class
var snapshotFeatureClass = FeatureSetByName($datastore, "JuneBackup");
var filteredFeatures = Filter(snapshotFeatureClass, "BCCGuid = @globalId");

if (Count(filteredFeatures) != 0) {
    return;
}

function pop_keys(dict, keys) {
    var new_dict = {};
    for (var k in dict) {
        if (IndexOf(keys, Upper(k)) != -1) {
            continue;
        }
        new_dict[k] = dict[k];
    }
    return new_dict;
}
var copy_feature = dictionary($feature);
var fields_to_not_copy = ['OBJECTID', 'Shape', 'CreationDate', 'Creator', 'EditDate', 'Editor', 'BCCCHOICES', 'DOB', 'HOMEADDRESS', 'EMAIL', 'EMPLOYER', 'EMPLOYERADDRESS', 'EXPLANATIONS', 'EDUCATIONBACKGROUND', 'RELEVANTEXPERIENCE', 'ADDITIONALINFOCOMMENTS', 'EMPLOYERADDRESSCITY', 'EMPLOYERADDRESSZIP', 'SUBMITDATE', 'ISCARLSBADRESIDENT', 'ISCARLSBADREGISTEREDVOTER', 'ISWILLINGDISCLOSEFINANCIAL', 'ISWILLINGETHICSTRAINING', 'FIRSTNAME', 'LASTNAME', 'HOMEADDRESSCITY', 'HOMEADDRESSZIP', 'COUNCILDISTRICT', 'WORKMOBILEPHONE', 'ACKCITYEMPLOYEE', 'ACKCITYCONTRACTOR', 'ACKCITIZENACADEMYGRAD', 'ACKRESPONSIBILITIES', 'ACKINTERVIEWWILLING', 'APPOINTEDBCC', 'STATUS', 'APPOINTEDDATE', 'TERMEXPIRATIONDATE', 'STAFFINITIALS', 'EMPLOYMENTSTATUS', 'SUBMITTERACKNOWLEDGETYPE', 'SUBMITTERACKNOWLEDGECHECK', 'INTEREST', 'APEXYEAR', 'ACKCITYEMPLOYEEPAST', 'PRIMARYPHONE', 'SECONDARYPHONE', 'WORKPHONE', 'COMMUNITYINVOLVEMENT', 'SERVICEONBCC', 'SERVICEONBCC2', 'SERVICEONBCC3', 'SERVICEONBCC4', 'SERVICEONBCC5', 'APPOINTEDPOSITION', 'APPOINTEDCATEGORY', 'NOMINATINGCOUNCILMEMBER', 'ApplicantID', 'created_user', 'created_date', 'last_edited_user', 'last_edited_date', 'GlobalID']
var filtered_atts = pop_keys(copy_feature['attributes'],fields_to_not_copy)
copy_feature['attributes'] = {}
copy_feature['attributes']['BCCGuid'] = $feature.GlobalID;
copy_feature['geometry'] = Geometry($feature)
return {
       "edit": [  
           {  
               "className" : "JuneBackup",
               "adds" : [copy_feature]
            }
     ]
}

cop 

0 Kudos
JuneAcosta
Occasional Contributor III

I'm still encountering the same issue with the script. The error message "invalid JSON on line 22" keeps appearing, and it seems to be linked to the line var copy_feature = dictionary($feature), just like in the previous scripts.

The problem persists when I test the script with both egdb and fgdb. I'm using ArcGIS Pro version 3.1.2. Could this issue be related to my version of ArcGIS Pro or Python? Which version of ArcGIS Pro are you using?

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

I am on 3.2.  Looks like Dictionary on Feature was not added till 3.2.  Change the line to 

var copy_feature = dictionary(Text($feature));

 

Dictionary(inputFeature) -> Dictionary

Since version 1.23

JuneAcosta
Occasional Contributor III

Now it validates! thanks for your help on this.

0 Kudos
VanessaSimps
Occasional Contributor III

I am trying to do something very similar, but using Attribute Rules. I tested this out, but am getting a few errors (not sure if everything translates between where you were using this and where I want to use it in ArcPro)? Thanks in advance

 

0 Kudos