Hello Friends, In this tutorial, I'll show you, how you can read the image URL that has been inserted in the rich text area on sobject. In this unit, you can find a sample code that read the image URL from the account object. This image URL will be used to display the image on the visualforce page. So without wasting time, let's get started
Get image from the rich text area
Step 1: Login to your Salesforce Org. and open developer console.
Step 2: Navigate to File | New | Apex Class and Create an Apex controller called GetAccountImageHandler and replace with the following code.
GetAccountImageHandler.apxc
GetAccountImage.vfp
Output:
See also:
Get image from the rich text area
Step 1: Login to your Salesforce Org. and open developer console.
Step 2: Navigate to File | New | Apex Class and Create an Apex controller called GetAccountImageHandler and replace with the following code.
GetAccountImageHandler.apxc
public class GetAccountImageHandler { private Id id; public GetAccountImageHandler(){ id = apexpages.currentpage().getparameters().get('id'); } public string accountImageURL{ get{ String imageURL=''; List<Account> lstAcc=[SELECT AccountImage__c FROM Account WHERE Id=:id]; Matcher imgMatcher = Pattern.compile( '<img(.+?)>' ).matcher(lstAcc[0].AccountImage__c); while (imgMatcher.find()) { String imageTag = imgMatcher.group(); System.debug('imageTag^^'+imageTag ); imageURL= imageTag.substringBetween(' src="', '"' ); System.debug('imageURL^^' + imageURL ); } return imageURL.unescapeHtml4(); } set{} } }Step 3: Navigate to File | New | Visualforce Page and create a new Visualforce Page called GetAccountImage and replace the following markup.
GetAccountImage.vfp
<apex:page controller="getAccountImageHandler" sidebar="false"> <img src="{!accountImageURL}"/> </apex:page>Note: Place an account Id(Id=AccountId) as a parameter in URL after previewing this visualforce page.
Output:
Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.
7 Comments
Hi Manish,
ReplyDeleteam able to display the text is as expected but am unable to display the image with correct height and width of the rich text field, could you please let me know how to display the text and image value of rich text field area in the visual force page.
Thank you!
hey, please email me your code, so clearly understand what you try to do, including rich text area field screenshot.
DeleteThank you
here is the code through which am trying to display the rich text area field(Resolution__c):
ReplyDelete
ReplyDeletestyle="font-family: 'Calibri', Arial, sans-serif ;font-size: 11.5px;padding-left:15px;margin-bottom:50px;width:5px !important;height:50px !Important;"
Im getting error 'Image not available'
ReplyDeleteI am getting the error message "List index out of bounds: 0"
ReplyDeleteI think the list is empty in which you try to getting value..
ReplyDeletePost a Comment