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
George AdamsGeorge Adams 

Trigger on Cases Needs to Count Associated Contact's Cases

Hello,

I've created a trigger on the Case object that checks to see if a certain checkbox field on the case is checked. If the box is checked, it will update a field on the Contact that is associated with that case. This part is working perfectly.

The next step is to only update the Contact if the current case is the first case associated with that Contact. If not, the trigger should do nothing. What's the best way to add that functionality to this existing trigger?
 
trigger unqualifyProbablySPAMFromTalkdesk2 on Case (before insert, before update) {

    //create set to hold the current case ID
    Set<Id> CurrentCaseContactID = new Set<Id>();
    
	//if current case is marked as SPAM, add to the above list
    for (Case c: Trigger.new) {
        if (c.We_Think_It_s_SPAM__c == true) {
            CurrentCaseContactID.add(c.ContactId);
        }    
    }
    
    //query Contact object for the entry with ContactId matching the current case
    List<Contact> ContactForUpdating = [SELECT Trigger_Test2__c
                                                       FROM Contact
                                      				   WHERE Id = :CurrentCaseContactID];
    
    //set the fields we want to update on the associated Contact
    for (Contact item: ContactForUpdating) {
        item.Trigger_Test2__c = 'worked';
    }
    
    //update the associated Contact
    update ContactForUpdating;
}

I'm trying to figure out how to count all cases associated with the current Contact, and either run this trigger if that sum <1 or do nothing.

Any help is appreciated as I'm very new to Apex (as you can probably see in the above code).

Thanks!
George AdamsGeorge Adams
For those finding this in the future, here's the solution I went with:
 
trigger unqualifyProbablySPAMFromTalkdesk2 on Case (before insert, before update) {

    Set<Id> CurrentCaseContactID = new Set<Id>();
    
        for (Case c: Trigger.new) {
            CurrentCaseContactID.add(c.ContactId);
    }
    
    integer caseCount;
    integer caseCount_var;
    
    AggregateResult[] ContactsByCaseCount = [SELECT count(id)caseCount
                                       				FROM Case
                                       				WHERE ContactId in :CurrentCaseContactID group by ContactId
                                            		LIMIT 1];    
	for (AggregateResult ar : ContactsByCaseCount) { 
		caseCount_var = integer.valueOf(ar.get('caseCount'));
	}
    
    Set<Id> CurrentCaseContactID_SPAM = new Set<Id>();
    
    for (Case c: Trigger.new) {
        if (caseCount_var<2 && c.We_Think_It_s_SPAM__c==true) {
            CurrentCaseContactID_SPAM.add(c.ContactId);
        } 
    }
    
    List<Contact> ContactForUpdating = [SELECT Trigger_Test2__c
                                                       FROM Contact
                                      				   WHERE Id = :CurrentCaseContactID_SPAM
                                       				   LIMIT 1];
    
    for (Contact item: ContactForUpdating) {
        item.Trigger_Test2__c = 'worked';
    }
    
    update ContactForUpdating;
}