0

Hi I have a custom setting which will contain the some part of "Subject", now when I will receive an email I need to compare this custom setting value from the "Subject" of the email. below is the code but Contains function is not working as it works only for Sequencing string. In below code consider str2 is the custom setting value and str1 is the subject value from the inbound email

string str1= 'As Oy Viherlaaksonranta 9 osoitteenmuutos';
string str2= 'As Oy osoitteenmuutos';

if(str2.contains(str1)){
   system.debug('inside if');
}

In the above code Contains is returning false. Can someone please help me, please let me know if any inputs from my side

  • Contains does exact matching. What you need is regex. vinaychaturvedi.com/blog/… – Pranay Jaiswal May 29 '18 at 14:27
  • I think you're sample is incorrect, str2 is much shorter than str1. Did you mean to do str1.contains(str2) instead? – gNerb May 29 '18 at 14:52
  • yup even if I do str1.contains(str2), it is not working. – Nshukla6 May 29 '18 at 15:57
  • Apolgies yes, it still wouldn't work, I just meant for your question, Its super important to have a clear question so that people can help. As for what will work, I have posted an answer below. – gNerb May 29 '18 at 16:00
1

Based on your two strings, You're gonna need a custom setting that has a comma separated string of keywords. You may also want to consider a match threshhold. For instance if the subject contains 80% of the keywords, you may want to take action (as subjects are unpredictable usually).

Below is a sample of what you can try. As written, this could cause false positives if the keywords are out of order so you may have to update the code if that is something you care about.

Map<String, SubjectKeywords__c> settings = SubjectKeywords__c.getAll();

for (Email email : emails) {
    Set<String> subject = new Set<String>(email.subject.split(' '));

    for (SubjectKeywords__c setting : settings) {
        Set<String> keywords = new Set<String>(setting.keywords__c.split(','));
        integer matchCount = 0;

        for (String keyword : keywords) {
            if (subject.contains(keyword)) {
                matchCount += 1;
            }
        }

        // Check to see if match count is above a set threshold (say, 80%)
        // if (matchCount / keywords.size() >= settings.threshhold__c / 100) {
        // }

        // Check to see if match count is exact if you do not want to use a threshold
        // if (matchCount == keywords.size()) {
        // }
    }
}

I'm not super familiar with your language. According to google it's finnish and "osoitteenmuutos" appears to translate to "address change". If that is the case, than maybe you don't want to include the "As Oy" in your search as these seem to be words that may appear in emails related to other topics as well. If you're looking for anything related to address change then your custom setting should only include the keyword "osoitteenmuutos".

This is mostly an uneducated guess though so I apologize if I made any mistakes during translation but the principal remains, you should try to keep your keywords to a minimum for best possible matches.

  • excellent Solution provided, specially Threshhold idea. Threshhold provide more stable solution in every cases. Thanks a lot again!! – Nshukla6 May 29 '18 at 18:03
1

Try utilising split and Set from Apex Class. Once you've set of subject defined in custom setting and email received. Iterate over them and check if keywords match or not.

Refer below example:

string str1= 'As Oy Viherlaaksonranta 9 osoitteenmuutos';
string str2= 'As Oy osoitteenmuutos';
Set<String> subjectKeywordsSet = new Set<String>(str2.split(' '));
Set<String> receivedEmailSubject =  new Set<String>(str1.split(' '));

Boolean isSubjectMatches = true;
for (String subjectKey : subjectKeywordsSet) {
  if (!receivedEmailSubject.contains(subjectKey))
    isSubjectMatches = false;
}
if(isSubjectMatches){
   system.debug('inside if');
}

Alternate way would be to use Regex.

  • This would fail if subject is "Dddajd As sddashdh", It will treat it as false positive. – Pranay Jaiswal May 29 '18 at 14:30
  • I believe ask is to match each keyword otherwise altering if statement can satisfy partial match as well, – Rohit Mourya May 29 '18 at 14:32
  • 1
    str2.split(' ') will return List<String> which can't be assigned to Set<String>. I have updated your code with Set<String> subjectKeywordsSet = new Set<String>(str2.split(' ')); – Himanshu May 29 '18 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.