+ Start a Discussion

trigger on date field

Hi All,


I am new to triggers.I have 2 date fileds.

1.First_Logged_On_ Date__c,


Here i want to check check if the second date filed is greater than 3 days with compare to first date filed then i want to delete the record,


Plz tell me..

Thanks in advance,



It sounds like you just need a Validation Rule on your object that would prevent it from saving the record in the first place, rather than save the record then delete it. This wouldn't require any Apex code, either.




thanks for ur reply,


Actulally i am doing modifications on existing project.it is online examination project for students.student can sign in using flex front end page.first "student staus" is trial after three days if student pay the money then student staus is active.If he wont pay money after three days i want to delete the record of particular student.In backend salseforce can check student status and counts the days from start date to end date  .

my requirement is if end date is gretater than 3days of start date and if the student status is trial then i want to delete that particular record automatically.

Plz tell give me example code how to solve this one.


Thanks in advance,



if (endDate > startDate.addDays(3) {




Hi Scoobie,


Thank you very much for ur reply,


In my project "Student__c" is the Object.


In this object fileds are

1.Course_Started_On__c  --->Text field

2.Course_Ends_On__c     --->Text field

3.Student_Status__c          ---->PickList field(values are Trial,Pending,Active,Paid)


Here i want to delete the record if the student is not paying money with in 3 days and if the Student_Status__c=='Trial'.

Here Some query is also required.

Can u please.... give me the entire  trigger code.Bcoz i am new to triggers.plz....

Thanks in advance,



 Hi Scoobi,


I wrote one trigger with ur instruction.But here i am facing  problem.

Mycode is:



trigger expiry on chiranjeevi__Obj_D__c (before insert,before update) {
List<chiranjeevi__Obj_D__c> objdlist=new List<chiranjeevi__Obj_D__c>();
for(chiranjeevi__Obj_D__c a : Trigger.new)
  objdlist=[SELECT chiranjeevi__enddate__c,chiranjeevi__startdate__c,chiranjeevi__student_status__c FROM chiranjeevi__Obj_D__c WHERE chiranjeevi__student_status__c = 'trial' ];
  if(a.chiranjeevi__enddate__c > a.chiranjeevi__startdate__c.addDays(3) )
    delete objdlist;


In this code it is deleting the records who has status 'trial'.But it is not checking the enddate is greater than startdate.addDays(3).So all records are deleted who are having the status is 'trial'.I need to satisfy the date condition also.

Plz tell me where i can change the logic.


Thanks in advance,