Fix NPE when persisting externalId