i have written a test class where i am comparing 2 fields. One of that fields is related to a formula field in an opportunity. More in detail: One field in the opportunity is filled by formula with a value from the linked account. This field is called 'Source_Details__c' in both, opp and acc.
here is the formula: "text( Account.Source_Details__c )" the field in Opportunity is called same 'Source_Details__c'
Another field in the opportunity will be filled with a trigger, based on that formula field. When i run a test class, i need to have the formula field to be filled, but it seems, that field is not filled, does a testclass not run formula fields? Maybe i should add the error message here:
System.QueryException: List has no rows for assignment to SObject
here is the trigger
trigger update_related_Supplier on Opportunity (before insert, before update)
{
for (Opportunity opp: Trigger.new)
{
if(opp.Source_Details__c != null)
{
Account[] acc = [Select Id from Account where Name = :opp.Source_Details__c] ;
if(acc.size() > 0)
{
opp.related_Partner_Supplier__c = acc[0].Id;
}
else
{
opp.related_Partner_Supplier__c = null;
}
}
}
}
and here is the test class
@IsTest
public class TestClassOppCaseStore {
static testMethod void TestClassOppCaseStore_trigger(){
Account a = new Account(Name = 'testAccount',
BillingPostalCode = '83059',
BillingCountry = 'Deutschland',
Source_Details__c = 'Düren (Saturn)');
insert a;
Lead l = new Lead (LastName = 'Musterhausen',
Country = 'Deutschland',
Company = 'MeierEisen',
PostalCode = '12457',
LeadSource = 'Import',
Status = 'new');
insert l;
Event k = new Event (Subject = 'Testing',
WhoId = l.Id,
StartDateTime = date.today(),
EndDateTime = date.today());
insert k;
Event m = new Event (Subject = 'Testing',
WhatId = a.Id,
StartDateTime = date.today(),
EndDateTime = date.today());
insert m;
Task t = new Task (Subject = 'Demotermin',
WhatId = a.Id);
insert t;
Task u = new Task (Subject = 'Demotermin',
WhoId = l.Id);
insert u;
Pricebook2 d = new Pricebook2 (Name = 'Standard');
insert d;
Product2 e =new Product2 (Name = 'Top AG',
VK_netto__c = 12);
insert e;
Opportunity g = new Opportunity (Name = 'testOpp',
AccountId = a.Id,
Amount = 250,
StageName = 'new',
CloseDate = date.today(),
related_Partner_Supplier__c = a.Id);
insert g;
update a;
update g;
CKSW_BASE__Service__c serv = new CKSW_BASE__Service__c (Opportunity__c = g.Id,
CKSW_BASE__Account__c = a.Id,
CKSW_BASE__Status__c = 'new');
insert serv;
Case c = new Case (Subject = g.Name,
AccountId = a.Id,
related_Partner_Supplier__c = a.Id);
insert c;
g = [SELECT Account.Id, Source_Details__c FROM Opportunity WHERE Id = :g.Id];
System.AssertEquals(a.Id, g.AccountID);
Account ag = [SELECT Id FROM Account WHERE Name = :g.Source_Details__c];
System.AssertEquals(ag.Id, g.related_Partner_Supplier__c);
c = [SELECT Account.Id, Account_Source_Details__c FROM Case WHERE Id = :c.Id];
System.AssertEquals(a.Id, c.AccountID);
Account ac = [SELECT Id FROM Account WHERE Name = :c.Account_Source_Details__c];
System.AssertEquals(ac.Id, c.related_Partner_Supplier__c);}}
recalculateFormulas()
developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… as long as they are not cross-object formulas