<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jeff Douglas - Technology, Coding and Bears... OH MY! &#187; Visualforce</title>
	<atom:link href="http://blog.jeffdouglas.com/category/technology/visualforce-technology/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jeffdouglas.com</link>
	<description>Get your head out of your #@! and into the clouds!</description>
	<lastBuildDate>Fri, 10 Sep 2010 10:06:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Why is this so DOM Hard?</title>
		<link>http://blog.jeffdouglas.com/2010/08/11/why-is-this-so-dom-hard/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=why-is-this-so-dom-hard</link>
		<comments>http://blog.jeffdouglas.com/2010/08/11/why-is-this-so-dom-hard/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 12:27:01 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=3052</guid>
		<description><![CDATA[Let me start out right away by saying I am not a DOM or CSS master. I&#8217;ve typically had employees or co-workers to perform these functions for my so I&#8217;ve (unfortunately) let me skills wane. However, I&#8217;m not totally clueless. I was trying to build the following Visualforce page a couple of days ago and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F08%2F11%2Fwhy-is-this-so-dom-hard%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F08%2F11%2Fwhy-is-this-so-dom-hard%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Let me start out right away by saying I am not a DOM or CSS master. I&#8217;ve typically had employees or co-workers to perform these functions for my so I&#8217;ve (unfortunately) let me skills wane. However, I&#8217;m not totally clueless. I was trying to build the following Visualforce page a couple of days ago and working with the DOM and Visualforce components seems to be much harder than what it should be. Perhaps I&#8217;m doing something wrong? Luckily <a href="http://th3silverlining.com" target="_blank">Wes Nolte</a> and <a href="http://d3developer.com/" target="_blank">Joel Dietz</a> <strong>are</strong> experts and they both have a number of great blog posts on this subject.</p>
<p>I was trying to create the following Visualforce page that contains a simple SelectList (I&#8217;ve sanitized the code for your protection).</p>
<p><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/08/dom1.png" alt="" width="419" height="41" /></p>
<p>When the user submits the form, the value of the currently selected  option is fired off via JavaScript. Shouldn&#8217;t be a big deal, right? Based upon the Visualforce docs for <a href="http://www.salesforce.com/us/developer/docs/pages/Content/pages_access.htm?SearchType=Stem&amp;Highlight=DOM" target="_blank">Using JavaScript to Reference Components</a>, I thought this code would work using the $Component variable (line #5).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;MyController&quot;</span> showHeader<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePage&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span>
  function doSubmit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span>document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{!$Component.theSelectList}&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;theForm&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageblock id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePageBlock&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageblockSection id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePageBlockSection&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePageBlockItem&quot;</span><span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectList value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!selectedContentId}&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;theSelectList&quot;</span><span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectOptions value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contentOptions}&quot;</span><span style="color: #339933;">/&gt;</span>      
          <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>selectList<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Submit&quot;</span> onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSubmit()&quot;</span> <span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageblockSection<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageblock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Unfortunately Firebug choked with s null pointer for the object&#8217;s reference.</p>
<p><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/08/dom2.png" alt="" width="459" height="153" /></p>
<p>I found Wes&#8217; great post, <a href="http://th3silverlining.com/2009/06/17/visualforce-component-ids-javascript/" target="_blank">VisualForce Component Ids &amp; Javascript</a>, and took a look at the element Id that Salesforce generated for the SelectList component. Based upon the other elements in the DOM hierarchy, I hard-coded my JavaScript to use the id that Salesforce was generating and the method worked correctly!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span>
  function doSubmit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span>document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;thePage:theForm:thePageBlock:thePageBlockSection:thePageBlockItem:theSelectList&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/08/dom3.png" alt="" width="570" height="156" /></p>
<p>So now I was confused. I dug deeper into Wes&#8217; post, the code and comments. Wes&#8217; solution was to assign the element value to a JavaScript variable just after it appears on the page (or at the same level within the page tree). My final (working) code looks like the following. Notice line #16 where I set the local JavaScript variable and then line #5 where I reference it&#8217;s currently selected value.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;MyController&quot;</span> showHeader<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePage&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span> 
  function doSubmit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span>options.<span style="color: #006633;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;theForm&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageblock id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePageBlock&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageblockSection id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePageBlockSection&quot;</span><span style="color: #339933;">&gt;</span>
         <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;thePageBlockItem&quot;</span><span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectList value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!selectedContentId}&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;theSelectList&quot;</span><span style="color: #339933;">&gt;</span>
              <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectOptions value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contentOptions}&quot;</span><span style="color: #339933;">/&gt;</span>       
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>selectList<span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span> var options <span style="color: #339933;">=</span> document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{!$Component.theSelectList}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
         <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Submit&quot;</span> onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSubmit()&quot;</span> <span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageblockSection<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageblock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span> 
&nbsp;
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Wes states that this solution is simple as well as flexible. However, what happens if you have 20+ form fields that you need to process in this manner? Does accessing the DOM seem too hard with Visualforce components? Let me know if I am doing something wrong?</p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/08/11/why-is-this-so-dom-hard/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Email a Document with Salesforce.com</title>
		<link>http://blog.jeffdouglas.com/2010/07/22/create-and-email-a-document-with-salesforce-com/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=create-and-email-a-document-with-salesforce-com</link>
		<comments>http://blog.jeffdouglas.com/2010/07/22/create-and-email-a-document-with-salesforce-com/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 10:39:28 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2895</guid>
		<description><![CDATA[After my last post, Create and Email a PDF with Salesforce.com, I received a few comments whether it was possible to do the same with Document stored in Salesforce.com. Could you choose a Document and then send it via email as an attachment? The short answer is yes! However, I tried to do it from [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F22%2Fcreate-and-email-a-document-with-salesforce-com%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F22%2Fcreate-and-email-a-document-with-salesforce-com%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p style="clear: both">After my last post, <a href="http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/" target="_blank">Create and Email a PDF with Salesforce.com</a>, I received a few comments whether it was possible to do the same with Document stored in Salesforce.com. Could you choose a Document and then send it via email as an attachment? The short answer is yes! However, I tried to do it from a Force.com Sites page but was not able to get it to work. The document is not marked for internal use and it is marked as an externally available image. I also made sure that the public settings for my Site included read access to documents but still the query for the document returns no results. No time to look at it in depth right now so if anyone has an idea, please send it my way.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/2010/07/22/create-and-email-a-document-with-salesforce-com/email-doc/" rel="attachment wp-att-2896"><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/email-doc.png" alt="" title="email-doc" width="500" class="alignnone size-full wp-image-2896" /></a></p>
<p style="clear: both"><strong>DocumentEmailer Visualforce Page</strong></p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;DocumentEmailController&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>sectionHeader title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Document Example&quot;</span> subtitle<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Email a Document&quot;</span> 
    description<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Example of how to email a Document.&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form <span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageMessages <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Document Input&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockButtons <span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandButton action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!sendDoc}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Send Document&quot;</span><span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockButtons<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSection <span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Email to send to&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!email}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Document&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;document&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectList value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!documentId}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;document&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
                 <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectOptions value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!documents}&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>selectList<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSection<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p style="clear: both"><strong>DocumentEmailController</strong></p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> with sharing <span style="color: #000000; font-weight: bold;">class</span> DocumentEmailController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ID documentId <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> email <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>SelectOption<span style="color: #339933;">&gt;</span> documents <span style="color: #009900;">&#123;</span>
    get <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>documents <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        documents <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>SelectOption<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        documents.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SelectOption<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'01570000001NZDn'</span>,<span style="color: #0000ff;">'Cup of Coffee? - DOC'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        documents.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SelectOption<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'01570000001NZDi'</span>,<span style="color: #0000ff;">'Workflow Cheatsheet -  PDF'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> documents<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> PageReference sendDoc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399;">Document</span> doc <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>select id, name, body, contenttype, developername, type 
      from <span style="color: #003399;">Document</span> where id <span style="color: #339933;">=</span> <span style="color: #339933;">:</span>documentId<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    Messaging.<span style="color: #006633;">EmailFileAttachment</span> attach <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">EmailFileAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span>doc.<span style="color: #006633;">contentType</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">setFileName</span><span style="color: #009900;">&#40;</span>doc.<span style="color: #006633;">developerName</span><span style="color: #339933;">+</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">+</span>doc.<span style="color: #006633;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">setInline</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">Body</span> <span style="color: #339933;">=</span> doc.<span style="color: #006633;">Body</span><span style="color: #339933;">;</span>
&nbsp;
    Messaging.<span style="color: #006633;">SingleEmailMessage</span> mail <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">SingleEmailMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setUseSignature</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setToAddresses</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> email <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Document Email Demo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setHtmlBody</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Here is the email you requested: '</span><span style="color: #339933;">+</span>doc.<span style="color: #006633;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setFileAttachments</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">EmailFileAttachment</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> attach <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// Send the email</span>
    Messaging.<span style="color: #006633;">sendEmail</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">SingleEmailMessage</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> mail <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">Message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">Severity</span>.<span style="color: #006633;">INFO</span>, <span style="color: #0000ff;">'Email with Document sent to '</span><span style="color: #339933;">+</span>email<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p style="clear: both"><strong>Test Class</strong></p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@isTest
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> Test_DocumentEmailer <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Document</span> document<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    document <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Document</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">Body</span> <span style="color: #339933;">=</span> <span style="color: #003399;">Blob</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Some Text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">ContentType</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'application/pdf'</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">DeveloperName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'my_document'</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">IsPublic</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    document.<span style="color: #003399;">Name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'My Document'</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">FolderId</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>select id from folder where name <span style="color: #339933;">=</span> <span style="color: #0000ff;">'My Test Docs'</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">id</span><span style="color: #339933;">;</span>
    insert document<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> testMethod <span style="color: #000066; font-weight: bold;">void</span> testDocumentEmailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    PageReference pref <span style="color: #339933;">=</span> Page.<span style="color: #006633;">DocumentEmailer</span><span style="color: #339933;">;</span>        
    DocumentEmailController con <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DocumentEmailController<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
    Test.<span style="color: #006633;">startTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>,con.<span style="color: #006633;">documents</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// populate the field with values</span>
    con.<span style="color: #006633;">documentId</span> <span style="color: #339933;">=</span> document.<span style="color: #006633;">id</span><span style="color: #339933;">;</span>
    con.<span style="color: #006633;">email</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test@noemail.com'</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// submit the request</span>
    pref <span style="color: #339933;">=</span> con.<span style="color: #006633;">sendDoc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Test.<span style="color: #006633;">stopTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><br class="final-break" style="clear: both" /></p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/22/create-and-email-a-document-with-salesforce-com/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Create and Email a PDF with Salesforce.com</title>
		<link>http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=create-and-email-a-pdf-with-salesforce-com</link>
		<comments>http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 12:03:15 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2883</guid>
		<description><![CDATA[This is a continuation of my post a couple of days ago, Attach a PDF to a Record in Salesforce, and shows how to dynamically generate a PDF and attach it to an email. The code is fairly similar and has the same issue with testing the PageReference getContent() method. You can run this demo [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F16%2Fcreate-and-email-a-pdf-with-salesforce-com%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F16%2Fcreate-and-email-a-pdf-with-salesforce-com%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p style="clear: both">This is a continuation of my post a couple of days ago, <a href="http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/" target="_blank">Attach a PDF to a Record in Salesforce</a>, and shows how to dynamically generate a PDF and attach it to an email. The code is fairly similar and has the same issue with testing the PageReference getContent() method.</p>
<p style="clear: both">
<h3 style="text-decoration:underline; clear: both"><a href="https://jeffdouglas-developer-edition.na5.force.com/examples/PdfEmailer" target="_blank">You can run this demo at my developer site.</a></h3>
</p>
<p style="clear: both"><a href="https://jeffdouglas-developer-edition.na5.force.com/examples/PdfEmailer"><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/email-pdf.png" alt="" title="email-pdf" width="500" height="237" class="alignnone size-full wp-image-2889" /></a>
<p><strong>PdfEmailer Visualforce Page</strong></p>
<p style="clear: both">The Visualforce page simply allows the user to enter their email address and select a sample Account from the picklist. This is Account that is passed to the PdfGeneratorTemplate Visualforce page to generate the PDF.</p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PdfEmailController&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>sectionHeader title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PDF Example&quot;</span> subtitle<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Email a PDF&quot;</span> 
    description<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Example of how to email a dynamically generated PDF.&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form <span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageMessages <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PDF Input&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockButtons <span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandButton action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!sendPdf}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Send PDF&quot;</span><span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockButtons<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSection <span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Email to send to&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!email}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Account&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;account&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectList value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!accountId}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;account&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
                 <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>selectOptions value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!accounts}&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>selectList<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSection<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p style="clear: both"><strong>PdfEmailerController</strong></p>
<p style="clear: both">The Controller passes the Account ID that the user entered as a parameter for the Visualforce page being generated. It let creates the attachment from the PDF content. The Body of the attachment uses the Blob returned from the PageReference’s getContent method. You could also use the getContentAsPDF method which always returns the page as a PDF, regardless of the <apex:page> component’s renderAs attribute. However, this method always seems to throw an error in the test class. See the <a href="http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm" target="_blank">PageReference documentation</a> for more info. The method then constructs the SimpleEmailMessage object and then sends it on its way to the recipient&#8217;s inbox.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> with sharing <span style="color: #000000; font-weight: bold;">class</span> PdfEmailController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ID accountId <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> email <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>SelectOption<span style="color: #339933;">&gt;</span> accounts <span style="color: #009900;">&#123;</span>
    get <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>accounts <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        accounts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>SelectOption<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        accounts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SelectOption<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'0017000000LgRMb'</span>,<span style="color: #0000ff;">'United Oil &amp; Gas Corp.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        accounts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SelectOption<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'0017000000LgRMV'</span>,<span style="color: #0000ff;">'Burlington Textiles Corp of America'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> accounts<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> PageReference sendPdf<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    PageReference pdf <span style="color: #339933;">=</span> Page.<span style="color: #006633;">PdfGeneratorTemplate</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// add parent id to the parameters for standardcontroller</span>
    pdf.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span>,accountId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// the contents of the attachment from the pdf</span>
    <span style="color: #003399;">Blob</span> body<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// returns the output of the page as a PDF</span>
      body <span style="color: #339933;">=</span> pdf.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// need to pass unit test -- current bug  </span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>VisualforceException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      body <span style="color: #339933;">=</span> <span style="color: #003399;">Blob</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Some Text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    Messaging.<span style="color: #006633;">EmailFileAttachment</span> attach <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">EmailFileAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'application/pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">setFileName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'testPdf.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">setInline</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">Body</span> <span style="color: #339933;">=</span> body<span style="color: #339933;">;</span>
&nbsp;
    Messaging.<span style="color: #006633;">SingleEmailMessage</span> mail <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">SingleEmailMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setUseSignature</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setToAddresses</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> email <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PDF Email Demo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setHtmlBody</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Here is the email you requested! Check the attachment!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mail.<span style="color: #006633;">setFileAttachments</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">EmailFileAttachment</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> attach <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// Send the email</span>
    Messaging.<span style="color: #006633;">sendEmail</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Messaging.<span style="color: #006633;">SingleEmailMessage</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> mail <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">Message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">Severity</span>.<span style="color: #006633;">INFO</span>, <span style="color: #0000ff;">'Email with PDF sent to '</span><span style="color: #339933;">+</span>email<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>PdfGeneratorTemplate Visualforce Page</strong>
<p>This is the Visualforce page that is generated in the Controller. It simply uses the StandardController and displays the Account name for the ID passed to it.
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page standardController<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Account&quot;</span> renderAs<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pdf&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Congratulations<span style="color: #339933;">!!&lt;/</span>h1<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>You created a PDF <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">!</span>account.<span style="color: #006633;">name</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p><strong>Test Class</strong>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@isTest
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> Test_PdfEmailController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> Account account<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    account <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Account<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    account.<span style="color: #003399;">Name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Test Account'</span><span style="color: #339933;">;</span>
    insert account<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> testMethod <span style="color: #000066; font-weight: bold;">void</span> testPdfEmailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    PageReference pref <span style="color: #339933;">=</span> Page.<span style="color: #006633;">PdfEmailer</span><span style="color: #339933;">;</span>
    pref.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span>,account.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Test.<span style="color: #006633;">setCurrentPage</span><span style="color: #009900;">&#40;</span>pref<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    PdfEmailController con <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PdfEmailController<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
    Test.<span style="color: #006633;">startTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>,con.<span style="color: #006633;">accounts</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// populate the field with values</span>
    con.<span style="color: #006633;">accountId</span> <span style="color: #339933;">=</span> account.<span style="color: #006633;">id</span><span style="color: #339933;">;</span>
    con.<span style="color: #006633;">email</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test@noemail.com'</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// submit the record</span>
    pref <span style="color: #339933;">=</span> con.<span style="color: #006633;">sendPdf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Test.<span style="color: #006633;">stopTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>
</p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Attach a PDF to a Record in Salesforce</title>
		<link>http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=attach-a-pdf-to-a-record-in-salesforce</link>
		<comments>http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 13:57:33 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2854</guid>
		<description><![CDATA[Salesforce.com makes it extremely easy to generate PDF documents on the fly by simply using the renderAs=&#8221;pdf&#8221; attribute for the &#60;apex:page&#62; component. It&#8217;s also a snap to attach these PDFs to records as Attachments. Below is a small Visualforce page and Controller that generates a PDF and saves it to an Account. Note: there is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F14%2Fattach-a-pdf-to-a-record-in-salesforce%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F14%2Fattach-a-pdf-to-a-record-in-salesforce%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Salesforce.com makes it extremely easy to generate PDF documents on the fly by simply using the renderAs=&#8221;pdf&#8221; attribute for the &lt;apex:page&gt; component. It&#8217;s also a snap to attach these PDFs to records as Attachments. Below is a small Visualforce page and Controller that generates a PDF and saves it to an Account.</p>
<p><strong>Note:</strong> there is a small issue when it comes to testing the Controller. Salesforce currently throws an error (Salesforce.com Error &#8220;Unable to retrieve object&#8221;) when getContent or getContentAsPDF is called from a test method. There&#8217;s an <a href="http://sites.force.com/ideaexchange/ideaView?c=09a30000000D9xt&#038;id=08730000000HzknAAC" target="_blank">Idea to make this work</a> properly. I encourage everyone to vote for this.</p>
<p><a href="http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/attach-pdf/" rel="attachment wp-att-2867"><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/attach-pdf.png" alt="" title="attach-pdf" width="500" class="alignnone size-full wp-image-2867" /></a></p>
<p><strong>PdfGenerator Visualforce Page</strong></p>
<p>The Visualforce page allows the users to enter the ID of the Account to attach the PDF to as well as the name of the PDF. You could just have easily used a Controller Extension instead of entering the ID for the Account but I went this route for simplicity.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PdfGeneratorController&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>sectionHeader title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PDF Example&quot;</span> subtitle<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Attach a PDF&quot;</span> 
    description<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Example of how to attach a PDF to a record.&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form <span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PDF Input&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockButtons <span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandButton action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!savePdf}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Save PDF&quot;</span><span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockButtons<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageMessages <span style="color: #339933;">/&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSection <span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;File Name&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pdfName&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!pdfName}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pdfName&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Account ID&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!parentId}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSection<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p><strong>PdfGeneratorController Custom Controller</strong></p>
<p>The Controller passes the Account ID that the user entered as a parameter for the Visualforce page being generated. It then creates a new Attachment object and sets some attributes. It sets the ParentId to the value of the Account ID that the user entered so that the PDF is attached to that record. The Body of the attachment uses the Blob returned from the PageReference&#8217;s getContent method. You could also use the getContentAsPDF method which always returns the page as a PDF, regardless of the &lt;apex:page&gt; component&#8217;s renderAs attribute. However, this method <strong>always</strong> seems to throw an error in the test class. See the <a href="http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm" target="_blank">PageReference documentation</a> for more info. The method then redirects the user to the Account page so they can view the PDF attachment.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> with sharing <span style="color: #000000; font-weight: bold;">class</span> PdfGeneratorController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ID parentId <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> pdfName <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> PageReference savePdf<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    PageReference pdf <span style="color: #339933;">=</span> Page.<span style="color: #006633;">PdfGeneratorTemplate</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// add parent id to the parameters for standardcontroller</span>
    pdf.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span>,parentId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// create the new attachment</span>
    Attachment attach <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Attachment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// the contents of the attachment from the pdf</span>
    <span style="color: #003399;">Blob</span> body<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// returns the output of the page as a PDF</span>
    	body <span style="color: #339933;">=</span> pdf.<span style="color: #006633;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// need to pass unit test -- current bug	</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>VisualforceException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	body <span style="color: #339933;">=</span> <span style="color: #003399;">Blob</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Some Text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    attach.<span style="color: #006633;">Body</span> <span style="color: #339933;">=</span> body<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// add the user entered name</span>
    attach.<span style="color: #003399;">Name</span> <span style="color: #339933;">=</span> pdfName<span style="color: #339933;">;</span>
    attach.<span style="color: #006633;">IsPrivate</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// attach the pdf to the account</span>
    attach.<span style="color: #006633;">ParentId</span> <span style="color: #339933;">=</span> parentId<span style="color: #339933;">;</span>
    insert attach<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// send the user to the account to view results</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> PageReference<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">+</span>parentId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>PdfGeneratorTemplate Visualforce Page</strong></p>
<p>This is the Visualforce page that is generated in the Controller. It simply uses the StandardController and displays the Account name for the ID passed to it.
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page standardController<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Account&quot;</span> renderAs<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pdf&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>Congratulations<span style="color: #339933;">!!&lt;/</span>h1<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>You created a PDF <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#123;</span><span style="color: #339933;">!</span>account.<span style="color: #006633;">name</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p><b>Test Class</b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">@isTest
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> Test_PdfGeneratorController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> Account account<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    account <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Account<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    account.<span style="color: #003399;">Name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Test Account'</span><span style="color: #339933;">;</span>
    insert account<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> testMethod <span style="color: #000066; font-weight: bold;">void</span> testPdfGenerator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    PageReference pref <span style="color: #339933;">=</span> Page.<span style="color: #006633;">PdfGenerator</span><span style="color: #339933;">;</span>
    pref.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span>,account.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Test.<span style="color: #006633;">setCurrentPage</span><span style="color: #009900;">&#40;</span>pref<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    PdfGeneratorController con <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PdfGeneratorController<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
    Test.<span style="color: #006633;">startTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// populate the field with values</span>
    con.<span style="color: #006633;">parentId</span> <span style="color: #339933;">=</span> account.<span style="color: #006633;">id</span><span style="color: #339933;">;</span>
    con.<span style="color: #006633;">pdfName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'My Test PDF'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// submit the record</span>
    pref <span style="color: #339933;">=</span> con.<span style="color: #006633;">savePdf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// assert that they were sent to the correct page</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span>pref.<span style="color: #006633;">getUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<span style="color: #0000ff;">'/'</span><span style="color: #339933;">+</span>account.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// assert that an attachment exists for the record</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #009900;">&#91;</span>select count<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from attachment where parentId <span style="color: #339933;">=</span> <span style="color: #339933;">:</span>account.<span style="color: #006633;">id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Test.<span style="color: #006633;">stopTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>
</p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Building a Dynamic Search Page in Visualforce</title>
		<link>http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=building-a-dynamic-search-page-in-visualforce</link>
		<comments>http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 10:31:34 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2778</guid>
		<description><![CDATA[I brushed this code off and thought it might be useful to someone as a starting point for a dynamic search page. It has some cool functionality including passing search criteria via Javascript to the controller, search as you type, sorting of results by clicking on the column header plus much more. Hope you find [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F13%2Fbuilding-a-dynamic-search-page-in-visualforce%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F07%2F13%2Fbuilding-a-dynamic-search-page-in-visualforce%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I brushed this code off and thought it might be useful to someone as a starting point for a dynamic search page. It has some cool functionality including passing search criteria via Javascript to the controller, search as you type, sorting of results by clicking on the column header plus much more. Hope you find it useful.</p>
<h3 style="text-decoration:underline"><a href="https://jeffdouglas-developer-edition.na5.force.com/examples/CustomerSearch" target="_blank"><font class="Apple-style-span" color="#000000">You can run this demo at my developer site.</font></a></h3>
<p><a href="https://jeffdouglas-developer-edition.na5.force.com/examples/CustomerSearch"><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/customer-search.png" alt="" title="customer-search" width="500" class="alignnone size-full wp-image-2782" /></a></p>
<p>Some of the interesting features of the Visualforce page and Apex controller include:
<ul>
<li>Instead of the typical overhead of using getters/setters in the controller to maintain the variables for the search form, I used <a href="http://blog.sforce.com/sforce/2009/10/passing-javascript-values-to-apex-controller.html" target="_blank">Dave Carroll&#8217;s blog</a> as an example to pass the variables via Javascript.</li>
<li>Since I&#8217;m using Javascript to pass my parameters, I can get fancy and make the search form more dynamic by running the search as the user types or selects an option in the picklist. No submit button is required.</li>
<li>Users can click on the column headers to toggle the direction of the search results by using a CommandLink component and passing the column name.</li>
<li>Build the picklist using Dynamic Apex&#8217;s <a href="http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm" target="_blank">Describe functionality</a> so that it&#8217;s maintenance free.</li>
<li>Perform the search against multiple fields including multi-select picklists using dynamic SOQL and preventing SOQL injection attacks.</li>
</ul>
<p><b>ContactSearchController</b></p>
<p>Take a look at the Apex code below. The search interaction is split up between two separate but integral parts. When the search is fired from the Visualforce page via Javascript, the SOQL is constructed in the runSearch() method and is then passed to the runQuery() method to execute. The SOQL string is persisted so that when the user clicks on the table column the same SOQL can be issued again in the toggleSort() method but ordered by a different field name and sort direction.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> with sharing <span style="color: #000000; font-weight: bold;">class</span> ContactSearchController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// the soql without the order and limit</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> soql <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">// the collection of contacts to display</span>
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Contact<span style="color: #339933;">&gt;</span> contacts <span style="color: #009900;">&#123;</span>get<span style="color: #339933;">;</span>set<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// the current sort direction. defaults to asc</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> sortDir <span style="color: #009900;">&#123;</span>
    get  <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sortDir <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  sortDir <span style="color: #339933;">=</span> <span style="color: #0000ff;">'asc'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">return</span> sortDir<span style="color: #339933;">;</span>  <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// the current field to sort by. defaults to last name</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> sortField <span style="color: #009900;">&#123;</span>
    get  <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sortField <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>sortField <span style="color: #339933;">=</span> <span style="color: #0000ff;">'lastName'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">return</span> sortField<span style="color: #339933;">;</span>  <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// format the soql for display on the visualforce page</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> debugSoql <span style="color: #009900;">&#123;</span>
    get <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> soql <span style="color: #339933;">+</span> <span style="color: #0000ff;">' order by '</span> <span style="color: #339933;">+</span> sortField <span style="color: #339933;">+</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">+</span> sortDir <span style="color: #339933;">+</span> <span style="color: #0000ff;">' limit 20'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// init the controller and display some sample data when the page loads</span>
  <span style="color: #000000; font-weight: bold;">public</span> ContactSearchController<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    soql <span style="color: #339933;">=</span> <span style="color: #0000ff;">'select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null'</span><span style="color: #339933;">;</span>
    runQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// toggles the sorting of query from asc&lt;--&gt;desc</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> toggleSort<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// simply toggle the direction</span>
    sortDir <span style="color: #339933;">=</span> sortDir.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'asc'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">'desc'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'asc'</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// run the query again</span>
    runQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// runs the actual query</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> runQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      contacts <span style="color: #339933;">=</span> Database.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span>soql <span style="color: #339933;">+</span> <span style="color: #0000ff;">' order by '</span> <span style="color: #339933;">+</span> sortField <span style="color: #339933;">+</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">+</span> sortDir <span style="color: #339933;">+</span> <span style="color: #0000ff;">' limit 20'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">Message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">Severity</span>.<span style="color: #006633;">ERROR</span>, <span style="color: #0000ff;">'Ooops!'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// runs the search with parameters passed via Javascript</span>
  <span style="color: #000000; font-weight: bold;">public</span> PageReference runSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399;">String</span> firstName <span style="color: #339933;">=</span> Apexpages.<span style="color: #006633;">currentPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> lastName <span style="color: #339933;">=</span> Apexpages.<span style="color: #006633;">currentPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lastname'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> accountName <span style="color: #339933;">=</span> Apexpages.<span style="color: #006633;">currentPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'accountName'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> technology <span style="color: #339933;">=</span> Apexpages.<span style="color: #006633;">currentPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'technology'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    soql <span style="color: #339933;">=</span> <span style="color: #0000ff;">'select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>firstName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      soql <span style="color: #339933;">+=</span> <span style="color: #0000ff;">' and firstname LIKE <span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">+</span><span style="color: #003399;">String</span>.<span style="color: #006633;">escapeSingleQuotes</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">'%<span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>lastName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      soql <span style="color: #339933;">+=</span> <span style="color: #0000ff;">' and lastname LIKE <span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">+</span><span style="color: #003399;">String</span>.<span style="color: #006633;">escapeSingleQuotes</span><span style="color: #009900;">&#40;</span>lastName<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">'%<span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>accountName.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      soql <span style="color: #339933;">+=</span> <span style="color: #0000ff;">' and account.name LIKE <span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">+</span><span style="color: #003399;">String</span>.<span style="color: #006633;">escapeSingleQuotes</span><span style="color: #009900;">&#40;</span>accountName<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">'%<span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">;</span>  
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>technology.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      soql <span style="color: #339933;">+=</span> <span style="color: #0000ff;">' and interested_technologies__c includes (<span style="color: #000099; font-weight: bold;">\'</span>'</span><span style="color: #339933;">+</span>technology<span style="color: #339933;">+</span><span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\'</span>)'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// run the query again</span>
    runQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// use apex describe to build the picklist values</span>
  <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> technologies <span style="color: #009900;">&#123;</span>
    get <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>technologies <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        technologies <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Schema.<span style="color: #006633;">DescribeFieldResult</span> field <span style="color: #339933;">=</span> Contact.<span style="color: #006633;">interested_technologies__c</span>.<span style="color: #006633;">getDescribe</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Schema.<span style="color: #006633;">PicklistEntry</span> f <span style="color: #339933;">:</span> field.<span style="color: #006633;">getPicklistValues</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
          technologies.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">getLabel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> technologies<span style="color: #339933;">;</span>          
    <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><b>CustomerSearch Visualforce Page</b></p>
<p>The Visualforce page has three sections: 1) a search form, 2) a results blockTable and 3) a debug panel displaying the SOQL that was executed. </p>
<p>The search form has a number of fields that fire the Javascript search using the onkeyup and onchange events. Instead of each form field passing the current values to actionFunction&#8217;s Javascript method, for ease of use, each form field calls the doSearch() function that gathers up the values and submits them. When the actionFunction renders it creates a Javascript function that POSTs the values to the controller in the same manner as CommandLink or CommandButton. </p>
<p>The search results BlockTable is rerendered when the search is submitted to the controller so that the results display properly. Users can click on the column headers to reorder the results of the query. This uses a CommandLink to pass the sort field to the controller and toggle the sort direction.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ContactSearchController&quot;</span> sidebar<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form <span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageMessages id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;errors&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Find Me A Customer!&quot;</span> mode<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;edit&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>table width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;100%&quot;</span> border<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>  
    <span style="color: #339933;">&lt;</span>td width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;200&quot;</span> valign<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;top&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Parameters&quot;</span> mode<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;edit&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;criteria&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
      function doSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        searchServer<span style="color: #009900;">&#40;</span>
          document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;firstName&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span>,
          document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastName&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span>,
          document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;accountName&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span>,
          document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;technology&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">options</span><span style="color: #009900;">&#91;</span>document.<span style="color: #006633;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;technology&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">selectedIndex</span><span style="color: #009900;">&#93;</span>.<span style="color: #006633;">value</span>
          <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span> 
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>actionFunction name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;searchServer&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!runSearch}&quot;</span> rerender<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;results,debug,errors&quot;</span><span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;firstName&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lastName&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;accountName&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;technology&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>actionFunction<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>table cellpadding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span> cellspacing<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>td style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;font-weight:bold;&quot;</span><span style="color: #339933;">&gt;</span>First Name<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;firstName&quot;</span> onkeyup<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSearch();&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>td style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;font-weight:bold;&quot;</span><span style="color: #339933;">&gt;</span>Last Name<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lastName&quot;</span> onkeyup<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSearch();&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>td style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;font-weight:bold;&quot;</span><span style="color: #339933;">&gt;</span>Account<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;accountName&quot;</span> onkeyup<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSearch();&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>td style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;font-weight:bold;&quot;</span><span style="color: #339933;">&gt;</span>Interested Technologies<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>select id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;technology&quot;</span> onchange<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSearch();&quot;</span><span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">&gt;&lt;/</span>option<span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>repeat value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!technologies}&quot;</span> var<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tech&quot;</span><span style="color: #339933;">&gt;</span>
              <span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!tech}&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #339933;">!</span>tech<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>repeat<span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>td valign<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;top&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock mode<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;edit&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockTable value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contacts}&quot;</span> var<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contact&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>column <span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>facet name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;header&quot;</span><span style="color: #339933;">&gt;</span>
                    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandLink value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;First Name&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!toggleSort}&quot;</span> rerender<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;results,debug&quot;</span><span style="color: #339933;">&gt;</span>
                        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sortField&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;firstName&quot;</span> assignTo<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!sortField}&quot;</span><span style="color: #339933;">/&gt;</span>
                    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>commandLink<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>facet<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.firstName}&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>column<span style="color: #339933;">&gt;</span>
&nbsp;
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>column <span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>facet name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;header&quot;</span><span style="color: #339933;">&gt;</span>
                    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandLink value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Last Name&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!toggleSort}&quot;</span> rerender<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;results,debug&quot;</span><span style="color: #339933;">&gt;</span>
                        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sortField&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lastName&quot;</span> assignTo<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!sortField}&quot;</span><span style="color: #339933;">/&gt;</span>
                    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>commandLink<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>facet<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.lastName}&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>column<span style="color: #339933;">&gt;</span>
&nbsp;
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>column <span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>facet name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;header&quot;</span><span style="color: #339933;">&gt;</span>
                    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandLink value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Account&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!toggleSort}&quot;</span> rerender<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;results,debug&quot;</span><span style="color: #339933;">&gt;</span>
                        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sortField&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;account.name&quot;</span> assignTo<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!sortField}&quot;</span><span style="color: #339933;">/&gt;</span>
                    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>commandLink<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>facet<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.account.name}&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>column<span style="color: #339933;">&gt;</span>
&nbsp;
            <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>column <span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>facet name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;header&quot;</span><span style="color: #339933;">&gt;</span>
                    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandLink value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Technologies&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!toggleSort}&quot;</span> rerender<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;results,debug&quot;</span><span style="color: #339933;">&gt;</span>
                        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>param name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sortField&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;interested_technologies__c&quot;</span> assignTo<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!sortField}&quot;</span><span style="color: #339933;">/&gt;</span>
                    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>commandLink<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>facet<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.Interested_Technologies__c}&quot;</span><span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>column<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockTable<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Debug - SOQL&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;debug&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!debugSoql}&quot;</span> <span style="color: #339933;">/&gt;</span>           
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>    
&nbsp;
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Sharing Controller Context with a Visualforce Popup</title>
		<link>http://blog.jeffdouglas.com/2010/06/17/sharing-controller-context-with-a-visualforce-popup/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=sharing-controller-context-with-a-visualforce-popup</link>
		<comments>http://blog.jeffdouglas.com/2010/06/17/sharing-controller-context-with-a-visualforce-popup/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 11:11:58 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/2010/06/17/sharing-controller-context-with-a-visualforce-popup/</guid>
		<description><![CDATA[&#8220;How do I popup a dialog in Visualforce&#8221;. I&#8217;m not sure what the deal is but this week I&#8217;ve received 3 emails asking me how to open a Visualforce popop window from a Visualforce page but both running in the same session. The question is asked fairly often on the message boards but there isn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F06%2F17%2Fsharing-controller-context-with-a-visualforce-popup%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F06%2F17%2Fsharing-controller-context-with-a-visualforce-popup%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<blockquote style="clear: both"><p style="clear: both"><strong>&#8220;How do I popup a dialog in Visualforce&#8221;. </strong></p>
</blockquote>
<p style="clear: both">I&#8217;m not sure what the deal is but this week I&#8217;ve received 3 emails asking me how to open a Visualforce popop window from a Visualforce page but <strong><u>both</u></strong> running in the same session. The question is asked fairly often on the message boards but there isn&#8217;t a native solution. </p>
<p style="clear: both">The use case is that you have a Visualforce page with a commandbutton that launches a Visualforce popup. You want the popup to be able to access the controller&#8217;s context so you can share state. Unfortunately this is not currently possible with Visualforce and Apex. Opening a new window will start a new request which initializes a new instance of the controller. You <em>could</em> pass some variables to the new controller via Javascript but making a callback to the opening Visualforce is tedious as best.</p>
<p style="clear: both">Depending on what your popup needs to do, the best solution might be to create a modal instead of a new Visualforce page. Dave Carroll has a great article titled, &#8220;<a href="http://wiki.developerforce.com/index.php/Visualforce_Popup" target="_blank">Modal Dialogs in Visualforce using the Yahoo! User Interface Library</a>&#8220;, that shows how to use an In-Page DIV or Hidden DIV to create the modal. You can probably accomplish the same effect with other libraries but Dave&#8217;s solution with <a href="http://developer.yahoo.com/yui/" target="_blank">YUI!</a> is straight-forward, simple and well documented.</p>
<p><br class="final-break" style="clear: both" /></p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/06/17/sharing-controller-context-with-a-visualforce-popup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uploading an Attachment using Visualforce and a Custom Controller</title>
		<link>http://blog.jeffdouglas.com/2010/04/28/uploading-an-attachment-using-visualforce-and-a-custom-controller/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=uploading-an-attachment-using-visualforce-and-a-custom-controller</link>
		<comments>http://blog.jeffdouglas.com/2010/04/28/uploading-an-attachment-using-visualforce-and-a-custom-controller/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 11:07:11 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2486</guid>
		<description><![CDATA[This is a follow up post to Uploading a Document using Visualforce and a Custom Controller showing an example for uploading an attachment for a Contact. The Visualforce page and Controller is very similar with a few exceptions. Attachments are different than documents and are only available for the following objects: Account Asset Campaign Case [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F28%2Fuploading-an-attachment-using-visualforce-and-a-custom-controller%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F28%2Fuploading-an-attachment-using-visualforce-and-a-custom-controller%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p style="clear: both">This is a follow up post to <a href="http://blog.jeffdouglas.com/2010/04/22/uploading-a-document-using-visualforce-and-a-custom-controller/" target="_blank">Uploading a Document using Visualforce and a Custom Controller</a> showing an example for uploading an attachment for a Contact. The Visualforce page and Controller is very similar with a few exceptions.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/attachments-screenshot.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/attachments-screenshot-thumb.png" height="257" align="left" width="550" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a><br style="clear: both" />Attachments are different than documents and are only available for the following objects:</p>
<p style="clear: both">
<ul style="clear: both">
<li>Account</li>
<li>Asset</li>
<li>Campaign</li>
<li>Case</li>
<li>Contact</li>
<li>Contract</li>
<li>Custom objects</li>
<li>EmailMessage</li>
<li>EmailTemplate</li>
<li>Event</li>
<li>Lead</li>
<li>Opportunity</li>
<li>Product2</li>
<li>Solution</li>
<li>Task</li>
</ul>
<p style="clear: both">Salesforce.com restricts an attachment size to a maximum size of 5 MB. For a file attached to a Solution, the limit is 1.5MB. The maximum email attachment size is 3 MB. You can contact Salesforce.com support and possibly have them increase these limits. They should be able to increase the document and attachment size to 25MB. They cannot increase the limits for emails.</p>
<p style="clear: both">AttachmentUploadExample</p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;AttachmentUploadController&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>sectionHeader title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Visualforce Example&quot;</span> subtitle<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Attachment Upload Example&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form enctype<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;multipart/form-data&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageMessages <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Upload a Attachment&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockButtons <span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandButton action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!upload}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Save&quot;</span><span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockButtons<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSection showHeader<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> columns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;block1&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;File Name&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fileName&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!attachment.name}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fileName&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;File&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputFile value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!attachment.body}&quot;</span> filename<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!attachment.name}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Description&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputTextarea value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!attachment.description}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSection<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p style="clear: both">AttachmentUploadController</p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> with sharing <span style="color: #000000; font-weight: bold;">class</span> AttachmentUploadController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> Attachment attachment <span style="color: #009900;">&#123;</span>
  get <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>attachment <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        attachment <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Attachment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> attachment<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> PageReference upload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    attachment.<span style="color: #006633;">OwnerId</span> <span style="color: #339933;">=</span> UserInfo.<span style="color: #006633;">getUserId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    attachment.<span style="color: #006633;">ParentId</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'0037000000lFxcw'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the record the file is attached to</span>
    attachment.<span style="color: #006633;">IsPrivate</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      insert attachment<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>DMLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">severity</span>.<span style="color: #006633;">ERROR</span>,<span style="color: #0000ff;">'Error uploading attachment'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
      attachment <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Attachment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">severity</span>.<span style="color: #006633;">INFO</span>,<span style="color: #0000ff;">'Attachment uploaded successfully'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p style="clear: both">
<p><br class="final-break" style="clear: both" /></p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/04/28/uploading-an-attachment-using-visualforce-and-a-custom-controller/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Create a New Record in Force.com Sites</title>
		<link>http://blog.jeffdouglas.com/2010/04/27/create-a-new-record-in-force-com-sites/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=create-a-new-record-in-force-com-sites</link>
		<comments>http://blog.jeffdouglas.com/2010/04/27/create-a-new-record-in-force-com-sites/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 12:00:09 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2474</guid>
		<description><![CDATA[So someone asked me yesterday for some code to allow external users to create contact records in Salesforce.com. They needed a simple form where people could enter the details and once submitted receive a confirmation of what information was entered. Here&#8217;s what the final page that was developed looks like. You can try the code [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F27%2Fcreate-a-new-record-in-force-com-sites%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F27%2Fcreate-a-new-record-in-force-com-sites%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p style="clear: both">So someone asked me yesterday for some code to allow external users to create contact records in Salesforce.com. They needed a simple form where people could enter the details and once submitted receive a confirmation of what information was entered. Here&#8217;s what the final page that was developed looks like. You can <a href="http://jeffdouglas-developer-edition.na5.force.com/examples /Contact_Create" target="_blank">try the code out on my developer site</a> to see how it runs.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/conact-create.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/conact-create-thumb.png" height="240" align="left" width="530" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a><br style="clear: both" />If this solution was meant to run inside the Salesforce.com UI, you would simply need a single Visualforce page that utilizes the standard controller for Contact. Once submitted, the standard controller would insert the new record and relocate the user to the display page for the new record. </p>
<p style="clear: both">However, since this is an external page we have to do a little more work. We need two Visualforce pages (one for the form and one for the confirmation page) and a custom controller to submit the new contact record and show the user the confirmation page. You&#8217;ll also need to set up a new Force.com Site and add the two new Visualforce pages to the site so they are accessible externally. You&#8217;ll also need to modify the Public Access Settings for the site to allow Read access to the Contact object.</p>
<p style="clear: both">Your code should look like the following:</p>
<p style="clear: both"><strong>Contact_Create Visualforce Page</strong></p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ContactCreateController&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>sectionHeader title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Visualforce Example&quot;</span> subtitle<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Create a Contact&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form <span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageMessages <span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;!--</span> <span style="color: #000000; font-weight: bold;">this</span> is where the error messages will appear <span style="color: #339933;">--&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Contact Info&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockButtons <span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandButton action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!save}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Save&quot;</span><span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockButtons<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSection showHeader<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> columns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.firstName}&quot;</span> <span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.lastName}&quot;</span> <span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.email}&quot;</span> <span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSection<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p style="clear: both"><strong>Contact_Create_Thankyou Visualforce Page</strong></p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ContactCreateController&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>sectionHeader title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Visualforce Example&quot;</span> subtitle<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Thank You&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form <span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Contact Info&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSection showHeader<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> columns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.firstName}&quot;</span> <span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.lastName}&quot;</span> <span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputField value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!contact.email}&quot;</span> <span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSection<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p style="clear: both"><strong>Apex Controller</strong></p>
<p style="clear: both">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> with sharing <span style="color: #000000; font-weight: bold;">class</span> ContactCreateController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// the contact record you are adding values to</span>
  <span style="color: #000000; font-weight: bold;">public</span> Contact contact <span style="color: #009900;">&#123;</span>
    get <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>contact <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        contact <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Contact<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> contact<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ContactCreateController<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// blank constructor</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// save button is clicked</span>
  <span style="color: #000000; font-weight: bold;">public</span> PageReference save<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      insert contact<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// inserts the new record into the database</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>DMLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">severity</span>.<span style="color: #006633;">ERROR</span>,<span style="color: #0000ff;">'Error creating new contact.'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// if successfully inserted new contact, then displays the thank you page.</span>
    <span style="color: #000000; font-weight: bold;">return</span> Page.<span style="color: #006633;">Contact_Create_Thankyou</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</p>
<p><br class="final-break" style="clear: both" /></p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/04/27/create-a-new-record-in-force-com-sites/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Uploading a Document using Visualforce and a Custom Controller</title>
		<link>http://blog.jeffdouglas.com/2010/04/22/uploading-a-document-using-visualforce-and-a-custom-controller/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=uploading-a-document-using-visualforce-and-a-custom-controller</link>
		<comments>http://blog.jeffdouglas.com/2010/04/22/uploading-a-document-using-visualforce-and-a-custom-controller/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 11:56:00 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2461</guid>
		<description><![CDATA[The Salesforce docs for the inputFile Visualforce component has an example of uploading a document using the Standard Controller. Here is a quick example of using a Custom Controller in case you want to make the upload process part of a larger transaction. Make sure you take a look at the finally block in the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F22%2Fuploading-a-document-using-visualforce-and-a-custom-controller%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F22%2Fuploading-a-document-using-visualforce-and-a-custom-controller%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p style="clear: both">The <a href="http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_inputFile.htm" target="_blank">Salesforce docs</a> for the inputFile Visualforce component has an example of uploading a document using the Standard Controller. Here is a quick example of using a Custom Controller in case you want to make the upload process part of a larger transaction.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/example-file-upload.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/example-file-upload-thumb.png" height="241" align="left" width="550" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a><br style="clear: both" />Make sure you take a look at the <strong>finally</strong> block in the controller below. The finally block always executes when the try block exits regardless if an error occurs or not. You need to ensure you clear out document&#8217;s body (document.body = null) so that the blob is not automatically included in the serialized image of the controller. If you do not clear out the body, you&#8217;ll get the following view state error:</p>
<blockquote style="clear: both"><p>Maximum view state size limit (128K) exceeded. Actual viewstate size for this page was&#8230;</p>
</blockquote>
<p style="clear: both"><strong>FileUploadExample</strong></p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>page controller<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;FileUploadController&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>sectionHeader title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Visualforce Example&quot;</span> subtitle<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;File Upload Example&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>form enctype<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;multipart/form-data&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageMessages <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlock title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Upload a File&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockButtons <span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>commandButton action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!upload}&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Save&quot;</span><span style="color: #339933;">/&gt;</span>
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockButtons<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSection showHeader<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> columns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;block1&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;File Name&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fileName&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!document.name}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fileName&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;File&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputFile value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!document.body}&quot;</span> filename<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!document.name}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;file&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Description&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputTextarea value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!document.description}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
        <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem <span style="color: #339933;">&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>outputLabel value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Keywords&quot;</span> <span style="color: #000000; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;keywords&quot;</span><span style="color: #339933;">/&gt;</span>
          <span style="color: #339933;">&lt;</span>apex<span style="color: #339933;">:</span>inputText value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{!document.keywords}&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;keywords&quot;</span><span style="color: #339933;">/&gt;</span>
        <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSectionItem<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlockSection<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>pageBlock<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>apex<span style="color: #339933;">:</span>page<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</p>
<p style="clear: both"><strong>FileUploadController</strong></p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> with sharing <span style="color: #000000; font-weight: bold;">class</span> FileUploadController <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Document</span> document <span style="color: #009900;">&#123;</span>
    get <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        document <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Document</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> document<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    set<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> PageReference upload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    document.<span style="color: #006633;">AuthorId</span> <span style="color: #339933;">=</span> UserInfo.<span style="color: #006633;">getUserId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">FolderId</span> <span style="color: #339933;">=</span> UserInfo.<span style="color: #006633;">getUserId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// put it in running user's folder</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      insert document<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>DMLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">severity</span>.<span style="color: #006633;">ERROR</span>,<span style="color: #0000ff;">'Error uploading file'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
      document.<span style="color: #006633;">body</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span> clears the viewstate
      document <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Document</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ApexPages.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ApexPages.<span style="color: #006633;">message</span><span style="color: #009900;">&#40;</span>ApexPages.<span style="color: #006633;">severity</span>.<span style="color: #006633;">INFO</span>,<span style="color: #0000ff;">'File uploaded successfully'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><br class="final-break" style="clear: both" /></p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/04/22/uploading-a-document-using-visualforce-and-a-custom-controller/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Easily Search and Edit Records with Visualforce</title>
		<link>http://blog.jeffdouglas.com/2010/04/07/easily-search-and-edit-records-with-visualforce/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=easily-search-and-edit-records-with-visualforce</link>
		<comments>http://blog.jeffdouglas.com/2010/04/07/easily-search-and-edit-records-with-visualforce/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 11:43:04 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2427</guid>
		<description><![CDATA[I tend to over think Visualforce development sometimes and make it harder than it should be. Development with Force.com is surprisingly easy, elegant and quick. For example I recently needed to develop a way for the users to search for records and update specific values. Users should be able to search for records by keyword [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F07%2Feasily-search-and-edit-records-with-visualforce%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.jeffdouglas.com%2F2010%2F04%2F07%2Feasily-search-and-edit-records-with-visualforce%2F&amp;source=jeffdonthemic&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p style="clear: both">I tend to over think Visualforce development sometimes and make it harder than it should be. Development with Force.com is surprisingly easy, elegant and quick. For example I recently needed to develop a way for the users to search for records and update specific values. Users should be able to search for records by keyword search, view multiple matching records, update values for multiple records, commit the changes to the database and check for validation errors.</p>
<p style="clear: both">Now if I was doing this in Java I&#8217;d need to:</p>
<ul style="clear: both">
<li>Create a JSP for the UI</li>
<li>Create a bean to model the data</li>
<li>Implement a Javascript framework for form validation</li>
<li>Write a DAO layer of to handle the search, retrieval and persisting of records</li>
<li>Write a Servlet to control the entire process</li>
</ul>
<p style="clear: both">Force.com makes developing this type of interface a snap. Using only a single Visualforce page and an Apex controller you can whip up the following in no time.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/edit-page10.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/edit-page10-thumb.png" height="312" align="left" width="480" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a><br style="clear: both" /><br />Force.com provides the data model (custom object), the search functionality (SOQL/SOSL) and the DAO layer (DML) for you so all you have to do is implement the business logic and process flow. One of my favorite features of Visualforce is the baked-in form validation. Gone are the days when you had to implement validation in each page you developed and then always remember to update the rules when changes are made. You simply define the fields at the object level with all of its rules and datatype specifics. When developing your Visualforce page, by simply using the standard components you invoke the validation inherit in the platform as well as some nifty Ajax eye-candy. You can focus on the business requirements and not the low level data integrity requirements.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/edit-page7.png" class="image-link" rel="lightbox"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/04/edit-page7-thumb.png" height="400" align="left" width="481" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a><br style="clear: both" /><br />So here is the code for the Visualforce page and Apex controller in case anyone wants to use it for a starting point.</p>
<p style="clear: both">

<div class="wp_syntax"><div class="code"><pre class="1" style="font-family:monospace;">&lt;apex:page standardController=&quot;MyObject__c&quot; extensions=&quot;ItemEditController&quot;&gt;
  &lt;apex:sectionHeader title=&quot;{!MyObject__c.Name}&quot; subtitle=&quot;Edit Records&quot;/&gt;
  &lt;apex:form &gt;
    &lt;apex:pageBlock mode=&quot;edit&quot; id=&quot;block&quot;&gt;
&nbsp;
      &lt;apex:pageBlockButtons location=&quot;both&quot;&gt;
        &lt;apex:commandButton action=&quot;{!save}&quot; value=&quot;Save Records&quot;/&gt;
        &lt;apex:commandButton action=&quot;{!cancel}&quot; value=&quot;Cancel&quot;/&gt;
      &lt;/apex:pageBlockButtons&gt;
      &lt;apex:pageMessages /&gt;
&nbsp;
      &lt;apex:pageBlockSection &gt;
        &lt;apex:pageBlockSectionItem &gt;
          &lt;apex:outputLabel for=&quot;searchText&quot;&gt;Keyword&lt;/apex:outputLabel&gt;
          &lt;apex:panelGroup &gt;
          &lt;apex:inputText id=&quot;searchText&quot; value=&quot;{!searchText}&quot;/&gt;
          &lt;apex:commandButton value=&quot;Search&quot; action=&quot;{!search}&quot; rerender=&quot;block&quot; status=&quot;status&quot;/&gt;
          &lt;/apex:panelGroup&gt;
        &lt;/apex:pageBlockSectionItem&gt;
      &lt;/apex:pageBlockSection&gt;&lt;br/&gt;
&nbsp;
      &lt;apex:actionStatus id=&quot;status&quot; startText=&quot;Searching... please wait...&quot;/&gt;
      &lt;apex:pageBlockSection title=&quot;Search Results&quot; id=&quot;resultsBlock&quot; columns=&quot;1&quot;&gt;
        &lt;apex:pageBlockTable value=&quot;{!searchResults}&quot; var=&quot;item&quot; rendered=&quot;{!NOT(ISNULL(searchResults))}&quot;&gt;
          &lt;apex:column value=&quot;{!item.Name}&quot; headerValue=&quot;Item&quot; width=&quot;100&quot;/&gt;
          &lt;apex:column headerValue=&quot;Value&quot; width=&quot;200&quot;&gt;
            &lt;apex:inputField value=&quot;{!item.Value__c}&quot;/&gt;
          &lt;/apex:column&gt;
        &lt;/apex:pageBlockTable&gt;
      &lt;/apex:pageBlockSection&gt;
    &lt;/apex:pageBlock&gt;
  &lt;/apex:form&gt;
&lt;/apex:page&gt;</pre></div></div>

</p>
<p style="clear: both">

<div class="wp_syntax"><div class="code"><pre class="1" style="font-family:monospace;">public with sharing class ItemEditController {
&nbsp;
  private ApexPages.StandardController controller {get; set;}
  public List&lt;itemObject__c&gt; searchResults {get;set;}
  public string searchText {get;set;}
&nbsp;
  // standard controller - could also just use custom controller
  public ItemEditController(ApexPages.StandardController controller) { }
&nbsp;
  // fired when the search button is clicked
  public PageReference search() {
    String qry = 'select id, name, value__c from ItemObject__c ' +
      'where name LIKE \'%'+searchText+'%\' order by name';
    searchResults = Database.query(qry);
    return null;
  }
&nbsp;
  // fired when the save records button is clicked
  public PageReference save() {
&nbsp;
    try {
      update searchResults;
    } Catch (DMLException e) {
      ApexPages.addMessages(e);
      return null;
    }
&nbsp;
    return new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
  }
&nbsp;
  // takes user back to main record
  public PageReference cancel() {
    return new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
  }
&nbsp;
}</pre></div></div>

</p>
<p><br class="final-break" style="clear: both" /></p>
<div class="post-scriptum"></div>]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/04/07/easily-search-and-edit-records-with-visualforce/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
