Sunday, 24 January 2021

How to get Image URL from a RichTextArea field

 Hi,

Here we are going to learn how to get an Image URL from a RichTextArea if someone adds an image in RichTextArea.

String decodedImageUrl;

 Matcher imgMatcher = Pattern.compile( '<img(.+?)>' ).matcher(<RichTextArea_FieldAPIName>);

// iterate each image tag found

        while ( imgMatcher.find() ) {        

            // get the image tag html

            String imageTag = imgMatcher.group();

            System.debug( 'imageTag=' + imageTag );

            String imageURL = imageTag.substringBetween( ' src="', '"' );

            System.debug( 'imageURL=' + imageURL );

            decodedImageUrl= imageURL.unescapeHtml4();

         }  

Reference:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_matcher_methods.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_pattern_and_matcher_pattern_methods.htm#apex_classes_pattern_and_matcher_pattern_methods


No comments:

Post a Comment

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...