Custom field value not saved first time after membership type changed
Overview
Membership type B has a custom field set but Membership type A does not. When a membership of type A is edited and changed to type B, the custom field set appears in the editor and values can be set for the custom fields. However when the membership is saved, the custom field values are not remembered. The values are only remembered if the membership is edited a second time, and the custom fields are set and saved again.
Reproduction steps
Go to https://dmaster.demo.civicrm.org/
Add new custom field set used for Memberships -> General
Add new field to field set: a dropdown field with option 1 and option 2
Find any membership of type Student and edit it
Change membership type from Student to General, the new custom field appears for editing
Select option 1 in the custom field and save the membership
View the membership - the custom field value is not set
Edit the membership again, again select option 1 in the custom field and save the membership
View the membership again - the custom field value is now set
Current behaviour
The first time a custom field value is set after a membership type is changed, the value is not remembered. It is only remembered after the second time it is saved.
Expected behaviour
The custom field value should be remembered the first time it is saved.
Environment information
Reproduced on https://dmaster.demo.civicrm.org/ 9th Dec 2022