Fix setting custom field data if custom field object is missing #730
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related Issue:
New Behavior
This PR fixes a bug in the set_custom_fields_values() function. After applying these changes custom fields for a given object are set only if the related CustomField object is already defined and if that CustomField object is enabled with the ContentType of the given object.
If the CustomField is present but not enabled for requested ContentType then nothing is set on the object and a helpful log is printed. On subsequent run, after corrections are made the startup script will fill back the missing custom fields for an (now) existing object.
However, the custom field data is set only if the given dictionary key is absent in the object.
Contrast to Current Behavior
Currently there is no check if the CustomField requested as some object's custom_field_data exists or not, neither there is a check if that CustomField object is enabled for the ContentType in question.
Because of that, the custom fields are being saved to the database for a given object regardless of the above conditions. If CustomerField is not defined or it doesn't have the relevant ContentType enabled an error will be thrown at the first try to edit the object in question (e.g., a Device).
Discussion: Benefits and Drawbacks
This fix is backwards compatible. The only drawback is that the set_custom_fields_values() function must be run regardless whether the object in question was created by the custom script or not. Given, that typically there shouldn't be that many custom fields to process this can omitted.
Changes to the Wiki
n/a
Proposed Release Note Entry
Bugfix: prevent saving custom fields data when related CustomField is not defined or without requested ContentType
Double Check
develop
branch.