I have one set of forms where my attachment (photos) re-naming naming expression works fine and another where a similar expression fails and the photos are named with a default fallback.
The below works well with the Type and Number variables used from the form
'DCIM/' || "Type" || '-' || "Number" || '-' || 'Photo-1_' || format_date(now(),'yyyyMMddhhmmsszzz') || '.jpg'
In another project a very similar expression doesn’t work unless I stop using the reference to variables - I can’t figure out why one works and the other not - I have checked the expression generates a successful result.
This is the one that doesn’t work
'DCIM/' || "TreeNumber" || '-' || 'Photo-1_' || format_date(now(),'yyyyMMddhhmmsszzz') || '.jpg'
Imported from GitHub discussion by @BTreeTech on 2024-05-01T08:44:21Z
Hi,
It looks like your second expression may be considered as a comment, since you wrote – without the ‘–’.
I would write it this way :
concat('DCIM/--',"TreeNumber",'-Photo-1_',format_date(now(),'yyyyMMddhhmmsszzz'),'.jpg')
Bye
Imported from GitHub comment by @Ipickedausername on 2024-05-01T17:19:36Z
Sorry I thought I had made it clear that the offending section was commented out to make the expression work.
My understanding is that using concat is a good suggestion as it handles null values so if “TreeNumber” is null the rest of the expression might still work.
Imported from GitHub comment by @BTreeTech on 2024-05-02T07:01:52Z
This really depends on the field data types and the allowable values for them. It’s hard to troubleshoot what the issue is and indeed whether it’s a QField issue in the first place, without more details (e.g., what widget type is used for TreeNumber vs. Type and Number? Do any other fields feed into this value; is there a default value expression? what sample values have you tried in QGIS vs QField?)
Imported from GitHub comment by @she-weeds on 2024-05-04T11:04:13Z
BTreeTech
I think your second expression is not working because it is a comment since you use ‘–’ so early on in the expression. If you copy it into the field calculator, everything after the ‘–’ will be greyed out, indicating that it is a comment. Also, I don’t know if commenting out a singular field/ variable is possible.
But in your initial post, you say you reference the variable “TreeNumber”. Am I correct to assume that it is located in another form, e.g. parent feature, and you want to use it in a naming expression for a child feature? I had a project that used referenced variables as well, and the naming expression never worked for me unless I saved the parent feature, which contained the referenced variable, before adding the child feature (i.e. photo).
Imported from GitHub comment by @Meykaefer on 2024-05-06T14:23:31Z
In the project where the naming expression works the referenced field “Number” is an integer set using the range widget with a default value expression. In the project where the naming expression doesn’t work the widget is a text edit with a more complicated expression for the default value that checks other layers (Trees, hedges and woodlands) to avoid duplicate IDs. In both cases the variable is being used within the same form but on a different tab. If I check the expression with existing features it works fine. I’ll try with the “TreeNumber” field setup using a range widget.
Imported from GitHub comment by @BTreeTech on 2024-05-07T07:30:38Z
Re - commenting it works fine if you split the expression across multiple lines. I have removed the commenting from the initial post as it seems to be creating more interested than the main issue.
This style is probably against all kinds of conventions but it works for me.
“TreeNumber” is in the same form but on a different tab in both projects
Imported from GitHub comment by @BTreeTech on 2024-05-07T07:37:45Z
BTreeTech, I have a question regarding the expression you use for the tree number. How does that expression function? Do you trigger it, or does it trigger automatically?
Imported from GitHub comment by @Meykaefer on 2024-05-07T16:49:52Z
Meykaefer “TreeNumber” is on a different tab of the same form, as above, it has an expression for the default value.
Imported from GitHub comment by @BTreeTech on 2024-05-08T06:55:23Z
In this instance using a variable in the expression for the attachment name works if the variable is set using the range widget rather than the text edit widget.
Imported from GitHub comment by @BTreeTech on 2024-05-14T11:24:27Z