カスタム オブジェクトに、カスタム選択リスト フィールド Food _Preference_ c が存在します。選択リストには、「Vegan」、「Kosher」、「No Preference」のオプションが含まれています。開発者は、レコードが作成または更新されるたびに値が入力されるようにする必要があります。 レコードが保存されるたびに値が選択されるようにする最適な方法は何ですか?
正解:C
To ensure that the Food_Preference__c picklist field has a value every time a record is created or updated, marking the field as Required on the field definition is the optimal solution. Option C: Mark the field as Required on the field definition Universal Enforcement: Setting the field as required at the field level ensures that it must have a value regardless of how the record is created or updated (UI, API, data import). Data Integrity: Prevents records from being saved without a value in the picklist field, maintaining data consistency. No Additional Code: Achieves the requirement declaratively without the need for Apex code. Reference: Making a Custom Field Required Why Other Options are Less Optimal or Incorrect: Option A: Mark the field as Required on the object's page layout UI Only: This setting only enforces the requirement when users interact with the record via the specific page layout. Not Universal: Does not prevent records from being saved without a value through the API, data import, or other page layouts. Option B: Set "Use the first value in the list as the default value" to True Default Value: Automatically populates the field on new records but does not prevent users from clearing the value. No Enforcement on Updates: Does not enforce a value on record updates if the field is cleared. Option D: Write an Apex trigger to ensure a value is selected Unnecessary Complexity: Writing code for a requirement that can be met declaratively adds unnecessary complexity. Maintenance Overhead: Triggers require testing and ongoing maintenance. Best Practices: Prefer declarative solutions over programmatic ones when possible. Conclusion: Marking the Food_Preference__c field as Required on the field definition ensures that a value is always selected when a record is saved, across all contexts. It is the most efficient and maintainable solution.