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
Brandon Bridges 16Brandon Bridges 16 

Custom_Field++ in a trigger. Bad Idea?


 

I'm concerned that my outgoing email count will increment too many times due to multiple firings in the same insert.

Will someone please tell me if this trigger is any good? How could it be improved?

trigger FirstContactResolution_Email on EmailMessage(before insert){
/*General Description:000000000000000000000000000000000000000000000000000000000000000000000
 * Track how many outgoing emails are associated with a case. 
 * Note that the value does not decrease when an associated email is deleted.
 * This field and trigger are specifically to determine first contact resolution.
000000000000000000000000000000000000000000000000000000000000000000
for(EmailMessage e:Trigger.new){
if(e.Incoming==false)//Email must be outgoing
{
case myCase=[SELECT id,outgoing_email_count__c//Get related case info
FROM case
WHERE id=:e.parentId LIMIT 1];//Based on parent ID of email.
 if(myCase!=null){
if(myCase.Outgoing_Email_Count__c!=null)
myCase.outgoing_email_count__c++;//Add one to related case outgoing count.
else myCase.Outgoing_Email_Count__c=1;//Unless no value exists
System.Debug(myCase.Outgoing_email_count__c);
upsert(myCase);//Update that case
}}}}