Select to view content in your preferred language

Using Schema Function in Attribute Rule

515
3
Jump to solution
10-16-2023 10:39 AM
Ariggs
by
Occasional Contributor

Hi All,

I'm trying to add a field to the Change Log table I've created that tracks edits to our data. The new field would record the changes made to the field values of a feature and would be in dictionary format (field output would be "x field: y new field value, z field: w new field value...). I only want to record fields that have had edits made to them. Here's the code I have so far-- I'm trying to create an empty dictionary, use the Schema tool to get the field names and values, compare them, and push values to the new dictionary that are different from the original values. 

var DeviceFeatureEdits = FeatureSetByName($datastore, 'ElectricDevice',['*'], true)

var editscalc = Dictionary()

//schema function returns an array of dictionaries, includes field names
var Cschema = Schema(DeviceFeatureEdits))        //Cschema is the post edit dictionary of field values
var Oschema = Schema(DeviceFeatureEdits.$originalfeature))        //Oschema is the pre-edit dictionary of field values

if (Oschema.assetid != Cschema.assetid){
push (editscalc, Cschema.assetid)
}

//I repeat the above If statement for all the fields. 

 

The lines where I use the Schema function keep causing either "semicolon or new line expected" or "reserved keyword used". Is the schema function not applicable to attribute rules, or am I using it wrong? Is this a viable way to do what I'm trying to do?

 

Thank you!

0 Kudos
1 Solution

Accepted Solutions
Ariggs
by
Occasional Contributor
var editscalc = Dictionary()
var fieldarray = Schema($feature).fields

for (var i in fieldarray){
    var z = fieldarray[i].name    
    if ($feature[z] != $originalfeature[z]){
        editscalc[z] = text(DomainName($feature, z))
        }
    }
 
Update with success, I was able to return the Field name and the new field value to one field to my Change Log table using the above code. The new field value is "editscalc" converted to text. I only wanted to use Schema to isolate the field names, not the values, without hard coding them.

View solution in original post

0 Kudos
3 Replies
Ariggs
by
Occasional Contributor

Wanted to add that trying this:

var Cschema = Schema($feature)
var Oschema = Schema($originalfeature)

if (Oschema.assetid != Cschema.assetid){
push (editscalc, Cschema.assetid)
}

 

yields the error message "Field not found assetid"

0 Kudos
PierreloupDucroix
Frequent Contributor

Hello,

I don't think you can get field values with the Schema function. According to the documentation, this is the result :

fields: Array<Dictionary> - Returns an array of dictionaries describing the fields in the Feature. Each dictionary describes the field name, alias, type, subtype, domain, length, and whether it is editable and nullable.

Field values are not part of the result.

Also in the first line, DeviceFeatureEdits is a featureSet, but Schema function takes a Feature as input, not a featureSet.

To achieve what you want, maybe you can create a function that directly compare $feature fields and values with $originalfeature fields and values and returns a dictionary of edits. I would be interested if you succeed in.

0 Kudos
Ariggs
by
Occasional Contributor
var editscalc = Dictionary()
var fieldarray = Schema($feature).fields

for (var i in fieldarray){
    var z = fieldarray[i].name    
    if ($feature[z] != $originalfeature[z]){
        editscalc[z] = text(DomainName($feature, z))
        }
    }
 
Update with success, I was able to return the Field name and the new field value to one field to my Change Log table using the above code. The new field value is "editscalc" converted to text. I only wanted to use Schema to isolate the field names, not the values, without hard coding them.
0 Kudos