Skip to main content
learn sfdc 提问于 #Apex
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.

test.starttest

   mybatchcls mb=new mybatchcls();

   database.execute();

test.stoptest;

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.   
8 个回答
  1. 2017年6月7日 06:29
    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