Friday, 13 June 2014

Displaying Image In Standard Layout

Hi All,
This post is regarding to store image in a field of an Object or to displaying image in standard layout. 

Approach:-
  • Create an URL field in any object say Image_Url__c
  • Create an Formula Field in that object and refer the new url field .
  • Image = IMAGE( Image_Url__c , 'Company Logo',200,200).
  • Create Static Resource and store all the image in the static resource.
  • Click on View File link of each Static Resource and copy the Url.
  • Now create Visualforce page and controller.
  • In controller page paste link which you have copied from Static resource.
  • Run page and enter account name ,choose image and then click on save button.
This is the page
<apex:page controller="addImageTorecordsController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
               <apex:inputField value="{!account.Name}"/>
               <apex:selectList label="Image URL" value="{!url}" size="1" multiselect="false">
                   <apex:selectoptions value="{!options }">
                   </apex:selectoptions>
               </apex:selectList>
               <apex:commandButton value="Save" action="{!savingaccount}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

This is the Controller:-

public with sharing class addImageTorecordsController {
    public Account account{get;set;}
    public List<SelectOption> options{get;set;}
    public String url{get;set;}
    public addImageTorecordsController (){
        account = new Account();
        options = new List<SelectOption>();
        options.add(new SelectOption('-None-','-None-'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1323960689000/asish','Lord Hanuman'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1380178695000/logo','Invoice It'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1324022123000/Library','Library'));
        options.add(new SelectOption('https://ap1.salesforce.com/resource/1402657479000/Asishhndsome','Asish'));
    }
   
    public PageReference savingaccount(){
       account.Image_Url__c = url;
       System.debug('printing Image-->'+url);
        insert account;
        return (new PageReference('/'+account.id));
    }
   
   
}

Red Color text are static resource image link which will differ from Org to org.For you link will be different.

No comments: