+ Start a Discussion
CoderCoder 

Trigger help

Hi,

 

I have 3 fileds.start date(text),end date(text) and student status[picklist(values are trial,active,paid)].here my condition is if the end date is greater than 3days of start date and if the student status is "trial" then i want to delete those records.

 

In my code it is not checking the dates,it deletes entire trial records.Plz tell me modifications on my code.

Here is my code..

 

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__startdate__c,chiranjeevi__enddate__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;
       }  
   
 }
}

 

 

plz tell me how to check the date fileds.

 

Thanks in advance,

Manu..

Best Answer chosen by Admin (Salesforce Developers) 
gtindugtindu

Ideally you would change the field types from Text to Date (or DateTime)


You can, alternatively, use date.parse(field) to turn a text representation into a date/time entity.

 

 

if (date.parse(a.chiranjeevi__enddate__c) > date.parse(a.chiranjeevi__startdate__c).addDays(3)) {
   delete objdlist;
}

 

Although you have to be careful because improperly formatted dates will yield errors.


 

All Answers

gtindugtindu

Ideally you would change the field types from Text to Date (or DateTime)


You can, alternatively, use date.parse(field) to turn a text representation into a date/time entity.

 

 

if (date.parse(a.chiranjeevi__enddate__c) > date.parse(a.chiranjeevi__startdate__c).addDays(3)) {
   delete objdlist;
}

 

Although you have to be careful because improperly formatted dates will yield errors.


 

This was selected as the best answer
CoderCoder

 

Thanks for ur reply,

 

But it is showing the error in this line

if (date.parse(a.chiranjeevi__enddate__c) > date.parse(a.chiranjeevi__startdate__c).addDays(3)​)

 

as

Error: Compile Error: line 10:104 no viable alternative at character '​' at line 10 column 104

gtindugtindu

Like i said - you should ideally change those fields to Date fields instead of Text fields.  Otherwise and empty or improperly formatted item will cause you trouble.