TransWikia.com

Calculation not working

Salesforce Asked by gnatrae on December 19, 2021

I’ve got a trigger I’m working on and it’s not calculating right. I am trying to count the number of Open System cases (indicated in the Send to: field). The calculation is not working. It doesn’t seem to count over one and if I edit a case that’s Not a System Sales case, it takes the count back down.

trigger OpenCasesSystemSales on Case (after insert, after update, after delete, after undelete) {
  List<Case> caseList = (trigger.isDelete)? trigger.old : trigger.new;
  set<Id> contactIdSet = new set<Id>();
  for(Case cs: caseList ){
     contactIdSet .add(cs.ContactId);
  }
if(contactIdSet.size() > 0){
    List<Contact> lstContact = [select id, Open_System_Sales_Cases__c, (select id, Send_To__c, status, Is_Closed__c from Cases)
    from Contact where id IN: contactIdSet ];
       if(lstContact.size() > 0){ 
          for(Contact con: lstContact)
          { 
             con.Open_System_Sales_Cases__c = con.Cases.size();
             Integer openCases = 0;
             for(Case cs : caseList){
                 if(cs.Is_Closed__c == false && cs.Send_To__c =='System Sales' )
                   openCases++;
             }
             con.Open_System_Sales_Cases__c = openCases;
          }
          update lstContact;
       }
    }
 }

One Answer

So far, I don't see exactly how to produce the behavior you're describing. There's definitely a couple of issues here, but I can't reason out the straight line to the specific problem you articulate. If you have other automation running on this object, that's something to take into account.

I'd streamline the logic like this:

List<Contact> lstContact = [
   SELECT Id, 
          (SELECT Id, Send_To__c, status, Is_Closed__c 
           FROM Cases
           WHERE Is_Closed__c = false AND Send_To__c = 'System Sales')
   FROM Contact
   WHERE Id IN :contactIdSet 
];
for (Contact con : lstContact) { 
    con.Open_System_Sales_Cases__c = con.Cases.size();
}
update lstContact;

You can let the SOQL query do some of the work for you and make the update straightforward.

Another approach would be to do away with the direct SOQL query and use an aggregate query instead, which could look something like this:

Map<Id, AggregateResult> agrs = new Map<Id, AggregateResult>([
   SELECT ContactId Id, count() ct
   FROM Case
   WHERE ContactId IN :contactIdSet
         AND Is_Closed__c = false 
         AND Send_To__c = 'System Sales'
   GROUP BY ContactId
]);
List<Contact> contactList = new List<Contact>();

// Since Aggregate Queries won't give us data on Contacts with 0
// Cases, iterate over the Contact Ids and source their count (or 0)
// from the Map
for (Id contactId : contactIdSet) {
    Integer count = (agrs.containsKey(contactId) ? agrs.get(contactId).get('ct') ? 0);
     contactList.add(new Contact(Id = contactId, Open_System_Sales_Cases__c = count));
}
update contactList;

Answered by David Reed on December 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP