5

I have a layer with a relate table (1-M) with three columns (fk_id, date, comment).

fid fk_id date comment
1 01 2021-02-03 Comment 1
2 02 2021-02-05 Comment 2
3 02 2021-03-02 Comment 3
4 02 2021-03-25 Comment 4
5 03 2021-01-11 Comment 5

I have also created a custom form for the layer and table.

Custom form

What I need is when I add a new record to the related table, the default comment must be the same as the last date for that fk_id.

So, in this example, if I create a new record in the related table using the form, the comment should be Comment 4.

I'm looking around the expression builder but I have no idea how to do this selection.

Anyone could give me a tip of how can I set this default?

3
  • Just to check I got this right: Based on your ID, you want to grab the comment of the newest of the already existing features with the same ID?
    – Erik
    Commented Jun 24, 2021 at 8:41
  • That's right (I guess I didn't explain myself well in the question). Commented Jun 24, 2021 at 8:54
  • It's always tricky to explain familiar issue to others ;-) anyway, to my knowledge it is not possible to automatically base one attribute of a new feature on something you're entering while creating that feature.
    – Erik
    Commented Jun 24, 2021 at 8:59

1 Answer 1

5

To add a new record to the related table, with the default comment the same as the last date for that attribute, please set up some defaults through RMC > Properties > Attribute Form > Fields > date > Defaults

settings

I set map_get(attributes(get_feature_by_id(@layer, maximum("fid"))), 'date') as the 'Default value' as well as ticked the 'Apply default value on update' and clicked Apply.

So, now after adding a new feature it will possess the date of the last feature.

Mind that maximum() function also includes group_by argument.

For the example of the question, a working expression could be:

map_get(attributes(get_feature_by_id(@layer, maximum("fid", group_by:="fk_id"))), 'comment')


References:

1
  • Thanks Taras, I used your expression using group_by for maximum(): map_get(attributes(get_feature_by_id(@layer, maximum(fid, fk_id))), 'comment') Commented Jun 24, 2021 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.