I have a Rich Text Area field named 'ItemDescription__c' that belongs to a custom object 'Item__c'. Is there any way to extract the image's URL in that text area (an HTML code), and put it in a different field?

The whole purpose is to put the image from the object in the CMS Collection, in the homepage of our Salesforce community (i tried to put the rich text field as the image in the collection but it doesn't work and i think it only gets a URL of the image).

Thank you for your help!

1 Answer 1


Campsite__c camp = [SELECT Id, RichField__c FROM Campsite__c LIMIT 1];

System.debug('Rich field:'); System.debug(camp.RichField__c);

String firstSubString = camp.RichField__c.substringBetween('<img', 'img>'); System.debug('First substring: ' + firstSubString);

String secondSubString = firstSubString.substringBetween('src="', '"'); System.debug('Second substring: ' + secondSubString);

String link = secondSubString.replace('amp;', ''); System.debug('Link: ' + link);

  • You can use this code to extract the image url.
  • Then, just add it to another field
  • and update it in Controller.cls

You must log in to answer this question.

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