0

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.

  • a.Upload_Image__c What are you storing in this field. – Tushar Sharma Oct 18 '16 at 13:50
1

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>
  • @ Rahul Sharma, Can you please tell me what is profilephoto/005/T – KS Kumaar Oct 18 '16 at 14:11
  • That is a standard salesforce image which is displayed for user when they do not have a profile picture. Have used it for demonstration. – Raul Oct 18 '16 at 14:14
  • Yes Rahul, I got it now. Its working now. – KS Kumaar Oct 18 '16 at 14:15
1

I believe the following change should work:

<apex:column>
    <apex:image url="{!blankvalue(a.Upload_Image__c, image)}" 
        width="50" height="50"> </apex:image>
</apex:column>

BlankValue allows you to show one text or another, depending on if the first value is blank (contains no text).

Alternatively, you could probably use:

"{!if(a.Upload_Image__c<>'', a.Upload_Image__c, image)}"
  • I tried with above two ways. But it is showing perfect result for records which does not have image and it is not giving proper image (showing some blank image) for records which have image. can you please tell me why we are getting this? – KS Kumaar Oct 18 '16 at 14:39
  • Sometimes there are problem with empty and null string values, try this - "{!IF(ISBLANK(a.Upload_Image__c), image, a.Upload_Image__c)}" – Raul Oct 19 '16 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.