Search Here

Scenario 3: A sales team wants to ensure that no Opportunity is created with a closing date in the past. How would you enforce this with a trigger?

You would use a ‘before insert’ and ‘before update’ trigger on the Opportunity object. The trigger would check the ‘CloseDate’ of each Opportunity record, and if the date is in the past, it would add an error message to the record.

trigger ValidateOpportunityCloseDate on Opportunity (before insert, before update) {

for(Opportunity opp : Trigger.new){

if(opp.CloseDate < Date.today()) {

opp.addError(‘Close Date cannot be in the past.’);

}

}

}

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.