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
Nancy GreshamNancy Gresham 

Test for Trigger

Hi guys! I've written my first trigger, which works in my sandbox, but I can't successfully write a test for it.  The field that it filters on is a read only formula field.  Any help would be greatly appreciated!
Thanks much!

trigger accountRankings on Account(after insert, after update, after delete){

for(Account acc : trigger.new)
{
//checking if the value is changed
if(acc.VAROrderPointsYTD__c != Null && (trigger.isInsert || (trigger.newMap.get(acc.id).VAROrderPointsYTD__c != trigger.oldMap.get(acc.id).VAROrderPointsYTD__c)))
{
break;
}
else return;
}

//rank top 100 accounts for more accounts change the row limit
List<Account> accounts = [Select VAROrderPointsYTD__c, Point_Ranking__c from Account Where RecordType.Name = 'Richmond VAR' ORDER BY VAROrderPointsYTD__c DESC limit 100];

Integer rank = 1;
for (Account account : accounts)
{
account.Point_Ranking__c = rank;
rank++;
}
update accounts;
}
 

 

Best Answer chosen by Nancy Gresham
SalesFORCE_enFORCErSalesFORCE_enFORCEr
You just need to create an Account with Record Type as Richmond VAR and insert it inside Test.startTest() and Test.stopTest(). Also, make sure VAROrderPointsYTD__c is populated so check the formula to understand how is it getting populated.

All Answers

SalesFORCE_enFORCErSalesFORCE_enFORCEr
You just need to create an Account with Record Type as Richmond VAR and insert it inside Test.startTest() and Test.stopTest(). Also, make sure VAROrderPointsYTD__c is populated so check the formula to understand how is it getting populated.
This was selected as the best answer
Siddharth Jain 64Siddharth Jain 64
Hi Nancy,

Read Only formula fields return values only when queried. So in your test class populate the fields with your test data that constitute the formula for that field and this should solve your problem.

I this answer help please do not forget to mark this as a best answers

Thanks
Siddharth
Nancy GreshamNancy Gresham
Thank you both so much! That is exactly what i needed. :)