+ Start a Discussion
RajevlsRajevls 

Compile:Error On triger

if(T.Completion_Date__c != NULL){
                   T.No_Of_Days_Open__c = T.Completion_Date__c - DATEVALUE(CreatedDate);
                }
Error :

 Error: Compile Error: Variable does not exist: CreatedDate at line 38 column 76
 if(T.Completion_Date__c != NULL){
                   T.No_Of_Days_Open__c = T.Completion_Date__c - DATEVALUE(T.CreatedDate);
                }
Error :

 Error: Compile Error: Method does not exist or incorrect signature: DATEVALUE(Datetime) at line 38 column 66

 

 

The Code :

 

trigger UpdateTypePicklist on Task(before update) {
            
            String Curr = Userinfo.getDefaultCurrency();
            
            for(Task T: Trigger.new){
              
            //Create an old and new map so that we can compare values  
              Task oldT = Trigger.oldMap.get(T.ID);    
              Task newT = Trigger.newMap.get(T.ID);
              
             //Retrieve the old and new Status Value   
              String OldStat = oldT.Status;
              String NewStat = newT.Status;
if((T.Type__c != NULL)&& (T.RecordTypeId == '012S0000000DIPs'))
                 {                  
                  T.Type = T.Type__c;
                 }
                
                if(OldStat!= NewStat){
                      
               T.Status_Update_Date_Time__c = DateTime.now();
               
                 if(T.Status == 'Completed'){
                   T.Completion_Date__c = Date.Today();
               
               }
               
               if(T.Completion_Date__c != NULL){
                   T.No_Of_Days_Open__c = T.Completion_Date__c - DATEVALUE(T.CreatedDate);
                }
               
               }
               
               
               If((T.CurrencyIsoCode!= Curr)&&((T.Type__c!='Estimate')&&(T.Type__c!='Quote'))){
                   T.adderror('Activity Currency Should not be updated');
                 
               }
                
   }
}


Best Answer chosen by Admin (Salesforce Developers) 
jungleeejungleee

Hi

 

try using the below code:

 

T.No_Of_Days_Open__c = T.Completion_Date__c.daysBetween(T.createdDate.date());

 Hope this helps!

 

Regards

Sam

All Answers

bob_buzzardbob_buzzard

DATEVALUE is a formula function, so you can't use that in apex.  Instead you should use the date method exposed by the datetime primitive:

 

 T.No_Of_Days_Open__c = T.Completion_Date__c - CreatedDate.date();

 

RajevlsRajevls
Edit 
 
ErrorError: Compile Error: Method does not exist or incorrect signature: CreatedDate.date() at line 39 column 66
jungleeejungleee

Hi

 

try using the below code:

 

T.No_Of_Days_Open__c = T.Completion_Date__c.daysBetween(T.createdDate.date());

 Hope this helps!

 

Regards

Sam

This was selected as the best answer
bob_buzzardbob_buzzard

It should be 'T.createdDate.date()' in my code sample above.