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??

  • 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 '15 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 '15 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 '15 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 '15 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 '15 at 16:04
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
}
  • Without a context your answer may count as a solution. However, retrieving 10,000 contacts with all their RichText values will hit the limits. – Andrii Muzychuk Aug 25 '17 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 – Ralph Callaway Nov 6 '18 at 0:50

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.