[SalesForce] Replace text in Rich text area field with trigger

We're using the "Solutions" standard object as our knowledge base, with "Solution details" standard field is a Rich Text Area field (32000 characters).

The field structure looks like this:

Purpose (header 1)

text..

Appropriate versions (Header 2)

text: version numbers.

Additional Details (Header 3)

text..

What I want to do is update the text under "header 2" without the need to enter to each solution and field and type the versions manually. I created a custom picklist field called "versions", and tried copying the picklist value into the "Solution details" field via workflow rule and field update – the problem is that it causes losing all HTML and turns this field to text only. I understood it's possible to update this without loosing HTML only with trigger. Did anyone accomplished something similar? are there even "Substitute" / "replace" functions in triggers??

Best Answer

Yes, APEX, the language used to write triggers, supports find and replace. String types (which include rich-text fields) have a number of options, primarily String.replace() and String.replaceAll() (see the String methods for the full list). You also use Pattern and Matcher classes that can do more complex regex related activities (see the Pattern and Matcher docs).

Basic Example

trigger YourTrigger on Solution (before update) {   
  for(Solution s : trigger.new) {
    s.replaceAll('Hello World','Goodbye World');
  }
}

More Info

If you're looking to explore what other functionality is available, you'll want to have the APEX API Reference handy.

Related Topic