Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
DJ 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(Trigger.isDelete){
if (ObjInd.Date__c != null){
Integer noOfMonths = (Date.today() - ObjInd.Date__c)/365*12;
if (noOfMonths <= 24 ){
}
}
}
}
}

Regards,
Dj
Alain Cabon
Hi,

You can use: mydate.monthsBetween(today);  directly
```Date mydate = Date.newInstance(2017,1,1);
Date today = 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);```

Date:
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.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_primitives.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

Regards
pigginsb
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 = system.now();

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

// the AddYears() method on a Date intance to obtain the date before which your record can be deleted

// compare the current time to
if (Date.today() <= 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 = system.now();

// 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 || (Date.today() <= 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 Tripathi

trigger DelTrigger on BD__c (before delete) {
for(BD__c  ObjInd : Trigger.Old){
if(Trigger.isDelete){
if (ObjInd.CreatedDate != null){
Date today = Date.today();
Date mydate = ObjInd.CreatedDate.date();
Integer monthDiff = mydate.monthsBetween(today);
if (monthDiff <= 24 ){