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
DeptonDepton 

SOQL for loop help

/* Provide summary of Number of Cases on Contacts record */ 

trigger CaseSumTrigger on Case(after delete, after insert, after undelete, 
after update) {

    Case [] cas;
    if (Trigger.isDelete) 
        cas= Trigger.old;
    else
        cas= Trigger.new;

    // get list of Contacts
    Set<ID> contIds = new Set<ID>();
    for (Case cs : cas) {
            contIds.add(cs.ContactId);
    }
    
    Map<ID, Case> CasesForContacts = new Map<ID, Case>([select Id
                                                            ,ContactId
                                                            from Case
                                                            where ContactId in :contIds]);

    Map<ID, Contact> contToUpdate = new Map<ID, Contact>([select Id
                                                                 ,items__c
                                                                  from Contact
                                                                  where Id in :contIds]);
                                                                 
    for (Contact  cont: contToUpdate.values()) {
        Set<ID> casIds = new Set<ID>();
        for (Case cs : CasesForContacts.values()) {
            if (cs.ContactId == cont.Id)
                casIds.add(cs.Id);
        }
        if (cont.items__c != casIds.size())
            cont.items__c = casIds.size();
    }

    update contToUpdate.values();

}

 

How/Where do i add the loop to get only CLOSED cases?

Thank you!

 

Best Answer chosen by Admin (Salesforce Developers) 
N.V.V.L.Vinay KumarN.V.V.L.Vinay Kumar
/* Provide summary of Number of Cases on Contacts record */ 

trigger CaseSumTrigger on Case(after delete, after insert, after undelete,
after update) {

    Case [] cas;
    if (Trigger.isDelete)
        cas= Trigger.old;
    else
        cas= Trigger.new;

    // get list of Contacts
    Set<ID> contIds = new Set<ID>();
    for (Case cs : cas) {

    if(cs.Status == 'Closed' && cs.ContactId != null){
            contIds.add(cs.ContactId);
    }

    }
    
    if(contIds != null && contIds.size() . 0){
    
    Map<ID, Case> CasesForContacts = new Map<ID, Case>([select Id
                                                            ,ContactId
                                                            from Case
                                                            where ContactId in :contIds]);

    Map<ID, Contact> contToUpdate = new Map<ID, Contact>([select Id
                                                                 ,items__c
                                                                  from Contact
                                                                  where Id in :contIds]);
   }

   if(contToUpdate != null && CasesForContacts != null){
                                                                 
    for (Contact  cont: contToUpdate.values()) {
        Set<ID> casIds = new Set<ID>();
        for (Case cs : CasesForContacts.values()) {
            if (cs.ContactId == cont.Id)
                casIds.add(cs.Id);
        }
        if (cont.items__c != casIds.size())
            cont.items__c = casIds.size();
    }

    update contToUpdate.values();
   }

}



Try this and if it works mark it as solution

All Answers

SLockardSLockard

Just change your first for loop like this:

for (Case cs : cas) 
{
     if (cs.Status == 'Closed')
     {
          contIds.add(cs.ContactId);
     }
}

 

DeptonDepton

Thank you!

 

might be something wrng in the code, it was working, then I have added yours, and now is not working

I am checking but any help would be really appreciated!

 

I just want to have a total number of cases open on the contact record!:)

N.V.V.L.Vinay KumarN.V.V.L.Vinay Kumar
/* Provide summary of Number of Cases on Contacts record */ 

trigger CaseSumTrigger on Case(after delete, after insert, after undelete,
after update) {

    Case [] cas;
    if (Trigger.isDelete)
        cas= Trigger.old;
    else
        cas= Trigger.new;

    // get list of Contacts
    Set<ID> contIds = new Set<ID>();
    for (Case cs : cas) {

    if(cs.Status == 'Closed' && cs.ContactId != null){
            contIds.add(cs.ContactId);
    }

    }
    
    if(contIds != null && contIds.size() . 0){
    
    Map<ID, Case> CasesForContacts = new Map<ID, Case>([select Id
                                                            ,ContactId
                                                            from Case
                                                            where ContactId in :contIds]);

    Map<ID, Contact> contToUpdate = new Map<ID, Contact>([select Id
                                                                 ,items__c
                                                                  from Contact
                                                                  where Id in :contIds]);
   }

   if(contToUpdate != null && CasesForContacts != null){
                                                                 
    for (Contact  cont: contToUpdate.values()) {
        Set<ID> casIds = new Set<ID>();
        for (Case cs : CasesForContacts.values()) {
            if (cs.ContactId == cont.Id)
                casIds.add(cs.Id);
        }
        if (cont.items__c != casIds.size())
            cont.items__c = casIds.size();
    }

    update contToUpdate.values();
   }

}



Try this and if it works mark it as solution
This was selected as the best answer