I don't think there is a possibility to check if a certain field exists. However, if a field doesn't exist, the its value will be reported as
Now there is one remaining problem, that the
prepare() which QGIS performs will check if all your fields are there (which is normally a desired behavior).
You can workaround this for certain parts of your code by wrapping them in an
eval statement. Please note that this exploits undocumented behavior and may change anytime in the future!
The following expression will return
TRUE if the field does not exist
eval('"field1"') IS NULL
Often you don't even have to check because
eval('"field1"') * 3
will itself return either
NULL or 3 times the value of
field1 because the nature of
NULL values is that they get propagated.
If you want to replace a non-existent value with some other value you can use the
Basic usage, replace field1 with 0 when unavailable
COALESCE( eval("field1"), 0 )
And a more complex example of how to put this together in an expression
COALESCE( eval("scale_factor"), 1 ) * ( COALESCE( eval("field1"), 0 ) + "field_which_is_always_available" )
This will replace
scale_factor with 1 if unavailable,
field1 with 0 if unavailable and the last one will always have to be there or your whole expression will result in an empty (NULL) value.
Please note: If you have NULL values inside your data, the result will obviously be the same as if the field does not exist. This may be in line with your requirements but it's something to keep in mind.