Fix #1818 by substituting a temporary variable and assigning the field to it after successful completion of the loop, as we do for non-shared attributes.
Issue #1818: Add a test