Uploading a Document using Visualforce and a Custom Controller


<apex:page controller="FileUploadCls">
  <apex:sectionHeader title="Visualforce Example" subtitle="File Upload Example"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a File">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!fileupload}" value="Save"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!document.description}" id="description"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Keywords" for="keywords"/>
          <apex:inputText value="{!document.keywords}" id="keywords"/>
        </apex:pageBlockSectionItem>

      </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
</apex:page>


Public class FileUploadCls{
    public Document document{    get{if(document==null)        document=new Document();        return document;    }    Set;    }   
    public PageReference fileupload() {

    document.AuthorId = UserInfo.getUserId();    document.FolderId = UserInfo.getUserId();      try {
      insert document;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
      return null;
    } finally {
      document.body = null; // clears the viewstate      document = new Document();
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
    return null;
  }
}

Call Apex Class from Custom Button (JAVASCRIPT) in Salesforce



Step 1  :  Lets first create the Apex class

Things to Remember: 
  • Class needs to be a Global class 
  • and the method you intend to call from the javascript must be a WebService Method
e.g.

Global Class RG_LeadConversionCls {    

webservice static void LeadConvert(Id Leadid)
{       

}


Step 2 : Now to setup the custom Button.

  • Goto --> Setup --> Object --> Buttons, links and Actions section
  • Click New Button or Link
  • Enter the Name of the button
  • Behaviour : Execute Javascript
  • Content source : On-Click Javascript


{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

sforce.apex.execute("RG_LeadConversionCls","LeadConvertId",{Leadid:"{!Lead.Id}"});


Add or Remove buttons on a List View


Add or remove standard buttons

1A. For standard objects
  i.    From Setup, click Customize| then click the object you want to modify | click Search Layouts.
1B. For custom objects
  i.    From Setup, click Create | then click Objects.
  ii.   Click one of the custom objects in the list.
  iii.  Scroll down to the "Search Layouts" section.
2. Click Edit next to the List View layout.
3. Select the Buttons you want to add or remove.
4. Click Save.


Add or remove custom buttons

1A. For standard objects
  i.    From Setup, click Customize| then click the object you want to modify | click Search Layouts.
1B. For custom objects
  i.    From Setup, click Create | then click Objects.
  ii.   Click one of the custom objects in the list.
  iii.  Scroll down to the "Search Layouts" section.
2. Click Edit next to the List View layout
3. Highlight the values and click on the Add or Remove button to toggle the visibility of the button on the layout.
4. Click Save.