Select to view content in your preferred language

Attribute rule for Inserting record to table

992
5
03-05-2023 12:09 PM
GIServicesUser
New Contributor II

I am wondering if I could create an update attribute rule to a table A  that copies a value (V) from a field in a table B (by using the $feature.globalid to find the record in table B) and inserting a new record into a table C to paste the V. So far, I managed to transfer the V from the $feature in table A to a new record in table C.

code:

return {
"edit": [{
    "className": "table C",
    "adds": [{ 
       'attributes': 
       {
         "gid_fk":$feature.globalid
        }
    }]
}]
}

Thanks in advance

0 Kudos
5 Replies
JohannesLindner
MVP Frequent Contributor

You could use the Filter() function.

 

var table_b = FeaturesetByName($datastore, "TableB")
var gid = $feature.GLobalID
var related_b_row = First(Filter("gid_fk = @gid")) // use the actual field name!
var v = IIf(related_b_row == null, null, related_b_row.V)

return {
"edit": [{
    "className": "table C",
    "adds": [{ 
       'attributes': 
       {
         "gid_fk":$feature.globalid,
         "v": v
        }
    }]
}]
}

Have a great day!
Johannes
0 Kudos
GIServicesUser
New Contributor II

Thanks Johannes, I tried this and works perfectly, however, another issue came up...as always, the function Filter if I am not mistaken is incompatible with my ArcGIS Server Installation 10.8.1 according to this and I get an error while trying to publish the feature (table A) with the attribute rule...

0 Kudos
JohannesLindner
MVP Frequent Contributor

Hmm, according to the version matrix, ArcGIS Server 10.8.1 should include Arcade 1.10 at least. Filter() was released in 1.5. What error do you get?

 

Anyway, you could try FeaturesetByRelationshipName(), but that was released in Arcade 1.8, so it will probably be the same.

You could also do it with a for loop. It will be slower than Filter(), especially if table B has many entries, but it should get the job done:

var table_b = FeaturesetByName($datastore, "TableB")
var gid = $feature.GLobalID
var v = null
for(var b in table_b) {
    if(b.gid_fk == gid) {
        v = b.v
        break
    }
}

return {
"edit": [{
    "className": "table C",
    "adds": [{ 
       'attributes': 
       {
         "gid_fk":$feature.globalid,
         "v": v
        }
    }]
}]
}

 


Have a great day!
Johannes
0 Kudos
GIServicesUser
New Contributor II

Hi Johannes, 

I used the loop method to write the script and even though the rule works fine on desktop, when trying to publish the table with the attribute rule I get the same error as before 

ERROR 001487: Failed to update the published service with the server-side data location

If I remove the attribute rule from the table A, the publishing of the table completes successfully.

I am not sure If the fact that the table B is a table view in the database and it is not included in the publishing service because the service has feature capability, has something to do with this error...

0 Kudos
JohannesLindner
MVP Frequent Contributor

I think table b has to be included for the $datastore to work. You don't need to consume the published table b in your map/application, but it should be in the service.


Have a great day!
Johannes
0 Kudos