You need to sign in to do that
Don't have an account?
George 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?
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!
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 Adams
For those finding this in the future, here's the solution I went with: