0

I am trying to figure out a way that I can look at the value of a text field and check it to see if any part of the text contains values that are entries in a Custom Setting. I tried the following to set up my Custom Setting list of words:

List<Keywords__c> listKeywords = [SELECT Name FROM Keywords__c];
Set<String> kw = new Set<String>;

Then within a loop of my custom object, I am trying to check if any part of the text field is one of the words in the list above:

for (Object__c object : Trigger.new) {
    if (kw.contains(object.Notes__c) {
        object.Impact__c = TRUE;
    } else {
        object.Impact__c = FALSE;
    }
}

The problem with this is that it checks the entire value of the Notes__c field against the list and does not just look for any part of Notes__c that is contained in the list. For example, if "Test" is in my custom setting, the code above works if the Notes__c field is "Test" but it does not work if the Notes__c field is "Test Test". Can anyone help with the proper code to get this to work?

2
  • 1
    Your code is checking if the custom setting contains your notes while your intention seems to check if the notes string contains the custom setting. Should the code be just the other way around, i.e. if (object.Notes__c.contains(kw))?
    – Chun
    Dec 2, 2020 at 1:18
  • Thanks. I did try that before posting but got an error: Method does not exist or incorrect signature: void contains(Set<String>) from the type String.
    – J. Neilan
    Dec 2, 2020 at 17:53

1 Answer 1

0

You can try this:

for (Object__c object : Trigger.new) {
    for (String note : object.Notes__c.split(' ')) {
        if (kw.contains(note) {
            object.Impact__c = TRUE;
            break;
        }
    }
    object.Impact__c = FALSE;
}

I will make an assumption and think that, if a "note" is indeed in your custom settings, you want Impact to be true, right? That is why I added the break;. But the key here is Notes__c.split(' ')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .