+ Start a Discussion
learn sfdc 24learn sfdc 24 

How to insert system.today(),createddate in testclass

school__c -->customObject
studentjoinage =system.today() - createddate; //its formula field which riturn number.
I written the batchclass,in that   if(sch.studentjoinage__c==30) then send an email.
   mybatchcls mb=new mybatchcls();
i get 40% codecoverage, in that start method ,finish methods r coverd but in execute method after if condition which means
messaging.singleemailmessage not coverd bcoze formula field.
i need help here to cover formula field.   
Anjum Attar 26Anjum Attar 26
For accessing formula field you need to insert or update the record and after that query on object and get the value of formula field for updated or inserted record. for example check the following ex.In below code count__c is a formula field
Account accInstance = new Account();
accInstance.Name = 'Test';
accInstance.NumberOfEmployees = 10;
insert accInstance;
accInstance = [SELECT Id, Quant__c FROM Account Where Name = 'Test'];
System.debug('>>>accInstance :' + accInstance);
Manually you cannot put value in formula field.
Santhosh SSanthosh S
Hi try to set the record created date to past date try something like this


system.today() - createddate.

Refer :
Anjum Attar 26Anjum Attar 26
createddate is a nonwriteable field we cannot update that so query on some old records and check there studentjoinage field value and it will work
Anjum Attar 26Anjum Attar 26
Thank you Santosh S, the links you have shared are very helpful.
Test.setCreatedDate(a.Id, DateTime.newInstance(2012,12,12));
Santhosh SSanthosh S
if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

This will help keep the forum future users determine what answers are useful
Shiva RajendranShiva Rajendran
Hi Learn sfdc 24,

Spring '16 release notes (http://docs.releasenotes.salesforce.com/en-us/spring16/release-notes/rn_apex_new_classes_methods.htm#rn_apex_new_classes_methods_changed_classes) document a new system method supporting this:
Steps :
Just create a new DateTime object
use Test.setCreatedDate to set the dateTime for the createdDate of that object

Use the below sample code :
Account account = new Account(Name = 'Test'); insert account; 
Datetime yesterday = Datetime.now().addDays(-1);
Test.setCreatedDate(account.Id, yesterday);

Let me know if it helps.

Thanks and Regards,
Shiva RV
Célio XavierCélio Xavier
Hello everyone, in my case I needed to test a class that needs an opportunity created with yesterday's date, I used the model below to solve the problem.
private class GetRescue300kTest {    
    static void setup(){

        Account ct = new Account();   
        ct.CPF__c = '123.123.123-12'; 
        ct.FirstName = 'CELIO';
        ct.LastName = 'XAVIER';               
        insert ct;         
        Opportunity op = new Opportunity();         
        op.Name = 'R5555'; 
        op.Loss_Reason__c = 'Bloqueio Judicial'; 
        op.AccountId = ct.Id;        
        op.StageName = 'Resgatado'; 
                               op.Reported_Value__c = 500000;        
        op.Amount = 500000; 
        insert op;  
        Datetime yesterday = Datetime.now().addDays(-1);
        Test.setCreatedDate(ct.Id, yesterday);     
        Test.setCreatedDate(op.Id, yesterday);
    static void testRescue300k(){            
        System.assertEquals(false, op.isEmpty());  
        GetRescue300k test = new GetRescue300k();