[SalesForce] Displaying image in visualforce page from document as well as from object

I want to display two records Last name, upload_image from contact object, where upload_image is a rich text field and which is used to store an image in that field. But all the records in object does not have image except some of them can have.

if i display all the records in page from contact, last name will display from its object(as shown in pic). Like wise upload_image has to display its value if the record has an image. If that record does not have image, its need to take the image from document and need to display in visual force page.

To achieve this, I tried with the following code. But i did not get perfect output.

Controller:

public class ImageUpload {

    public String image {set;get;}
    public integer value {set;get;}
    public List<contact> cons {set;get;}

    public void main(){
        image = '/servlet/servlet.FileDownload?file=01528000002TdaY';
        cons = [select Lastname, upload_image__c from contact];
    }
}

VisualForce Page:

<apex:page controller="ImageUpload" sidebar="false" >
  <apex:form >
    <apex:pageBlock id="two">
       <apex:pageBlockSection >
           <apex:pageBlockSectionItem >
               <apex:pageBlockTable value="{!cons}" var="a">
                <apex:column value="{!a.Lastname}"/>
                <apex:column value="{!a.Upload_Image__c}"/>
               <apex:image url="{!image}" width="50" height="50"> </apex:image>
               </apex:pageBlockTable>
           </apex:pageBlockSectionItem>
       </apex:pageBlockSection>
        <apex:commandButton value="Click" action="{!main}" reRender="two"/>
    </apex:pageBlock>
  </apex:form>
</apex:page>

enter image description here

can anyone please help me with this….
any help is appreciated.

Best Answer

Try something like this:

<apex:page standardController="Contact" recordsetvar="Accounts">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!Accounts}" var="a">
            <apex:column value="{!a.Name}"/>
            <apex:column headerValue="Upload Image">
                <apex:outputField value="{!a.Upload_Image__c}" rendered="{!NOT(ISBLANK(a.Upload_Image__c))}"/>
                <apex:image value="/profilephoto/005/T" rendered="{!ISBLANK(a.Upload_Image__c)}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Related Topic