2

i want to search a text in Rich Text area field of a custom object.

Since We can't use the RichTextArea field in the where clause of SOQL, I am looking for the way to search a text.

Is there any way to do it??

5
  • Do you want to look for a static string? In that case you could create a formula field with a regular expression in it to search for your string. Then you could query that new field to determine if that record should be returned in your query.
    – Kasper
    Jun 12, 2015 at 10:40
  • That is actually wrong. Can't use regex in a formula field. You could put it in a work flow rule and use that to update a record. Bit of a hassle, but it could be done i guess.
    – Kasper
    Jun 12, 2015 at 14:25
  • This came up in the 'queue' for review. Your answer would have been more appropriate as a comment to obtain clarification. You mention workflow and query. It's unclear if you meant running a query on the field (can't do that in workflow) or using a string method. This question doesn't appear to be about workflow. It's tagged with Apex, Dynamic-SOQL, and SOSL. One can use RegEx in Apex matcher methods, but formula fields are not used in Apex. You've not provided an answer that's relevant to the question.
    – crmprogdev
    Jun 12, 2015 at 14:44
  • Ah, i think my answer might have been on the short side. I'm assuming that the op would like to do a sosl/soql query on a rich text area and that this isn't allowed. A work around for this would be to have the matching be done by a formula field or a workflow rule depending on requirements. After that the query can be run on the created field. This works around the restrictions of not being able to do a where query on the rich text area. Haven't tested it though.
    – Kasper
    Jun 12, 2015 at 15:46
  • 2
    Hi Boopathi, welcome to SFSE! Please take the time to visit the Help center and read How do I ask a good question. The more details you provide, particularly code you've written, the more likely it is that someone will respond to your question with an answer that you'll find helpful.
    – crmprogdev
    Jun 12, 2015 at 16:04

1 Answer 1

1

I know this question was asked quite a while ago, but for any of you who are looking for help, I offer this solution.

You can perform filtering as desired in Apex. The quickest and easiest way to execute Apex is in the "Execute Anonymous Window" of the Force.com Developer Console.

Below is an example of searching for 'foo' in a Rich Text custom field (Rich_Text__c) on the Contact.

List<Contact> contactList = [Select Id, Rich_Text__c FROM Contact];

for( Contact theContact : contactList ) {
    if(theContact.Rich_Text__c != null && theContact.Rich_Text__c.contains('foo')) {
    // Do what you will
}
2
  • Without a context your answer may count as a solution. However, retrieving 10,000 contacts with all their RichText values will hit the limits. Aug 25, 2017 at 11:13
  • you can get up to 50k in one transaction, and no need to put them into a list, just pass the query directly into the for loop instead of creating an intermediary loop Nov 6, 2018 at 0:50

You must log in to answer this question.

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