The case is related to an opportunity which has quotes attached. I am struggling to build the formula because you can't use a roll-up on a cases because they are a child object
#Trailhead Challenges
Hi, @melissa vaughn
You are absolutely right: in Salesforce, you cannot directly use a Roll-Up Summary Field on the Case object to count Quotes, as Case is a child object of Opportunity (through a lookup relationship), and Roll-Up Summary only works for master-detail relationships. However, we can solve this using Record-Triggered Flow or Apex Trigger.
1. Create a field Quote_Count__c on Opportunity (Number).
2. Write a trigger on the Quote object:
(example of Apex Trigger):
trigger QuoteCountTrigger on Quote (after insert, after update, after delete) {
Set<Id> oppIds = new Set<Id>();
if (Trigger.isInsert || Trigger.isUpdate) {
for (Quote q : Trigger.new) {
if (q.OpportunityId != null) {
oppIds.add(q.OpportunityId);
}
}
}
if (Trigger.isDelete) {
for (Quote q : Trigger.old) {
if (q.OpportunityId != null) {
oppIds.add(q.OpportunityId);
}
}
}
if (!oppIds.isEmpty()) {
List<Opportunity> oppsToUpdate = new List<Opportunity>();
for (AggregateResult ar : [SELECT OpportunityId, COUNT(Id) quoteCount
FROM Quote
WHERE OpportunityId IN :oppIds
GROUP BY OpportunityId]) {
oppsToUpdate.add(new Opportunity(
Id = (Id)ar.get('OpportunityId'),
Quote_Count__c = (Decimal)ar.get('quoteCount')
));
}
if (!oppsToUpdate.isEmpty()) {
update oppsToUpdate;
}
}
}
Sincerely,
Mykhailo Vdovychenko
Bringing Cloud Excellence with IBVCLOUD OÜ