What if I want to display my image on contact, and I frequently change it also. The answer in general is, create a visualforce page page and override it to native contact detail page.
This doesn't sounds good to me, am not happy overriding native pages provided by Salesforce. So I finally decided to put my head together and think how I can do this in another way.
Idea came in mind that what if I use Notes and Attachments on Contact object. Yes! I have uploaded some of my pictures in Notes and Attachment and provided a button on detail page to select one of them.
When you click on the "Set Profile Picture" a pop up gets open where all valid images are shown with radio option as shown in above screen. Selected image is displayed on contact like this,
How can you implement this?? Follow some simple steps.
Make sure you copy all Labels, Names etc correctly.
1) Create field of Type : "Text" with Field Label : "StoreImageUrl" and Field Name : "StoreImageUrl" (Lenght - 255) -> Make it visible to all profiles but do not show this on any page layout. This is for internal use only.
2) Create another field of Type : "Formula" with Field Label : " " and Field Name : "Image" with return type "Text"
Formula will contain
3) Create an Apex Class
4) Create a visualforce page with Label and Name "DisplayImages"
5) Create a Detail Page button with Label : "Set Profile Picture" and Name : "Set_Profile_Picture", Behavior : "Execute Java Script" and Content Source : "OnClick JavaScript"
6) Edit the contact layout and add "Set Profile Picture" button.
7) Create a contact and add some images in notes and attachments, click on "Set Profile Picture". Select the image and click save.
*Note : I hate using JS with IE.
Also you may have to change a line of the Apex code above : "https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=' + selectedImage ;"
in "SaveImage()" method.
Above path can be different on different organizations, so you just need to make sure that this is correct by :
View the attachment uploaded and copy the path from the URL except the ID.You can also create a custom setting to store this path.
Once above steps are done we are all set to go.
Cheers.
This doesn't sounds good to me, am not happy overriding native pages provided by Salesforce. So I finally decided to put my head together and think how I can do this in another way.
Idea came in mind that what if I use Notes and Attachments on Contact object. Yes! I have uploaded some of my pictures in Notes and Attachment and provided a button on detail page to select one of them.
When you click on the "Set Profile Picture" a pop up gets open where all valid images are shown with radio option as shown in above screen. Selected image is displayed on contact like this,
How can you implement this?? Follow some simple steps.
Make sure you copy all Labels, Names etc correctly.
1) Create field of Type : "Text" with Field Label : "StoreImageUrl" and Field Name : "StoreImageUrl" (Lenght - 255) -> Make it visible to all profiles but do not show this on any page layout. This is for internal use only.
2) Create another field of Type : "Formula" with Field Label : " " and Field Name : "Image" with return type "Text"
Formula will contain
IF( StoreImageUrl__c != null , IMAGE( StoreImageUrl__c , '' , 100 , 100 ) , IMAGE('' , '' , 0 , 0))Make it visible for all profiles and show it at top on all layouts.
3) Create an Apex Class
public class DisplayImagesController { //Parent Contact private String ContactId ; //Image selected from UI public String selectedImage {get; set;} public DisplayImagesController() { //Fetching contact Id ContactId = ApexPages.CurrentPage().getParameters().get('Id') ; selectedImage = '' ; } public boolean validateImage(String image) { String Regex = '([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)'; Pattern MyPattern = Pattern.compile(Regex); Matcher MyMatcher = MyPattern.matcher(image); return MyMatcher.matches() ; } public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); //All attachments related to contact List<Attachment> attachLst = [SELECT Id , Name FROM Attachment WHERE ParentId =: ContactId] ; //Creating option list for(Attachment att : attachLst) { String imageName = att.name.toLowerCase() ; if(validateImage(imageName)) { options.add(new SelectOption(att.Id , att.Name)); } } return options ; } public PageReference SaveImage() { //Contact to update List<Contact> conToUpdate = new List<Contact>() ; conToUpdate = [select id,StoreImageUrl__c from contact where id =: ContactId] ; //Inserting image parth if(conToUpdate.size() > 0) { conToUpdate[0].StoreImageUrl__c = 'https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=' + selectedImage ; update conToUpdate[0] ; } return null ; } }
4) Create a visualforce page with Label and Name "DisplayImages"
<apex:page id="pg" controller="DisplayImagesController" sidebar="false" showHeader="false"> <script> function validateRadio() { var elLength = document.getElementById('pg:frm:pb:pbs:pbsi:selectR'); var childInputElements = elLength.getElementsByTagName('input'); var flag = false ; for(var i=0; i < childInputElements.length; i++) { if(childInputElements[i].checked == true) { flag = true ; break } } if(!flag) { alert('Please select one Picture') ; return false ; } else { Validate() ; } } </script> <apex:form id="frm"> <apex:actionFunction name="Validate" action="{!SaveImage}" onComplete="alert('Picture is Applied'); window.close();" reRender=""/> <apex:pageBlock id="pb" title="Contact" tabStyle="Contact"> <apex:pageBlockButtons > <apex:commandButton rendered="{!IF(Items.size > 0 , true , false)}" value="Save" onClick="return validateRadio() ;"/> <apex:commandButton value="Cancel" onClick="window.close() ;"/> </apex:pageBlockButtons> <apex:pageBlockSection id="pbs" title="All Images" rendered="{!IF(Items.size > 0 , true , false)}"> <apex:pageBlockSectionItem id="pbsi"> <apex:selectRadio id="selectR" value="{!selectedImage}"> <apex:selectOptions id="MyRadio" value="{!Items}" /> </apex:selectRadio> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection rendered="{!IF(Items.size > 0 , false , true)}"> <apex:outputLabel value="No attachments to display or there is no valid image in attachments."/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
5) Create a Detail Page button with Label : "Set Profile Picture" and Name : "Set_Profile_Picture", Behavior : "Execute Java Script" and Content Source : "OnClick JavaScript"
window.showModalDialog('/apex/DisplayImages?Id={!Contact.Id}','') ; window.location.href = window.location.href ;
6) Edit the contact layout and add "Set Profile Picture" button.
7) Create a contact and add some images in notes and attachments, click on "Set Profile Picture". Select the image and click save.
*Note : I hate using JS with IE.
Also you may have to change a line of the Apex code above : "https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=' + selectedImage ;"
in "SaveImage()" method.
Above path can be different on different organizations, so you just need to make sure that this is correct by :
View the attachment uploaded and copy the path from the URL except the ID.You can also create a custom setting to store this path.
Once above steps are done we are all set to go.
Cheers.