Upload Image and Update Image in Contact Detail Page

Requirement:

To Create  a Custom Button on Contact Detail Page when user click on it open a vf page from vf page attach img from local drive and save that img on Notes and Attachement and Populate last updted image on contact detail page:

Solution: 1 ) Create a Custom Button on Contact
2) Create A VF Page
3) Create a Formula Field on Contact.

Page and Class Code as Mention Below

Page Code


<apex:page standardController="Contact" extensions="uploadImageController" sidebar="false">


<apex:form >
<apex:pageMessages ></apex:pageMessages>
    <apex:pageBlock title="Attachment" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save"
            action="{!save}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection collapsible="false"  columns="1" >
            <apex:inputHidden value="{!Contact.Name}"/>
            <apex:outputText value="Attachment: "/><apex:inputFile value="{!bl}" filename="{!fileName}"/><br/>
        </apex:pageBlockSection>
    </apex:pageBlock>
   </apex:form>

</apex:page>


Class Code


public class uploadImageController{
      
    public Contact objContact{get;set;}
    public string contactID{get;set;}
    public string stringid{get;set;}
    
   public uploadImageController(ApexPages.StandardController controller) {
     objContact = new Contact();
      contactID = ApexPages.currentPage().getParameters().get('id'); 
    }
    
    public Attachment attach = new Attachment();
    public Blob bl {get; set;} 
    public String contentType {get; set;} 
    public String fileName {get; set;}
    
    public PageReference save(){
        attach.OwnerId = UserInfo.getUserId();
        attach.ParentId = contactID;
        attach.Name = fileName;
        attach.Body = bl;
        attach.ContentType = contentType;
        
       
        try {
             insert attach;
            if(attach.id!=null)
            {
              objContact = new Contact(id = contactID);
              objContact.Department= attach.id; // add Attach Id in Department field
            }
            update objContact;
   
        }
        
         catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error Choose File Name'));
            return null;
        } 
        
        finally {
        attach = new Attachment(); 
        }
        
      
         PageReference p= new PageReference('/' + contactID);
          p.setRedirect(true);
        
        return p;
    }

}


Formula Field:

IMAGE('https://c.ap1.content.force.com/servlet/servlet.FileDownload?file='+Department,'dd')








Thanks
Sumit Shukla
In case of any issue please feel free drop a mail 
sumitshukla.mca@gmail.com
voice call @ 9711055997




Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page