QGIS - QField - Retrieve target field value when source feature intersects target feature in target layer
refFunctions won't work in QField as it is a QGIS desktop plugin (and is the downside of the many answers that rely on a plugin for expression-based solutions) - QField won't recognise the functions.
With your aggregate expression, consider using expression:=to_string("polygonID")
if the polygonID
field is numeric, as concatenate only works on string values.
If your expression isn't working because the underlying field name has changed, you have two options:
Option 1: Use Layer ID
You can obtain the layer ID in the Expression window under Map Layers; double-click the layer you want.
Although the layer ID contains the original filename in front the reference stays the same even if you change the layer name. The example below shows two aggregate expression inside the point labels, one using the layer ID for the line layer, the other using the layer name (initially named tr_road
).
When I change the layer name the expression using the layer ID still evaluates, but the expression using the layer name fails completely (which was probably what happened to you)
Option 2: Test for valid layer name
The downside to layer IDs is that they are unique each layer in a project. If your aggregate expression is designed to always pick up a specifically named layer but you expect it to refer to any potential layer with that name (including a layer you might add later, or another layer renamed accordingly), it won't work.
In this instance you will need to use layer_property()
to test whether a layer exists - if it doesn't it simply returns a NULL
and afaik is the only way to "except" the termination error that aggregate()
returns when an invalid layer name is provided - which as you've experienced, is not very helpful....
For the specific QField use case where the layer name change is predictable and your aggregate()
expression only needs to look at one of two options, try the following expression to use either layer name
or layer name (offline)
aggregate(
layer:= coalesce(layer_property('polygonLayer','name'),
layer_property('polygonLayer (offline)','name')),
aggregate:='concatenate',
expression:=polygonID,
concatenator:=', ',
filter:=intersects($geometry, geometry(@parent))
)
For more complex cases where you want it to pick the first of a potentially long list of layer names, consider using with_variable()
and array functions instead, so you can more easily edit your list at the top of your expression and add/remove items easily. I have put that expression in the relevant GitHub discussion thread here.
Great news, the last release of QFieldSync
preserve the layer name without (offline) suffix. I made a quick test with aggregate
function and coalesce
isn't needed anymore, values are well calculated with the original layer name.