+ Start a Discussion
carmilyn.c martincarmilyn.c martin 

Trigger on Date

I need to create a trigger where (tot.Date_vod__c < (THIS_YEAR(system.today())-1-11))

I want to avoid users from deleting TOT with Date_vod__c that is less than Jan 11 of this year
I am not sure if I am doing it correctly.
Best Answer chosen by carmilyn.c martin
UC InnovationUC Innovation
Hi Carmilyn,

Try using a simple trigger like this.
 
trigger TOTTrigger on TOT (before delete) {
	for (TOT deleteMe : trigger.Old) {
		if (deleteMe.Date_Vod__c < Date.NewInstance(Date.today().year(), 1, 11)) {
			deleteMe.addError('Meaningful Error Message.');
		}
	}
}

The above code basically checks compares the date on the record to Jan 1 of this year. If the record has a date thatless than that then it will result in error.

Hope this helps!

AM

All Answers

UC InnovationUC Innovation
Hi Carmilyn,

Try using a simple trigger like this.
 
trigger TOTTrigger on TOT (before delete) {
	for (TOT deleteMe : trigger.Old) {
		if (deleteMe.Date_Vod__c < Date.NewInstance(Date.today().year(), 1, 11)) {
			deleteMe.addError('Meaningful Error Message.');
		}
	}
}

The above code basically checks compares the date on the record to Jan 1 of this year. If the record has a date thatless than that then it will result in error.

Hope this helps!

AM
This was selected as the best answer
carmilyn.c martincarmilyn.c martin
Works like a charm! Thank you very much!!