7

Have a rich text field called - snapshot__c which has image in it. This field is inside custom object called obj__c.

obj__c has another field case__c using lookup in it.

On a Vf page, I'm selecting a case and need to display the image (snapshot__c). I am able to fire the query, but the image isn't just getting displayed. Instead getting something weird. Image for reference:

enter image description here

Query in controller:

con = [select snapshot__c from obj__c WHERE Case__c =: ex.case__c];

VF pageblock:

<apex:pageBlockTable value="{!con}" style="width:1200px;" var="c" rendered="{!if(showPB==true,true,false)}" border="2" title="Some Name">
    <apex:column value="{!c.snapshot__c}" style="width:500px;" rendered="false" />
</apex:pageBlockTable>

Any alternative for apex:pageBlockTable and/or apex:column ? Can I just use div and how? What am I doing wrong? Pls help.

0

2 Answers 2

5

Rich fields are stored as HTML in Salesforce, so to retrieve your image, you will have to parse the text and get the image from there. Luckily this is a fairly simple task to do.

Once queried, the field will look like this in the variable:

enter image description here

What you want is what is in the src attribute of the img tag. You can just copy the tag, or get the link and remake it in your page (using a repeat attribute or something).

Here's a sample code on how you would do this (I used a Developer Edition organization with a object from Trailhead):

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);
17
  • How simple is it? On the fly, how do I retrieve that scr code?
    – Viraj
    Commented May 25, 2016 at 19:54
  • Query for the field in the controller, and use the method substringBetween(open, close) from the String class. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented May 25, 2016 at 19:57
  • Read about it. So, in my case you meant to say, I will have to add the <img src as open and <img> as the 2 parameters?
    – Viraj
    Commented May 25, 2016 at 20:16
  • No, because you have the alt tag too. I would use <img and img> and then get the URL separately. Commented May 25, 2016 at 20:22
  • 1
    @RenatoOliveira I checked the url, there is a veritcal bar in my url which is replaced by &#124; . I guess this might be creating issues and & is changed to &amp
    – Nehal
    Commented May 24, 2019 at 12:42
2

Try below code,

<apex:pageBlockTable value="{!con}" style="width:1200px;" var="c" rendered="{!if(showPB==true,true,false)}" border="2" title="Some Name">
<apex:column>
  <apex:facet name="header">Snapshot</apex:facet>
  <apex:outputText value="{!c.snapshot__c}" escape="false"/>
</apex:column></apex:pageBlockTable>
5
  • I did try this, gave error as - SObject row was retrieved via SOQL without querying the requested field: obj__c .snapshot__c
    – Viraj
    Commented May 25, 2016 at 21:01
  • oh that shouldn't happen. You are actually querying snapshot__c field in the query right?
    – Victor
    Commented May 25, 2016 at 21:04
  • Added rendered="false" after escape="false" which gave the same output like the one in question.
    – Viraj
    Commented May 25, 2016 at 21:04
  • Yes, I re-checked the query and its right. Getting same as @Renato displayed it. Can we skip column tag and use any other? Plus, why the apex:outputText as the image would not be the text, but some image which is in RTF.
    – Viraj
    Commented May 25, 2016 at 21:10
  • img=img.substringBetween('src="', '"'); img=img.unescapeHtml4(); What I did is the same way, but used different parameters. But had the issues with decoding the &amp; character in the URL. The last line unescapeHtml4() actually resolved the issue
    – Viraj
    Commented May 26, 2016 at 13:47

You must log in to answer this question.

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