function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Swamy P R NSwamy P R N 

how to populate data into formulae field in test classes

Hi Buddies,

Am really afraid that data is not puplating in a formulae field after inserting the record in test classes.
Am doing this,
Opportunity__c copp = new Opportunity__c();
            copp.Name='test1';
            copp.Owner_of_Opportunity__c=[select id,name from user where name=:Userinfo.getname()].id;
            copp.Time_Based_Mgmnt__c=t.id;
            copp.Amount__c=35000;
            copp.Probability__c=string.valueof(10);
        insert copp;
        System.Debug('Probability is**'+copp.Probability__c+' Amount is**'+copp.Amount__c+' Expected Revenue is **'+copp.Expected_Revenue__c);

USER_DEBUG|[19]|DEBUG|Probability is**10 Amount is**35000 Expected Revenue is **null

Expected Revenue = Amount__c * VALUE(TEXT(Probability__c)) &
Probability is picklist value in my opportunity &
Amount is currency only.

After inserting opportunity, it needs to poulate a value in Expected Revenue. But it was not doing that, am confused. it is showing value in normal records, but it was not showing in test class records.

Please give me the solution.
Sonu Sharma.ax1610Sonu Sharma.ax1610
Hi Swamy,

Data is formula field is populated after saving record to database and to get value calculated by formula field, you have to use SOQL query after inserting record.

Opportunity__c copp = new Opportunity__c();
            copp.Name='test1';
            copp.Owner_of_Opportunity__c=[select id,name from user where name=:Userinfo.getname()].id;
            copp.Time_Based_Mgmnt__c=t.id;
            copp.Amount__c=35000;
            copp.Probability__c=string.valueof(10);
        insert copp;
Opportunity__c  oppp= [Select Probability__c,Amount__c,Expected_Revenue__c from Opportunity__c where id=:copp.id];
        System.Debug('Probability is**'+oppp.Probability__c+' Amount is**'+oppp.Amount__c+' Expected Revenue is **'+oppp.Expected_Revenue__c);

Use following code to get these values.

Regards
-S


Swamy P R NSwamy P R N
Yes Sonu Sharma,

It is coming value by querying it in test class, but its throwing an error while running my test class. Am just comparing integer value and passing formulae field value only, over here it is coming. check the error line in bold & underline format.

Test Class:
cls_Schedular_forSendingMail_toMngr ccm= new cls_Schedular_forSendingMail_toMngr();
            ccm.reachedMaxvalue();

Class Method:

public Integer oppamnt=0;
public void reachedMaxvalue(){
for(user u:[select id,name,email,managerid from user where isActive=true]){
             
  for(Opportunity__c opp:[select id, name, Expected_Revenue__c, Owner_of_Opportunity__c,         Time_Based_Mgmnt__c from Opportunity__c where Owner_of_Opportunity__c=:u.id]){
              if(oppamnt!=0 && oppamnt!=null){
                     oppamnt = integer.valueof(opp.Expected_Revenue__c)+oppamnt;--->Error Line
                }else{
                         oppamnt=integer.valueof(opp.Expected_Revenue__c);
                 }
       }
   }
}
Sonu Sharma.ax1610Sonu Sharma.ax1610
Hi Swamy,

Try with test class like this

@isTest
private class MyTestClassOpp
{
  static testMethod void testMyTestClassOpp()
  {
   Opportunity__c copp = new Opportunity__c();
            copp.Name='test1';
            copp.Owner_of_Opportunity__c=[select id,name from user where name=:Userinfo.getname()].id;
            copp.Time_Based_Mgmnt__c=t.id;
            copp.Amount__c=35000;
            copp.Probability__c=string.valueof(10);
        insert copp;
cls_Schedular_forSendingMail_toMngr ccm= new cls_Schedular_forSendingMail_toMngr();
            ccm.reachedMaxvalue();

//Opportunity__c  oppp= [Select Probability__c,Amount__c,Expected_Revenue__c from Opportunity__c where id=:copp.id];
  }

}

I hope this will solve your problem else also post the error message which you are getting.

Regards
-S