2

I have the following snippet:

for(Account eachAccount : accountWebsites){
            // if there's a match, assign the contact to the account and update
            System.debug('Searching for ' + myConDomain + ' in ' + eachAccount.Website +
                         '; index is ' + myConDomain.indexOf(eachAccount.Website));
            if(myConDomain.indexOf(eachAccount.Website)!=-1){
                eachAccount.OwnerId = myCon.id;
                update eachAccount;
            }    
  }

I have a test class where there definitely should be a match for the indexOf() but oddly, I get no match as shown by the output of my System.debug:

15:10:00:955 USER_DEBUG [19]|DEBUG|Searching for bark.com in www.bark.com; index is -1

Is there something I'm doing wrong or some concept about indexOf() that I'm not understanding?

Thank you in advance.

1 Answer 1

2

The issue here is that you're searching your substring (bark.com) for the full website name (www.bark.com) rather than the other way around. The string passed into the indexOf function should be what you are searching for, not what you are searching against.

This code should work:

for(Account eachAccount : accountWebsites){
    if(eachAccount.Website.indexOf(myConDomain)!=-1){
        eachAccount.OwnerId = myCon.id;
        update eachAccount;
   }    
}
1
  • Thank you, that fixed it!
    – Kate
    Dec 15, 2022 at 21:55

You must log in to answer this question.

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