<?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; Technology</title>
	<atom:link href="http://blog.jeffdouglas.com/category/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>Thu, 29 Jul 2010 19:04:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Using the Salesforce.com OAuth Playground</title>
		<link>http://blog.jeffdouglas.com/2010/07/28/using-the-salesforce-oauth-playground/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=using-the-salesforce-oauth-playground</link>
		<comments>http://blog.jeffdouglas.com/2010/07/28/using-the-salesforce-oauth-playground/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 12:39:25 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Salesforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2968</guid>
		<description><![CDATA[﻿My article on developer.force.com, Using OAuth to Authorize External Applications, shows how to develop external Java applications that use OAuth to access your Saleforce.com data. But what if you want to do the opposite? What if you need to write an application on the Force.com platform that uses your data from Google, Twitter, LinkedIn or [...]]]></description>
			<content:encoded><![CDATA[<p>﻿My article on developer.force.com, <a href="http://wiki.developerforce.com/index.php/Using_OAuth_to_Authorize_External_Applications">Using OAuth to Authorize External Applications</a>, shows how to develop external Java applications that use OAuth to access your Saleforce.com data. But what if you want to do the opposite? What if you need to write an application on the Force.com platform that uses your data from Google, Twitter, LinkedIn or TripIt using OAuth? Luckily <a href="http://twitter.com/jesperfj">Jesper Jorgenson</a> at Salesforce.com posted an open source project called <a href="http://code.google.com/p/sfdc-oauth-playground/">sfdc-oauth-playground</a> which is a generic consumer implementation of OAuth as a Force.com App.</p>
<p>The main purpose of this beta project is to show you how to write OAuth signed requests in Apex. There is a managed packaged you can install into your org but if you really want to dig into the guts of OAuth (and who doesn&#8217;t??) you&#8217;ll need to download and install the source code from the project. The managed package doesn&#8217;t afford you to opportunity to modify code or view granular debug statements.</p>
<p>Jesper doesn&#8217;t provide much documentation for the project so I&#8217;ve put together a short video showing how to get started using Google Accounts and Blogger. Unfortunately different providers implement OAuth to their own liking so you sometimes have to make modifications to the requests being sent over the wire. I ran into a number of issues so hopefully this will assist you in getting up and running with OAuth.</p>
<p><span class="youtube">
<object width="480" height="295">
<param name="movie" value="http://www.youtube.com/v/sXJxG6zWSZA&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=0&amp;fs=1&amp;hl=en&amp;autoplay=0&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0?rel=1&amp;hd=1&#038;hd=1&#038;w=600]" />
<param name="allowFullScreen" value="true" />
<embed wmode="transparent" src="http://www.youtube.com/v/sXJxG6zWSZA&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=0&amp;fs=1&amp;hl=en&amp;autoplay=0&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0?rel=1&amp;hd=1&#038;hd=1&#038;w=600]" type="application/x-shockwave-flash" allowfullscreen="true" width="480" height="295"></embed>
<param name="wmode" value="transparent" />
</object>
</span><p><a href="http://www.youtube.com/watch?v=sXJxG6zWSZA&fmt=18">www.youtube.com/watch?v=sXJxG6zWSZA</a></p></p>
<p> </p>
<p>﻿In case you don&#8217;t have time for the video, here are a couple of screenshots outlining the process using the OAuth Consumer Playground application from the managed package.</p>
<p><strong>﻿Create a new OAuth Service (e.g., Blogger) with various URLs for accessing services</strong></p>
<p><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-unauthorized1_large.png" rel="lightbox[2968]"><img title="oauth-service-unauthorized.png" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-unauthorized1.png" border="0" alt="oauth-service-unauthorized.png" width="400" height="308" /></a></p>
<p> </p>
<p>﻿<strong>Authorize Salesforce access to Blogger</strong></p>
<p><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-preauth1_large.png" rel="lightbox[2968]"><img title="oauth-service-preauth.png" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-preauth1.png" border="0" alt="oauth-service-preauth.png" width="400" height="266" /></a></p>
<p> </p>
<p><strong>﻿Grant access to Blogger</strong></p>
<p><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-blogger_large.png" rel="lightbox[2968]"><img title="oauth-service-blogger.png" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-blogger.png" border="0" alt="oauth-service-blogger.png" width="400" height="266" /></a></p>
<p> </p>
<p><strong>﻿Authorization confirmation</strong></p>
<p><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-auth_large.png" rel="lightbox[2968]"><img title="oauth-service-auth.png" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-auth.png" border="0" alt="oauth-service-auth.png" width="400" height="266" /></a></p>
<p> </p>
<p>﻿<strong>OAuth Service access tokens</strong></p>
<p><a href=""http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-authorized_large.png"><img title="oauth-service-authorized.png" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-authorized.png" border="0" alt="oauth-service-authorized.png" width="400" height="308" /></a></p>
<p> </p>
<p><strong>﻿Testing the service</strong></p>
<p><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-test_large.png" rel="lightbox[2968]"><img title="oauth-service-test.png" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/oauth-service-test.png" border="0" alt="oauth-service-test.png" width="400" height="333" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/28/using-the-salesforce-oauth-playground/feed/</wfw:commentRss>
		<slash:comments>0</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[<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>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/22/create-and-email-a-document-with-salesforce-com/feed/</wfw:commentRss>
		<slash:comments>2</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[<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>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/16/create-and-email-a-pdf-with-salesforce-com/feed/</wfw:commentRss>
		<slash:comments>2</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[<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>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/feed/</wfw:commentRss>
		<slash:comments>4</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[<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>

]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using RSA-SHA1 with Salesforce Crypto Class</title>
		<link>http://blog.jeffdouglas.com/2010/07/06/using-rsa-sha1-with-salesforce-crypto-class/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=using-rsa-sha1-with-salesforce-crypto-class</link>
		<comments>http://blog.jeffdouglas.com/2010/07/06/using-rsa-sha1-with-salesforce-crypto-class/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 21:28:31 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Salesforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2754</guid>
		<description><![CDATA[This is a rather obscure post but it will definitely come in handy to someone trying to use the RSA-SHA1 algorithm with the Salesforce Crypto class. I&#8217;m spent the past two days trying to hook up OAuth using Apex and various Google Services. The standard HMAC-SHA1 algorithm is fairly straight forward with OAuth and Google [...]]]></description>
			<content:encoded><![CDATA[<p style="clear: both">This is a rather obscure post but it will definitely come in handy to someone trying to use the RSA-SHA1 algorithm with the Salesforce Crypto class. I&#8217;m spent the past two days trying to hook up OAuth using Apex and various Google Services. The standard HMAC-SHA1 algorithm is fairly straight forward with OAuth and Google but when you are required to register a domain and upload a certificate&#8230; well things get somewhat hairy. There&#8217;s very little documentation on the RSA-SHA2 algorithm and I could only find one relevant post on the topic.</p>
<p style="clear: both">According to the <a href="http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_crypto.htm">Crypto docs</a>, the arguments for the <strong><u>sign</u></strong> method are an algorithm name (i.e., RSA-SHA1), an input Blob (the string to encrypt) and a privateKey Blob. The value of privateKey must be decoded using the EncodingUtilbase64Decode method, and should be in <a href="http://www.rsa.com/rsalabs/node.asp?id=2130" target="_blank">RSA&#8217;s PKCS #8 (1.2) Private-Key Information Syntax Standard form</a>. </p>
<p style="clear: both">The sample code from the docs doesn&#8217;t help determining how to obtain the privateKey:</p>
<p style="clear: both">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> algorithmName <span style="color: #339933;">=</span> <span style="color: #0000ff;">'RSA'</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> <span style="color: #0000ff;">'pkcs8 format private key'</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Blob</span> privateKey <span style="color: #339933;">=</span> EncodingUtil.<span style="color: #006633;">base64Decode</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Blob</span> input <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;">'12345qwerty'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Crypto.<span style="color: #006633;">sign</span><span style="color: #009900;">&#40;</span>algorithmName, input, privateKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p style="clear: both">So after a couple of days of trial and error, swearing and sticking needles in my eyes, I finally came up with the solution for Google. It may not apply to other services but the methodology is roughly the same for the privateKey.</p>
<p style="clear: both">The first step is to generate a self-signing public certificate and private key. You can find more info <a href="http://code.google.com/apis/gdata/docs/auth/authsub.html#Registered" target="_blank">here</a>. Open terminal and run the following:</p>
<p style="clear: both; font-family: Verdana;">openssl req -x509 -nodes -days 365 -newkey rsa:1024 -shal -subj &#8216;/C=US/ST=CA/L=San Mateo/CN=www.appirio.com&#8217; -keyout key-mycompanyrsa.pem -out cert-mycompanyrsa.pem</p>
<p style="clear: both">This will spit out a cert file (cert-mycompanyrsa.pem) and private key file (key-mycompanyrsa.pem). Then upload the cert to your domain for the <a href="https://www.google.com/accounts/ManageDomains" target="_blank">Google account</a>. This will generate a consumer key and a consumer secret for your application.</p>
<p style="clear: both">Now here was my mistake. I was opening the private key file (key-mycompanyrsa.pem) and using the contents of this file in my Apex code for the privateKey Blob value. Google was choking when creating the OAuth request token saying that the signature was incorrect: <strong><em>signature_invalid</em></strong>.</p>
<p style="clear: both">Here&#8217;s the fix. You need to use the private key file (key-mycompanyrsa.pem) with the openssl pkcs8 command to process the private keys into PKCS#8 format.</p>
<p style="clear: both">Open terminal again and run:</p>
<p style="clear: both; font-family: Verdana;">openssl pkcs8 -topk8 -nocrypt -in key-mycompanyrsa.pem -outform PEM</p>
<p style="clear: both">This will display a new private key in terminal and <strong><em>THIS</em></strong> is the value you use for the privateKey argument for the sign method. Just copy everything betweek the &#8220;begin&#8221; line and &#8220;end&#8221; line.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/privatekey.png" class="image-link" rel="lightbox[2754]"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/privatekey-thumb.png" height="275" align="left" width="380" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a></p>
<p><br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/06/using-rsa-sha1-with-salesforce-crypto-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error Compiling Salesforce Metadata API with WSC</title>
		<link>http://blog.jeffdouglas.com/2010/07/01/error-compiling-salesforce-metadata-api-with-wsc/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=error-compiling-salesforce-metadata-api-with-wsc</link>
		<comments>http://blog.jeffdouglas.com/2010/07/01/error-compiling-salesforce-metadata-api-with-wsc/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 12:00:52 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Code Sample]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Salesforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/2010/07/01/error-compiling-salesforce-metadata-api-with-wsc/</guid>
		<description><![CDATA[This might not be relevant right now but you may find it useful in the near future. I&#8217;m tinkering with an app that spits out documentation for an org. I really hate doing documentation and am quite lazy so I thought I&#8217;d write something to allow my computer to do my work for me. I [...]]]></description>
			<content:encoded><![CDATA[<p style="clear: both">This might not be relevant right now but you may find it useful in the near future. I&#8217;m tinkering with an app that spits out documentation for an org. I really hate doing documentation and am quite lazy so I thought I&#8217;d write something to allow my computer to do my work for me. </p>
<p style="clear: both">I ran into a small issue when trying to compile the Metadata WSDL with the <a href="http://code.google.com/p/sfdc-wsc/" target="_blank">Force.com Web Services Connector</a> so I thought I&#8217;d post my fix.</p>
<p style="clear: both">So to use the Metadata API with the WSC, first download the Metadata WSDL from your org and the latest <a href="http://code.google.com/p/sfdc-wsc/downloads/list" target="_blank">WSC jar from the project</a>. Now issue something like the following to generate the stub code from the WSDL:</p>
<blockquote style="clear: both"><p style="clear: both">java -classpath wsc-19.jar com.sforce.ws.tools.wsdlc metadata.wsdl metadata.jar</p>
</blockquote>
<p style="clear: both">Unfortunately this results in an error similar to:</p>
<blockquote style="clear: both"><p style="clear: both">-dir/com/sforce/soap/metadata/Encoding.java:12: &#8216;} &#8216; expected UTF-8,</p>
</blockquote>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/terminal-error.png" class="image-link" rel="lightbox[2747]"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/terminal-error-thumb.png" height="275" align="left" width="380" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a><br style="clear: both" />The error is thrown because the WSDL contains some hyphens that is making the compiler puke. <strong>You can fix the issue by replacing the hyphens in the Metadata WSDL with underscores</strong>, saving the WSDL and then compiling the stub code again. </p>
<p style="clear: both">Here&#8217;s the offending portion of the Metadata WSDL.</p>
<p style="clear: both"><a href="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/metadataapi-replace.png" class="image-link" rel="lightbox[2747]"><img class="linked-to-original" src="http://blog.jeffdouglas.com/wp-content/uploads/2010/07/metadataapi-replace-thumb.png" height="239" align="left" width="332" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a></p>
<p><br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/07/01/error-compiling-salesforce-metadata-api-with-wsc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Any Interest in a Chatter / Spring / App Engine Tutorial?</title>
		<link>http://blog.jeffdouglas.com/2010/06/30/any-interest-in-a-chatter-spring-app-engine-tutorial/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=any-interest-in-a-chatter-spring-app-engine-tutorial</link>
		<comments>http://blog.jeffdouglas.com/2010/06/30/any-interest-in-a-chatter-spring-app-engine-tutorial/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 11:35:44 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[GAE/J]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[VMforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2735</guid>
		<description><![CDATA[Following up on yesterday&#8217;s post, Get Ready for VMForce &#8211; Here&#8217;s How!, I was thinking about putting together a series of tutorials on building a Chatter app on App Engine using Spring MVC with JDO. This will take considerable effort and I&#8217;m just wondering if it would be valuable to the Salesforce.com community. Tell me [...]]]></description>
			<content:encoded><![CDATA[<p>Following up on yesterday&#8217;s post, <a href="http://blog.jeffdouglas.com/2010/06/29/get-ready-for-vmforce/" target="_blank">Get Ready for VMForce &#8211; Here&#8217;s How!</a>, I was thinking about putting together a series of tutorials on building a Chatter app on App Engine using Spring MVC with JDO. This will take considerable effort and I&#8217;m just wondering if it would be valuable to the Salesforce.com community.
<div>Tell me what you think.</div>
<p/>
<script type='text/javascript' language='javascript' charset='utf-8' src='http://s3.polldaddy.com/p/3412057.js'></script><noscript> <a href='http://answers.polldaddy.com/poll/3412057/'>View Poll</a></noscript><br />
<br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/06/30/any-interest-in-a-chatter-spring-app-engine-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Get Ready for VMForce &#8211; Here&#8217;s How!</title>
		<link>http://blog.jeffdouglas.com/2010/06/29/get-ready-for-vmforce/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=get-ready-for-vmforce</link>
		<comments>http://blog.jeffdouglas.com/2010/06/29/get-ready-for-vmforce/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 14:59:41 +0000</pubDate>
		<dc:creator>Jeff Douglas</dc:creator>
				<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[VMforce]]></category>

		<guid isPermaLink="false">http://blog.jeffdouglas.com/?p=2730</guid>
		<description><![CDATA[VMForce is coming (sometime in the near future) so what can you do to get started now? If I was a betting man (with inside info), here is what I&#8217;d do to get ready. Start Building on Google App EngineMy guess is that VMForce will look a little like Google App Engine. You&#8217;ll probably have [...]]]></description>
			<content:encoded><![CDATA[<p style="clear: both"><a href="http://www.vmforce.com/"><img src="http://blog.jeffdouglas.com/wp-content/uploads/2010/06/vmforce.png" alt="" title="vmforce" width="223" height="92" class="alignleft size-full wp-image-2731" /></a><a href="http://www.vmforce.com/" target="_blank">VMForce</a> is coming (sometime in the near future) so what can you do to get started now? If I was a betting man (with inside info), here is what I&#8217;d do to get ready. </p>
<p style="clear: both"><strong>Start Building on Google App Engine</strong><br />My guess is that VMForce will look a little like Google App Engine. You&#8217;ll probably have access to a wide range of Java libaries but not all of them. Based upon Salesforce.com&#8217;s current model you can expect that there will be some platform governors and limits in place. This should provide you with some experience and a working knowledge of some issues you may run into. If you want to get up and running quickly you can always buy my book <a href="http://www.amazon.com/gp/product/143022553X?ie=UTF8&#038;tag=jefdoublo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=143022553X" target="_blank">Beginning Java Google App Engine</a> (yes&#8230; blatant plug).</p>
<p style="clear: both"><strong>Start Learning Spring</strong><br />I would probably start with Spring MVC or <a href="http://www.springsource.org/webflow" target="_blank">Spring Web Flow 2</a> initially. The good thing is that most of Spring also runs on App Engine so you <em>should</em> be able to move any apps you write on App Engine to VMForce. Here&#8217;s a <a href="http://static.springsource.org/docs/Spring-MVC-step-by-step" target="_blank">great article</a> to get you started with Spring MVC.</p>
<p style="clear: both">I would also download the <a href="http://www.springsource.com/products/sts" target="_blank">SpringSource Tool Suite</a> which is a Eclipse plugin for Spring development. It should fit in nicely with the Force.com IDE.</p>
<p style="clear: both">You might also want to dowload Spring&#8217;s <a href="http://www.springsource.com/products/tcserver" target="_blank">tc Server Developer Edition</a>. If you are using App Engine you can develop locally but you might also want to check out tc Server. The key component of tc Server Developer Edition is the Spring Insight console, a dashboard view of real-time Spring application performance metrics. Without changing code, developers can use Spring Insight to detect, analyze and diagnose application performance issues right from the desktops. This was demoed at Google IO and it was very slick! There&#8217;s a <a href="http://s3.springsource.com/MRKT/spring-metrics/Spring_Insight-Milestone_3-Screencast.mp4" target="_blank">really cool video</a> if you want to see how it works.</p>
<p style="clear: both"><strong>Dig into Persistence Layers</strong><br />You&#8217;ll definitely want to start working with a persistance layer like JDO, JPA or Hibernate. Based upon some <a href="http://blog.sforce.com/sforce/2010/06/forcecom-for-java-developers.html" target="_blank">recent comments</a> from Salesforce.com, I would look at JPA first.</p>
<p style="clear: both"><strong>Develop with the Force.com Web Service Connector (WSC)</strong><br />Most developers are familiar with the WS-APIs but you should really start looking at the <a href="ttp://code.google.com/p/sfdc-wsc" target="_blank">Force.com Web Service Connector</a>. I&#8217;m writing an article for developer.force.com on how to use the WSC so keep an eye out for that shortly.</p>
<p style="clear: both"><strong>Try Spring Roo</strong><br />If you have some extra time you might want to look into <a href="http://www.springsource.org/roo/why" target="_blank">Spring Roo</a>. Roo is a lightweight developer tool that allows you to build apps in minutes. They demoed Roo and Google Web Toolkit together at Google IO with impressive results.</p>
<p><br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jeffdouglas.com/2010/06/29/get-ready-for-vmforce/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
<enclosure url="http://s3.springsource.com/MRKT/spring-metrics/Spring_Insight-Milestone_3-Screencast.mp4" length="82795161" type="video/mp4" />
		</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[<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>
]]></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>
	</channel>
</rss>
