You need to sign in to do that
Don't have an account?
Benjamin Louis
Trigger - Count string occurrence in outgoing case emails.
Hi Guys,
I would kindly like to ask for your help. I am a beginner Apex student, but I am stuck with a trigger :(.
I created a custom field on the case SObject which should serve as a counter.
The trigger should count the string occurences in only outgoing emails and return the # value to this field.
Basically what I want is there are negative words and positive ones (counter should be like positive - negative found words)
This is how far I got:
trigger CaseEmailScore on Case (after insert, after update) {
// Word criteria table,List
List<String> positiveWords = new List<String>();
positiveWords.add('Dear');
positiveWords.add('Kind');
positiveWords.add('Dreamforce');
positiveWords.add('Kind regards,');
positiveWords.add('Best regards,');
List<String> negativeWords = new List<String>();
negativeWords.add('wtf');
negativeWords.add('I\'m');
negativeWords.add('stupid');
for (Case myCase : Trigger.new) {
// Create list of positive emails which are within cases
List<EmailMessage> caseEmails = [ SELECT Id,
HtmlBody,
TextBody,
FromAddress,
ParentId
FROM EmailMessage
WHERE ParentId != null];
//AND TextBody LIKE :positiveWords]; -> ERROR: TextBody and Html can not be filtered in query call
}
}
//Counting calculations possible .countMatches()
I would really appreciate if you could help, so I can understand and continue learning :).
I would kindly like to ask for your help. I am a beginner Apex student, but I am stuck with a trigger :(.
I created a custom field on the case SObject which should serve as a counter.
The trigger should count the string occurences in only outgoing emails and return the # value to this field.
Basically what I want is there are negative words and positive ones (counter should be like positive - negative found words)
This is how far I got:
trigger CaseEmailScore on Case (after insert, after update) {
// Word criteria table,List
List<String> positiveWords = new List<String>();
positiveWords.add('Dear');
positiveWords.add('Kind');
positiveWords.add('Dreamforce');
positiveWords.add('Kind regards,');
positiveWords.add('Best regards,');
List<String> negativeWords = new List<String>();
negativeWords.add('wtf');
negativeWords.add('I\'m');
negativeWords.add('stupid');
for (Case myCase : Trigger.new) {
// Create list of positive emails which are within cases
List<EmailMessage> caseEmails = [ SELECT Id,
HtmlBody,
TextBody,
FromAddress,
ParentId
FROM EmailMessage
WHERE ParentId != null];
//AND TextBody LIKE :positiveWords]; -> ERROR: TextBody and Html can not be filtered in query call
}
}
//Counting calculations possible .countMatches()
I would really appreciate if you could help, so I can understand and continue learning :).
I made a new version that includes a fix for the NullPointerException, and wherre the word look-up is not case-sensitive anymore. Included a test class while I was on it as well.
Trigger: Test class (100% coverage)
All Answers
I would do it with EmailMessage as the triggered object instead of Case. This way we will only calculate on the actually triggered emails rather than process all the related emails on the Case that is triggered. Also since the Case isn't updated when a email is sent (without anything custom automation), this way it is always updated as soon as a new email is sent from a Case.
Please test it out and don't forget to change the question to Solved and select Best Answer if this would solve your issue.
Pssst. Replace Positive_Email_Words__c and Negative_Email_Words__c to the fields you created on Case already.
Awesome code, I still need to learn a few things I guess :P! I can see the counting works perfectly, but I get a NullPointerException on line 39,
can you please help, how to get rid of it? I think it's cause the c.Positive_Email_Words__c is actually null, and can not add to a null value.
Thank you so much!
Nevermind, I solved the NullPointerException , set default value of Positive_Email_Words and Negative_Email_Words to 0 :).
Thanks again :)!
I made a new version that includes a fix for the NullPointerException, and wherre the word look-up is not case-sensitive anymore. Included a test class while I was on it as well.
Trigger: Test class (100% coverage)
I guess it can't get any better :) , really appreciate your help, test class is also working perfectly!
Thank you so much!