Activity Forums Salesforce® Discussions How can I upload contact image using Visualforce and Javascript in Salesforce?

  • Manpreet

    Member
    January 24, 2018 at 6:00 am

    Hi shashank,

    You can do something like this:

    public class uploadData{

    public String naam{get;set;}
    public ID folderid{get;set;}
    public Blob file{get;set;}

    public void insrt(){

    Document d= new Document();

    d.name = naam;
    d.body=file; // body field in document object which holds the file.
    d.folderid='00l90000000inFEAAY'; //folderid where the document will be stored insert d;
    insert d;

    Case cs = new Case();
    cs.FirstExample__Comments__c = '<img src="https://rustagiankit-developer-edition--c.ap1.content.force.com/servlet/servlet.FileDownload?file='+d.id+'" width="500" height="281"></img>'; //FirstExample is namespace & Comments__c is Rich Text Area field
    cs.Status = 'New';
    cs.Origin = 'Web';
    insert cs;
    }
    }
    *************************************
    ***************VF page***************

    <apex:page controller="uploadData">
    <apex:form>
    <apex:outputLabel value="Document Name"></apex:outputLabel>
    <apex:inputText id="name" value="{!naam}"/>

    <apex:outputLabel value="Upload Document"></apex:outputLabel>
    <apex:inputfile value="{!file}"></apex:inputfile>

    <apex:commandButton value="Save" action="{!insrt}" id="save"/>
    </apex:form>
    </apex:page>
    **************************************
    NOTE :- If you have a existing image stored in the Document, then you can only refer to below mentioned snippet of code only :-
    Case cs = new Case();
    cs.FirstExample__Comments__c = '<img src="https://xyz-developer-edition--c.ap7.content.force.com/servlet/servlet.FileDownload?file=01590000000QPTY" width="400" height="320"></img>';
    cs.Status = 'New';
    cs.Origin = 'Web';
    insert cs;

    This is for Case object.You can do similar with Contact Object.

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos