User Story
As a user, when I insert a new Opportunity or update an existing one, I want the Type field to be mandatory so that I cannot leave it empty. This will help ensure that all Opportunities are properly classified and easy to identify. If I try to save an Opportunity without selecting a Type, the system should prevent me from doing so and display an error message prompting me to fill in the required field.
Solution 1
trigger OppTrigger on Opportunity (before insert, before update) {
for(Opportunity opp : Trigger.new){
if(String.isBlank(opp.Type)){
opp.Type.addError('Type must be selected');
}
}
}
Solution 2
trigger OppTrigger on Opportunity (before insert, before update) {
for(Opportunity opp : Trigger.new){
if(opp.Type==null || opp.Type==''){
opp.Type.addError('Type must be selected');
}
}
}