Tag Archives: Salesforce

Add Fields in ApexPages.StandardController

If you want any fields value then you need add the field API name in standard controller.

public with sharing class AccountControllerExt
{
	public Account acc {get;set;}
	 
	public AccountControllerExt(ApexPages.StandardController stdController)
	{
		stdController.addFields(new List<String>{'Name', 'Region__c', 'TAX_ID_Number__c'});
		this.acc = (Account)stdController.getRecord();
	}
}

apex:inputfile ‘FileName’ and ‘Blob’ value being set null

Attachment Upload Controller:

public with sharing class AttachmentUploadController {
 
  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }
 
  public PageReference upload() {
 
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = '0010E00000CxfKm';//Record Id the file is attached to
    attachment.IsPrivate = true;
    attachment.ContentType = 'image/jpeg';
 
    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }
} 

Attachment Upload Visualforce Page:

<apex:page controller="AttachmentUploadController">  
	<apex:sectionHeader title="Example" subtitle="Attachment Upload Example"/>
	<apex:form>
	<apex:pageMessages />
		<apex:pageBlock title="Upload a Attachment">
			<apex:pageBlockButtons >
				<apex:commandButton action="{!upload}" value="Save"/>
			</apex:pageBlockButtons>
			<apex:pageBlockSection showHeader="false" columns="2">
				<apex:pageBlockSectionItem >
					<apex:outputLabel value="File Name" for="fileName"/>
					<apex:inputText value="{!attachment.name}" id="fileName"/>
				</apex:pageBlockSectionItem>
				<apex:pageBlockSectionItem >
					<apex:outputLabel value="File" for="file"/>
					<apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
				</apex:pageBlockSectionItem>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>  

Use enctype="multipart/form-data" in apex:form tag, when your form includes any apex:inputfile element.

<apex:form enctype="multipart/form-data">

apex:form tag support attributes

Attribute Description
accept The text to display as a tooltip
when the user’s mouse pointer hovers over this component.
acceptcharset A comma-separated list of
character encodings that a server processing this form can handle. If not
specified, this value defaults to “UNKNOWN”.
dir The direction in which the
generated HTML component should be read. Possible values include
“RTL” (right to left) or “LTR” (left to right).
enctype The content type used to submit the
form to the server. If not specified, this value defaults to
“application/x-www-form-urlencoded”.
forceSSL The form will be submitted using
SSL, regardless of whether the page itself was served with SSL. The default
is false. If the value is false, the form will be submitted using the same
protocol as the page. If forceSSL is set to true, when the form is submitted,
the page returned will use SSL.
id An identifier that allows the form
component to be referenced by other components in the page.
onclick The JavaScript invoked if the
onclick event occurs that is, if the user clicks the form.
ondblclick The JavaScript invoked if the
ondblclick event occurs that is, if the user clicks the form twice.
onkeydown The JavaScript invoked if the
onkeydown event occurs that is, if the user presses a keyboard key.
onkeypress The JavaScript invoked if the
onkeypress event occurs that is, if the user presses or holds down a keyboard
key.
onkeyup The JavaScript invoked if the
onkeyup event occurs that is, if the user releases a keyboard key.
onmousedown The JavaScript invoked if the
onmousedown event occurs that is, if the user clicks a mouse button.
onmousemove The JavaScript invoked if the
onmousemove event occurs that is, if the user moves the mouse pointer.
onmouseout The JavaScript invoked if the
onmouseout event occurs that is, if the user moves the mouse pointer away
from the form.
onmouseover The JavaScript invoked if the
onmouseover event occurs that is, if the user moves the mouse pointer over
the form.
onmouseup The JavaScript invoked if the
onmouseup event occurs that is, if the user releases the mouse button.
onreset The JavaScript invoked if the
onreset event occurs that is, if the user clicks the reset button on the
form.
onsubmit The JavaScript invoked if the
onsubmit event occurs that is, if the user clicks the submit button on the
form.
prependId A Boolean value that specifies
whether or not this form should prepend its ID to the IDs of its child
components during the clientid generation process. If not specified, the
value defaults to true.
rendered A Boolean value that specifies
whether the component is rendered on the page. If not specified, this value
defaults to true.
style The style used to display the form
component, used primarily for adding inline CSS styles.
styleClass The style class used to display
the form component, used primarily to designate which CSS styles are applied
when using an external CSS stylesheet.
target The name of the frame that
displays the response after the form is submitted. Possible values for this
attribute include “_blank”, “_parent”, “_self”,
and “_top”. You can also specify your own target names by assigning
a value to the name attribute of a desired destination.
title The text to display as a tooltip
when the user’s mouse pointer hovers over this component.

Upload Document Using Visualforce Page and Custom Controller

File Upload Controller:

public with sharing class FileUploadController {

  public Document document {
    get {
      if (document == null)
        document = new Document();
      return document;
    }
    set;
  }

  public PageReference upload() {

    document.AuthorId = UserInfo.getUserId();//Current User Id
    document.FolderId = UserInfo.getUserId();//Add Folder Id

    try {
      insert document;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
      return null;
    } finally {
      document.body = null;//Clear the View State
      document = new Document();
    }

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

File Upload Visualforce Page:

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

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

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

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

        <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>  

Output: