1

Is there a way to override Rich Text Area field font-family with apex code? My first attempt was with !important key-word in inline style for tag, but the style was erased.

Steps to reproduce:

SELECT Notes__c FROM Account

<span style="font-family: simsun;">123</span>

Then execute code in Dev Console:

Account a = [SELECT Notes__c FROM Account LIMIT 1];
a.Notes__c = '<span style="font-family: Montserrat !important;">' + a.Notes__c + '</span>';
update a;

Then retrieve Notes once more:

SELECT Notes__c FROM Account

<span style=""><span style="font-family: simsun;">123</span></span>

NOTE 1:

If I update Rich Text Area field without !important it looks like:

<span style="font-family: Montserrat;"><span style="font-family: simsun;">123</span></span>

But it does not override inner font-family (as expected).

NOTE 2:

I see the possibility with parsing Rich Text Area field content and removing all occurrences of font-family:...; from it, but I try to find an easier way.

NOTE 3: (related to @arut's answer)

I do have different font-families, so font-family: simsun; was just for example. Therefore, I cannot use replace.

0

1 Answer 1

2

Instead of a.Notes__c = '<span style="font-family: Montserrat !important;">' + a.Notes__c + '</span>';, you need to execute

a.Notes__c = a.Notes__c.replace('font-family: simsun;', 'font-family: Montserrat;')

Note that a.Notes__c = a.Notes__c.replace('font-family: simsun;', 'font-family: Montserrat !important;') will not work because having !important seems to wipe out the contents of style attribute, resulting in <span style="">123</span>. Also, you need to take precaution while updating rich text field value styles via code because there could be other elements with this style which you might not want to overwrite.


The following is not really an answer. If you are interested in knowing a bit more about how CSS would work with your expected update, keep reading. Even if you succeed in updating notes to <span style="font-family: Montserrat !important;"><span style="font-family: simsun;">123</span></span>, I doubt it would work as expected. In CSS, !important instructs all other subsequent rules for a particular element (or element type) to be ignored [The key word to note here is 'particular'].

An inline CSS on outer span element (with style="font-family: Montserrat !important;") will not override inner span's inline CSS (style="font-family: simsun;"), but all other font-family styles defined on the outer span element.

On the contrary, if an internal style sheet or external style sheet was applied (to the page displaying this notes field) with span {font-family: Montserrat !important;}, the both outer span and inner span would obey this style.

So, you have to update to either <span style="font-family: Montserrat;"><span>123</span></span> or <span style="font-family: Montserrat;">123</span>.

4
  • Thank you, that's really helpful, but I have mentioned particular font-family just for example, so I may have any other font-families and replace will not work in that case. Oct 24, 2020 at 12:14
  • If you need to able to replace different font-families, you can do it by using replace along with some conditional code logic. As mentioned in my answer, updating text area field can be tricky and one needs to be cautious. At a high level, do you have some analysis on what all updates are to be done (i.e., is it just font-family replacements)? and, how many records are to be updated (rather, how much content & formatting is present in the rich text area fields of these records)?
    – arut
    Oct 24, 2020 at 17:06
  • 1
    It is just about font-families, I need to update it every time text in rich area is updated (trigger on insert and on update). I think I will try to make replace with regex, thank you! Oct 25, 2020 at 17:19
  • 1
    I would suggest you to also consider capturing these field updates in a custom object (at least for initial few months; so you have a back-up to revert if something is messed up). Reason being updating text area fields via code is not something I've heard being done quite often and as such there could be unidentified issues that could lead to formatting inconsistencies. Also, with standard field tracking, I did notice this one known issue. So, you should be able to differentiate b/w your updates and the regular updates.
    – arut
    Oct 25, 2020 at 17:30

You must log in to answer this question.

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