function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DJ 367DJ 367 

Date expressions must use Integer or Long

Hello Everyone I am writting a trigger for validating a record not be deleted before 2 years from RECORD CREATION
DATE. I am getting error like 'Date expressions must use Integer or Long' Can someone please help me to 
complete this code. Thanks in advance.

trigger DelTrigger on BD__c (before Delete) {

        for(BD__c ObjInd : Trigger.Old){
                if (ObjInd.Date__c != null){
                          Integer noOfMonths = ( - ObjInd.Date__c)/365*12;
                    if (noOfMonths <= 24 ){
                            ObjInd.addError('Record can not be deleted');

Alain CabonAlain Cabon

You can use: mydate.monthsBetween(today);  directly
Date mydate = Date.newInstance(2017,1,1);
Date today =;
Integer monthDiff = mydate.monthsBetween(today);
system.debug('month diff:' + monthdiff);

Integer noOfDays = mydate.daysBetween(today);
Decimal noOfMonths = (Decimal)noOfDays/365*12;
system.debug('noOfMonths:' + noOfMonths);

Decimal rounded2decimal = noOfMonths.setScale(2);
system.debug('rounded2decimal:' + rounded2decimal);

  1. A value that indicates a particular day. Unlike Datetime values, Date values contain no information about time.
  2. Date values must always be created with a system static method.
  3. You can add or subtract an Integer value from a Date value, returning a Date value.
  4. Addition and subtraction of Integer values are the only arithmetic functions that work with Date values.
  5. You can’t perform arithmetic functions that include two or more Date values. Instead, use the Date methods.

It sounds like you could simplify this effort by using some DateTime and Date class methods. Consider the following...
// CreatedDate field on any record is a DateTime field
DateTime createdDate =;

// You can use the Date() method on a DateTime instance to obtain the Date component of the value
Date createdDay =;

// the AddYears() method on a Date intance to obtain the date before which your record can be deleted
Date deleteDay = createdDay.addYears(2);

// compare the current time to 
if ( <= deleteDay) {
    // unable to delete the record if less than or equal to the delete day
This can be simplified further by adding years to the CreatedDate directly, then comparing Date.Today() to the result...
DateTime createdDate =;

if (Date.Today() <= createdDate.addYears(2).Date()) {
    // unable to delete record within 2 years of createdDate...
Something else to consider is whether any user should ever be able to delete the record. You could identify a user profile or permission set assignment to allow certain users to bypass this trigger. Or you could include a checkbox field accessible only to admins. Consider the following...
for (CustomObject__c each : trigger.old) {
    if (each.Allow_Delete__c == false || ( <= each.CreatedDate.addYears(2).Date())) {
        each.addError('unable to delete record within 2 years of creation');
I think including an "Allow Delete" checkbox, accessible only to admins could help avoid the future of pain of actually having to delete one of these records before the 2-year limit. This allows a back-door to actually delete the record as needed, without having to go back in and update code or deactivate the trigger later.

Ayush TripathiAyush Tripathi

trigger DelTrigger on BD__c (before delete) {
    for(BD__c  ObjInd : Trigger.Old){
            if (ObjInd.CreatedDate != null){
                Date today =;
                Date mydate =;
                Integer monthDiff = mydate.monthsBetween(today);
                if (monthDiff <= 24 ){
                    ObjInd.addError('Record can not be deleted');