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.

Regular Expression for email validation using Apex in Salesforce.

Sample Regular Expression for email validation using Apex is given below


if(Pattern.matches('([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})', email)){
//success
}
else{
//error        
}

Cheers!!! Enjoy

Hide the Current Date Link on an Inputfield

The standard apex:inputfield component for a date field generates a link next to the input field, taking a lot of space. The link allows someone to easily enter the current date. However, when you use multiple date fields within a table component, it can be confusing for the user to have these extra links (see Screenshots). This recipe solves this by creating a Visualforce component that uses CSS to remove the link.

Visualforce Component Code

Here's the code for the Visualforce component. It wraps the component body in an HTML div tag, and adds styling to hide a part of that wrapped component.


<apex:component access="global">
<style>
    div.hideCurrDate span.dateInput span.dateFormat{
       display:none;
    }
</style>
<div class="hideCurrDate">
<apex:componentBody />
</div>
</apex:component>

How to use component

Here's a simple Visualforce page to demonstrate component usage:


<apex:page standardController="Opportunity">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>

<c:noDateLink>
<apex:inputField value="{!Opportunity.CloseDate}"/>
<c:noDateLink>


</apex:pageBlockSection>

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

Source: Cookbook
Cheers!!! Enjoy