You need to sign in to do that
Don't have an account?
Sai.Maharajpet
trigger to validate the age by using date of birth and add error message if age < 18 please help me in this thanks
trigger DateofBirth on Account (before insert) {
for(Account a:trigger.new){
if(a.Date_of_Birth__c!=NUll && ((system.today()- a.Date_of_Birth__c)/365 <=18)){
a.Date_of_Birth__c.adderror('Age should be more than 18');
}
}
}
error i am facing:
Date expressions must use Integer or Long
Use daysBetween() method of date class to find number of days between today and date of birth instead of subtraction.
Try this code
Please refer below link which might help you in this
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm
Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.
Thanks and Regards
All Answers
Use daysBetween() method of date class to find number of days between today and date of birth instead of subtraction.
Try this code
Please refer below link which might help you in this
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm
Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.
Thanks and Regards
for(Account ac:trigger.new){
Date a = Date.today();
Date startDate = Date.valueOf(ac.birthdate__c);
Integer numberDaysDue = integer.valueOf(startDate.daysBetween(a));
Integer age = numberDaysDue/365;
if(age <= 18){
system.debug('Test ::' + age);
ac.addError('Age should be more than 18');
}
}
}