You need to sign in to do that
Don't have an account?
Shital Sonkusale
Formula fields not evaluating in test class
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!
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!
We have got the issue. SObject1 is a managed package object. Whenever we create SObject1, the managed package code changed it's name to created record's Id. However the change takes some time to reflect when created in a test class. That's why after inserting SObject1,when I queried it's name through soql it returned name as 'Name-3312'. But when using the formula field on sobject3, the name was already replaced by Id of sobject1 and hence it the formula field returned ID ,not the name. The managed package code wasn't caught in debug logs ,so took a long time to figure out whatsw happening.
Thank you all for your help, I am closing this thread now.
All Answers
newCollP = [SELECT Id,SObject2__r.SObject1__r.Name, Col_Type__C from SObject3__C where Col_Type__C='Name-3312'];
It might have to do with the two levels of lookup. Can you create a Col_Type__C formula field on SObject2__C, which is "SObject1__r.Name", and then redefine SObject3__C.Col_Type__C to be "SObject2__r.Col_Type__C"?
When I execute this query in Query editor I get below result:
Id=a33g0000000gAAQ
SObject2__r.SObject1__r.Name = [object Object]
Col_Type__C = 'Real Estate - 3123'
Such results I get.
And I cannot actually change any of the objects. I just need to cover these fields in my test class.
here Account_Name__c is the formula field on OpportunityLineItem (3rd Sobject)
You should get the desired result. Please mark this as best answer if it resolves.
I tried , but it's still populating some id in Formula field and not the coltype.name = 'Name-3312'
I have tried things like clearing test execution history and querying the specific field in test class,but nothing worked.
The formula field is populated with Salesforce 15 digit ID.
newCollP = [SELECT Id,SObject2__r.SObject1__r.Name, Col_Type__C from SObject3__C where Id=:newCollP.id];
Just add a debug "newCollp.Col_Type__c" right below your query in test class to see the result.
If still it didn't work then share your complete code here.
-Sid
We have got the issue. SObject1 is a managed package object. Whenever we create SObject1, the managed package code changed it's name to created record's Id. However the change takes some time to reflect when created in a test class. That's why after inserting SObject1,when I queried it's name through soql it returned name as 'Name-3312'. But when using the formula field on sobject3, the name was already replaced by Id of sobject1 and hence it the formula field returned ID ,not the name. The managed package code wasn't caught in debug logs ,so took a long time to figure out whatsw happening.
Thank you all for your help, I am closing this thread now.