Standard Controller Extension,Custom Controller Extensions and Uploading the image and displaying in contact details page.
Hi,
In this post i am trying to give a small example on Standard Controller Extension. so the usage of Standard Controller Extension (or) Controller Extension is to extend the functionality.
Here the requirement is create a visualforce page using standard controller and extend the upload image functionality in VF page and display the image in Contact detail page. for this i created two fields in Contact object.
1). Created a Text field with Name "Srinivas__Images_Path__c" length of 255 to store the url of the image.
2). Created a Formula Field with the name of "Srinivas__Picture__c" return type as Text like this.
Extension Class:
Here i created a class and used Standard Controller in constructor. once the user upload the image that image details is stored as attachment and put the attachment record url in contact object text field to populate with formula field.
public class ContactPhotoExtension {
public Contact cont;
public blob picture {get;set;}
public ContactPhotoExtension(ApexPages.StandardController st){
this.cont = (Contact) st.getRecord();
}
public PageReference save() {
PageReference pr ;
try{
insert cont;
if(picture !=null) {
Attachment attachment = new Attachment();
attachment.body = picture;
attachment.name = 'Contact_' + cont.id + '.jpg';
attachment.parentid = cont.id;
attachment.ContentType = 'application/jpg';
insert attachment;
cont.Srinivas__Images_Path__c = '/servlet/servlet.FileDownload?file='+ attachment.id;
update cont;
Pr = new PageReference('/'+cont.id);
pr.setRedirect(true);
}
} catch(Exception e){
system.debug('Error Message==>'+e);
}
return pr;
}
}
Visualforce Page:
<apex:page standardController="Contact" extensions="ContactPhotoExtension">
<apex:form >
<apex:pageblock title="Contact With Image">
<apex:pageblockButtons >
<apex:commandButton action="{!Save}" value="Save with Image"/>
</apex:pageblockButtons>
<apex:pageblockSection title="Contact Information">
<apex:inputField value="{!Contact.FirstName}"/>
<apex:inputField value="{!Contact.LastName}"/>
<apex:inputField value="{!Contact.Email}"/>
<apex:inputField value="{!Contact.Phone}"/>
</apex:pageblockSection>
<apex:pageBlockSection title="Upload Image Here">
<apex:inputFile value="{!picture}" accept="image/*" />
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>
once you develop these things try to create a record with image and click on "Save with Image" button.
Finally the record detail page is like this.like this you can extend the functionality of standard or custom with out building the all the things.
Custom Controller With Extension:
Here i created two simple classes to demonstrate simple custom controller extension.
Class 1 :
in this class i am querying account records and stored those in list.
Public class AccountDisplatRecCls{
public List<Account> getAccounts(){
List<Account> accList = [select id,Name,AccountNumber from Account];
return accList;
} }
Class 2:
in this class i am querying contact records and stored those in list and displaying in vf page.
public class AccountDisplatRecClsExtn{
public List<Contact> contList {get;set;}
public AccountDisplatRecClsExtn(AccountDisplatRecCls act){
contList = [select id,FirstName,LastName,Email from Contact];
}
}
Visualforce Page:
in this page i am using booth custom controller and extension controller.
<apex:page controller="AccountDisplatRecCls" extensions="AccountDisplatRecClsExtn">
<apex:pageBlock title="Account Records">
<apex:pageblockTable value="{!Accounts}" var="acc">
<apex:column value="{!acc.Name}"/>
<apex:column value="{!acc.accountNumber}"/>
</apex:pageblockTable>
</apex:pageBlock>
<apex:pageblock title="Contacts Details">
<apex:pageblockTable value="{!contList }" var="con">
<apex:column value="{!con.FirstName}"/>
<apex:column value="{!con.LastName}"/>
<apex:column value="{!con.Email}"/>
</apex:pageblockTable>
</apex:pageblock>
</apex:page>
Output:
Further reference :
http://www.salesforce.com/docs/developer/pages/Content/pages_controller_extension.htm
Thanks
2 comments:
Thank you it was very helpful I f you can shoot an video explanation will apppreciate that a lot
Thank you for sharing the information.دانلود آهنگ جدید
Post a Comment