• Shital Sonkusale
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies
I am trying to cover a method which uses formula field.
This is my test class code:

SObject1__C coltype = new SObject1__C();
coltype.name = 'Name-3312';
insert coltype;
System.debug('--->SObject1.Name = '+coltype.name); // Gives 'Name-3312';

SObject2__C colObj = new SObject2__C();
colObj.SObject1__C = coltype.Id;
insert colObj;  

SObject3__C newCollP = new SObject3__C();
newCollP.SObject2__C = colObj.Id,
insert newCollP;

SObject3__C has a fomula field named 'Col_Type__C' which is evaluated as follows:
"SObject2__r.SObject1__r.Name"
System.debug('--->newCollP.Col_Type__C = '+ newCollP.Col_Type__C); //returns a2Hg0000001tqQ2

This 'Col_Type__C' field's value is then used in a method in a helper class which i am trying to cover. As posted by many, I have tried to query the object SObject3__C and populate the field Col_Type__C in the test class as below. But it's not populatting the formula field's value as 'Name-3312'. 
newCollP = [SELECT Id,SObject2__r.SObject1__r.Name, Col_Type__C from SObject3__C where Id=:newCollP.id];

When I debug Col_Type__C value in helper class it returns some ID. I need the value of  as name of SObject1__C.

What is missing in my code? How can I get the formula field populated? 

Any help is appreciated!
I am trying to cover a method which uses formula field.
This is my test class code:

SObject1__C coltype = new SObject1__C();
coltype.name = 'Name-3312';
insert coltype;
System.debug('--->SObject1.Name = '+coltype.name); // Gives 'Name-3312';

SObject2__C colObj = new SObject2__C();
colObj.SObject1__C = coltype.Id;
insert colObj;  

SObject3__C newCollP = new SObject3__C();
newCollP.SObject2__C = colObj.Id,
insert newCollP;

SObject3__C has a fomula field named 'Col_Type__C' which is evaluated as follows:
"SObject2__r.SObject1__r.Name"
System.debug('--->newCollP.Col_Type__C = '+ newCollP.Col_Type__C); //returns a2Hg0000001tqQ2

This 'Col_Type__C' field's value is then used in a method in a helper class which i am trying to cover. As posted by many, I have tried to query the object SObject3__C and populate the field Col_Type__C in the test class as below. But it's not populatting the formula field's value as 'Name-3312'. 
newCollP = [SELECT Id,SObject2__r.SObject1__r.Name, Col_Type__C from SObject3__C where Id=:newCollP.id];

When I debug Col_Type__C value in helper class it returns some ID. I need the value of  as name of SObject1__C.

What is missing in my code? How can I get the formula field populated? 

Any help is appreciated!

I'm having a hard time writing a test method for a custom object that has Formula Fields on them that are being used in the methods I am trying to cover.

 

When I debug the fields in the test method they are coming up as null, but, if I query the record it shows those fields as being updated correctly.

 

Where I run into a problem is when I have nested formula fields and then pass the final record I want tested to the method via a trigger.  The field that starts at the top level of the hierarchy hasn't made it all of the way to the final record.

 

Is there something I'm missing or do I need to query each and every object before associating it to the next in the hierarchy?  The final record doesn't have a direct reference to the fields in question but the class queries them based on a set criteria.

 

Any help would be appreciated.  Thanks!