You need to sign in to do that
Don't have an account?
Akshay Pai
OpportunityLineItem formula field is not getting correct values in Test class
I have a formula field in OpportunityLineItem -'Is Available'(IsAvailable__c) which takes values from product object's checkbox field -'Is Available'
FORMULA : PricebookEntry.Product2.IsAvailable__c
When I tried to create dummy OpportunityLineItem record within test class 'Is Available' formula field in OpportunityLineItem is returning me value as false,where as I am setting this value True at product level. Because of this major part of my class is not getting code coverage.
Can anyone please help me to find what I am doing wrong here.
FORMULA : PricebookEntry.Product2.IsAvailable__c
When I tried to create dummy OpportunityLineItem record within test class 'Is Available' formula field in OpportunityLineItem is returning me value as false,where as I am setting this value True at product level. Because of this major part of my class is not getting code coverage.
Can anyone please help me to find what I am doing wrong here.
static testmethod void TestMethod() { //Created A dummy Account and Opportunity records I.e Acc & Opt //getting standard pricebook id Id pricebookId = Test.getStandardPricebookId(); //Created a product Product2 prod = new Product2( Name = 'Product X', ProductCode = 'Pro-X', isActive = true, IsAvailable__c = True // setting field value as true ); insert prod; //Created pricebook entry PricebookEntry pbEntry = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 100.00, IsActive = true ); insert pbEntry; //created opportunity line item. OpportunityLineItem oli = new OpportunityLineItem( OpportunityId = opt.Id, units__c = 25, Type__c = 'No. Users', PricebookEntryId = pbEntry.Id, TotalPrice = 200, Quantity = 1 ); insert oli; system.debug('oli.IsAvailable__c ='+ oli.IsAvailable__c ); // This is returing me value as false Test.startTest(); //calling my required methods here Test.stopTest(); }
Best Answer chosen by Akshay Pai
Akshay Pai
I found the solution! As Formula field is calculated on the fly and the values will be available after record is committed to database ,After Inserting the Opportunity Line Item I re-queried the record and found the correct values.