<?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>X-Squared On-Demand &#187; Apex</title>
	<atom:link href="http://www.x2od.com/cat/salesforce/development/apex-development-salesforce/feed" rel="self" type="application/rss+xml" />
	<link>http://www.x2od.com</link>
	<description>Salesforce Configuration, Administration, and Development</description>
	<lastBuildDate>Fri, 11 Jun 2010 16:30:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Sophisticated DateTime &#8220;Formula Fields&#8221; with Apex and Field-Level Security</title>
		<link>http://www.x2od.com/2010/05/17/sophisticated-datetime-formula-fields-with-apex-and-fls.html</link>
		<comments>http://www.x2od.com/2010/05/17/sophisticated-datetime-formula-fields-with-apex-and-fls.html#comments</comments>
		<pubDate>Mon, 17 May 2010 20:39:25 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Force.com Platform]]></category>
		<category><![CDATA[Native Application]]></category>
		<category><![CDATA[Salesforce CRM]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[salesforce.com]]></category>
		<category><![CDATA[Eclipse IDE]]></category>
		<category><![CDATA[Force.com Builder]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=997</guid>
		<description><![CDATA[What do you do when you want to calculate a formula-like field but a regular formula won't work? Salesforce CRM's formulas handle dates very well. If you want to enter a date value and have formula fields display, for instance, mydate__c + 21 days, that's simple. Just use mydate__c + 21. Side note: If you [...]]]></description>
			<content:encoded><![CDATA[<p>What do you do when you want to calculate a formula-like field but a regular formula won't work?  </p>
<p>Salesforce CRM's formulas handle dates very well.  If you want to enter a date value and have formula fields display, for instance, mydate__c + 21 days, that's simple.  Just use <code> mydate__c + 21</code>.</p>
<p><i>Side note: If you try going the long way around and use <code>DATE( YEAR( mydate__c ), MONTH( mydate__c ), DAY( mydate__c ) + 21 ) </code> and mydate__c = 09/17/2010, Salesforce returns #Error! because there's no date 09/38/2010.  Similarly, adding three months to a date like 1/31/2010 will also give an error.  More about this in a future post.</i></p>
<p>DateTime fields are like Date fields, but they include... wait for it... a time component (and can be created in the running user's local time zone or in GMT).</p>
<p>Here's a use-case for a DateTime formula field:</p>
<p>A photography studio schedules photo shoots, and different packages include different durations.  Similarly, we could use a hair salon which offers different services, each with a different duration, a dentist... you get the idea.</p>
<p>Requirements:</p>
<ol>
	<li>Enter a DateTime for an appointment start time (<code>starttime__c</code>)</li>
	<li>Enter a duration (though in a production system, I'd include a value on the <code>Product2</code> sObject, we'll just enter a value here) (<code>minutes__c</code>)</li>
	<li>Display a read-only DateTime field with the end time (<code>endtime__c</code>)</li>
	<li>The end time must be read-only to all users, like any formula field</li>
</ol>
<p>Here's what won't work:</p>
<ul>
	<li>A formula field won't work because there are no MINUTE(), HOUR(), SECOND() formula functions</li>
	<li>Workflow won't work because it depends on formulas to fill new values for date/datetime fields</li>
</ul>
<p>That leaves Apex.  First, the configuration:</p>
<ol>
	<li>Create DateTime field <code>starttime__c</code></li>
	<li>Create DateTime field <code>endtime__c</code></li>
	<li>Set <code>endtime__c</code> field-level security to Read-Only for all profiles</li>
	<li>Create Number (18,0) field <code>minutes__c</code></li>
	<li>Create a trigger on the sobject</li>
</ol>
<p>Here's the trigger:</p>
<pre class="brush: java;">
trigger timeTrigger on TestObject__c (before insert, before update) {
    for (TestObject__c t : Trigger.New){
    	if(t.StartTime__c != null &amp;&amp; t.minutes__c != null){
        datetime myDateT = t.StartTime__c;
        double d = t.minutes__c;
        Integer shootmins = d.intValue();
        if(mydateT != null &amp;&amp; shootmins != null)
        	t.EndTime__c = myDateT.addminutes(integer.valueof(shootmins));
       	}
    }
} 
</pre>
<p>Regular readers will note that I do usually split triggers into a trigger and a class, but I've not done so here purely for the sake of brevity.</p>
<p>Here's the test code:</p>
<pre class="brush: java;">
public without sharing class shootTimesTriggerTest {

    private static testMethod void ShootCalculateEndTime_PositiveTestCases() {
        TestObject__c to;
        TestObject__c l;    
        test.starttest();
        l = new TestObject__c (name = 'test');
        datetime myDateTime = datetime.newInstance(2008, 12, 1, 12, 30, 2);
        l.StartTime__c = myDateTime;
        l.minutes__c = 90;
        insert l;
        to = [SELECT id, EndTime__c FROM TestObject__c WHERE id = :l.id];
        datetime newDateTime = datetime.newInstance(2008, 12, 1, 14, 0, 2);
        system.assertequals(to.EndTime__c, newDateTime);
        l.minutes__c = 45;
        update l;        
        to = [SELECT id, EndTime__c FROM TestObject__c WHERE id = :l.id];
        newDateTime = datetime.newInstance(2008, 12, 1, 13, 15, 2);
        system.assertequals(to.EndTime__c, newDateTime);
        test.stoptest();
    }

    private static testMethod void OppCalculateEndTime_NegativeTestCases() {
        test.starttest();
        TestObject__c l = new TestObject__c (name = 'test');
        l.minutes__c = null;
        insert l;
        system.assertequals(l.EndTime__c, null);
        test.stoptest();
    } 
}
</pre>
<p>A few points about how this works:</p>
<ul>
	<li>Triggers run in System mode, so they don't respect field-level security.  Thus, we can set a field to read-only for all profiles, and the EndTime__c field will still be updated.</li>
	<li>The test code runs in System mode as well, avoiding any potential problems if the field were set to invisible to a profile and we used System.RunAs() to test for various profiles.</li>
	<li>Although I'm not a fan of using SOQL queries this often, I used these in the interest of saving time.  Keep in mind that if you had quite a few queries in your regular code, adding these two might put you over the limit, so use queries sparingly!</li>
	<li>This is the only way I know of to add minutes to a DateTime.</li>
</ul>
<p>Did I miss anything?  Please let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2010/05/17/sophisticated-datetime-formula-fields-with-apex-and-fls.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New Developer Library Released</title>
		<link>http://www.x2od.com/2009/11/19/new-developer-library-released.html</link>
		<comments>http://www.x2od.com/2009/11/19/new-developer-library-released.html#comments</comments>
		<pubDate>Thu, 19 Nov 2009 17:43:29 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Force.com Platform]]></category>
		<category><![CDATA[New Features]]></category>
		<category><![CDATA[Visualforce]]></category>
		<category><![CDATA[Winter 10]]></category>
		<category><![CDATA[salesforce.com]]></category>
		<category><![CDATA[Dreamforce 2009]]></category>
		<category><![CDATA[Salesforce.com]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=853</guid>
		<description><![CDATA[Today, Developer Force (http://developer.force.com) released its new library. Here are a few of them. All can be found at http://wiki.developerforce.com/index.php/Documentation. Workbook http://www.salesforce.com/us/developer/docs/workbook/index.htm Fundamentals http://www.salesforce.com/us/developer/docs/fundamentals/index.htm Cookbook http://www.salesforce.com/us/developer/docs/cookbook/index.htm Apex Advanced Code Example http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_shopping_cart_example.htm https://sites.secure.force.com/appexchange/listingDetail?listingId=a0N30000001saDCEAY And many more to come!]]></description>
			<content:encoded><![CDATA[<p>Today, Developer Force (<a href="http://developer.force.com">http://developer.force.com</a>) released its new library.  Here are a few of them.  All can be found at <a href="http://wiki.developerforce.com/index.php/Documentation">http://wiki.developerforce.com/index.php/Documentation</a>.</p>
<p>Workbook<br />
<a href="http://www.salesforce.com/us/developer/docs/workbook/index.htm">http://www.salesforce.com/us/developer/docs/workbook/index.htm</a></p>
<p>Fundamentals<br />
<a href="http://www.salesforce.com/us/developer/docs/fundamentals/index.htm">http://www.salesforce.com/us/developer/docs/fundamentals/index.htm</a></p>
<p>Cookbook<br />
<a href="http://www.salesforce.com/us/developer/docs/cookbook/index.htm">http://www.salesforce.com/us/developer/docs/cookbook/index.htm</a></p>
<p>Apex Advanced Code Example<br />
<a href="http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_shopping_cart_example.htm">http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_shopping_cart_example.htm</a><br />
<a href="https://sites.secure.force.com/appexchange/listingDetail?listingId=a0N30000001saDCEAY">https://sites.secure.force.com/appexchange/listingDetail?listingId=a0N30000001saDCEAY</a></p>
<p>And many more to come!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2009/11/19/new-developer-library-released.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overload Apex Class to be Controller AND Extension</title>
		<link>http://www.x2od.com/2009/07/24/overload-apex-class-to-be-controller-and-extension.html</link>
		<comments>http://www.x2od.com/2009/07/24/overload-apex-class-to-be-controller-and-extension.html#comments</comments>
		<pubDate>Fri, 24 Jul 2009 20:01:00 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Visualforce]]></category>
		<category><![CDATA[X-Squared On Demand]]></category>
		<category><![CDATA[Eclipse IDE]]></category>
		<category><![CDATA[Force.com Platform]]></category>
		<category><![CDATA[New Features]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=682</guid>
		<description><![CDATA[Coding the new premium version of Mass Update Contacts (details to come), I replaced the two parts of the page with Apex Components. This will allow the app to support custom address fields and international address formats.
I didn't want to write one ControllerExtension for the main page, a CustomController for the view section component, and another CustomController for the pageblocktable component. So here is the overloaded class constructor. Note that this works because an extension passes the StandardController to the constructor, and a CustomController passes nothing:]]></description>
			<content:encoded><![CDATA[<p>Wow - today brought an interesting discovery.  Here's the situation:</p>
<p>Coding the new premium version of Mass Update Contacts (details to come), I replaced the two parts of the page with Apex Components.  This will allow the app to support custom address fields and international address formats.</p>
<p>I didn't want to write one ControllerExtension for the main page, a CustomController for the view section component, and another CustomController for the pageblocktable component.  So here is the overloaded class constructor.  Note that this works because an extension passes the StandardController to the constructor, and a CustomController passes nothing:</p>

<pre class="brush: java;">
public with sharing class VersatileClass {

private Account account;

public VersatileClass(){
	system.debug('OPERATING AS CONTROLLER');
		if(System.currentPageReference().getParameters().get('id')==null){
			//Include error checking here
		} else{
			string AId = System.currentPageReference().getParameters().get('id');
			account = [select id, name from Account where id = :AId];
			//And whatever else you want to do
		}
}

public VersatileClass(ApexPages.StandardController controller) {
	system.debug('OPERATING AS EXTENSION');
		if(System.currentPageReference().getParameters().get('id')==null){
			//Include error checking here
		} else{
			this.account = (Account)controller.getRecord();
			//And whatever else you want to do
		}
	}
}
</pre>
<p>Enjoy!  This should save people a lot of time.</p>]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2009/07/24/overload-apex-class-to-be-controller-and-extension.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Email Inbox Version 2</title>
		<link>http://www.x2od.com/2009/07/15/email-inbox-version-2.html</link>
		<comments>http://www.x2od.com/2009/07/15/email-inbox-version-2.html#comments</comments>
		<pubDate>Thu, 16 Jul 2009 01:39:41 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Visualforce]]></category>
		<category><![CDATA[salesforce.com]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[X-Squared On Demand]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=664</guid>
		<description><![CDATA[Well, it took about ten minutes from the release of Email Inbox Version 1 for people to request additional features, screenshots, etc. Version 2 includes some neat icons, and this post has the multiple-times-requested screenshots we promised. First, a teaser screenshot. (There's a better one below the code.) Here's the updated Visualforce code: &#60;apex:page controller=&#34;EmailMessageController&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Well, it took about ten minutes from the release of <a href="http://www.x2od.com/2009/07/14/visualforce-email-inbox.html">Email Inbox Version 1</a> for people to request additional features, screenshots, etc.  Version 2 includes some neat icons, and this post has the multiple-times-requested screenshots we promised.</p>
<p>First, a teaser screenshot.  (There's a better one below the code.)</p>
<center><a href="http://www.x2od.com/wp/uploads/EmailInbox-screenshot.JPG"><img src="http://www.x2od.com/wp/uploads/EmailInbox-screenshot-300x50.jpg" alt="EmailInbox screenshot" title="EmailInbox screenshot" width="300" height="50" class="aligncenter size-medium wp-image-672" /></a></center>
<p>Here's the updated Visualforce code:</p>
<pre class="brush: xml;">
&lt;apex:page controller=&quot;EmailMessageController&quot; action=&quot;{!ViewData}&quot;&gt;
    &lt;apex:sectionHeader title=&quot;Email Messages&quot; subtitle=&quot;&quot;&gt;&lt;/apex:sectionHeader&gt;
    &lt;apex:pageblock id=&quot;emailblock&quot;&gt;
        &lt;apex:facet name=&quot;header&quot;&gt;
            &lt;apex:form &gt;
                &lt;apex:panelGrid styleClass=&quot;list&quot;
                    columnClasses=&quot;pbTitle,pbButton,pbHelp&quot; columns=&quot;3&quot; border=&quot;0&quot;
                    cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;
                    &lt;apex:outputLabel &gt;&lt;h3&gt;Messages&lt;/h3&gt;&lt;/apex:outputLabel&gt;
                    &lt;apex:commandButton value=&quot; Refresh &quot; styleClass=&quot;btn&quot;
                        action=&quot;{!ViewData}&quot; rerender=&quot;emailblock&quot;&gt;&lt;/apex:commandButton&gt;
                    &lt;apex:SelectList value=&quot;{!wheretext}&quot; size=&quot;1&quot;  id=&quot;controllerselectlist&quot;&gt;
                        &lt;apex:actionSupport event=&quot;onchange&quot; action=&quot;{!ViewData}&quot;
                            reRender=&quot;emailblock&quot;&gt;&lt;/apex:actionSupport&gt;
                        &lt;apex:selectOptions value=&quot;{!views}&quot; /&gt;
                    &lt;/apex:SelectList&gt;
                &lt;/apex:panelGrid&gt;
            &lt;/apex:form&gt;
        &lt;/apex:facet&gt;
        &lt;apex:form &gt;
            &lt;apex:pageblocktable value=&quot;{!Messages}&quot; var=&quot;e&quot; id=&quot;emailtable&quot;
                bgcolor=&quot;#F3F3EC&quot; styleClass=&quot;list&quot; rowClasses=&quot;dataRow&quot;
                onRowMouseOver=&quot;hiOn(this);&quot; onRowMouseOut=&quot;hiOff(this);&quot;&gt;
                &lt;apex:column &gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;{!$ObjectType.EmailMessage.fields.Subject.label}{!IF(sortExpression=='Subject', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;Subject&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                    &lt;apex:outputLink value=&quot;/{!e.Id}&quot; target=&quot;_blank&quot;&gt;{!e.Subject}&lt;/apex:outputLink&gt;
                &lt;/apex:column&gt;
                &lt;apex:column &gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                    {!$ObjectType.Contact.fields.Name.label}
                    &lt;/apex:facet&gt;
                    &lt;apex:outputLink value=&quot;/{!e.Parent.ContactId}&quot; target=&quot;_blank&quot;
                        rendered=&quot;{!IF(e.Parent.ContactId != '',true,false)}&quot;&gt;{!e.FromName}&lt;/apex:outputLink&gt;
                    &lt;apex:outputtext value=&quot;{!e.FromName}&quot;
                        rendered=&quot;{!IF(e.Parent.ContactId != '',false,true)}&quot; /&gt;
                &lt;/apex:column&gt;
                &lt;apex:column &gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                    {!$ObjectType.Account.fields.Name.label}
                    &lt;/apex:facet&gt;
                    &lt;apex:outputLink value=&quot;/{!e.Parent.AccountId}&quot; target=&quot;_blank&quot;&gt;{!e.Parent.Account.Name}&lt;/apex:outputLink&gt;
                &lt;/apex:column&gt;
                &lt;apex:column value=&quot;{!e.FromAddress}&quot;&gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;{!$ObjectType.EmailMessage.fields.FromAddress.label}{!IF(sortExpression=='FromAddress', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;FromAddress&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                &lt;/apex:column&gt;
                &lt;apex:column value=&quot;{!e.Status}&quot;&gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;{!$ObjectType.EmailMessage.fields.Status.label}{!IF(sortExpression=='Status', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;Status&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                &lt;/apex:column&gt;
                &lt;apex:column value=&quot;{!e.MessageDate}&quot;&gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;{!$ObjectType.EmailMessage.fields.MessageDate.label}{!IF(sortExpression=='MessageDate', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;MessageDate&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                &lt;/apex:column&gt;
                &lt;apex:column &gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;Inbound/Outbound{!IF(sortExpression=='Incoming', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;Incoming&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                    &lt;apex:image url=&quot;/img/emailInbound.gif&quot; rendered=&quot;{!e.Incoming}&quot; /&gt;
                    &lt;apex:image url=&quot;/img/emailOutbound.gif&quot; rendered=&quot;{!NOT(e.Incoming)}&quot; /&gt;
                    &lt;!--&lt;apex:outputfield value=&quot;{!e.Incoming}&quot; rendered=&quot;{!NOT(e.Incoming)}&quot; /&gt;--&gt;
                &lt;/apex:column&gt;
                &lt;apex:column &gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;Attachment{!IF(sortExpression=='HasAttachment', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;HasAttachment&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                    &lt;apex:image url=&quot;/img/emailHasAttach.gif&quot; rendered=&quot;{!e.HasAttachment}&quot;/&gt;
                    &lt;apex:outputfield value=&quot;{!e.HasAttachment}&quot; rendered=&quot;{!NOT(e.HasAttachment)}&quot; /&gt;
                &lt;/apex:column&gt;
                &lt;apex:column &gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;{!$ObjectType.Case.fields.CaseNumber.label}{!IF(sortExpression=='ParentId', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;ParentId&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                    &lt;apex:outputLink value=&quot;/{!e.ParentId}&quot;&gt;{!e.Parent.CaseNumber}&lt;/apex:outputLink&gt;

                &lt;/apex:column&gt;
                &lt;apex:column value=&quot;{!e.ToAddress}&quot;&gt;
                    &lt;apex:facet name=&quot;header&quot;&gt;
                        &lt;apex:commandLink action=&quot;{!ViewData}&quot;
                            value=&quot;{!$ObjectType.EmailMessage.fields.ToAddress.label}{!IF(sortExpression=='ToAddress', 
                            IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
                            &lt;apex:param value=&quot;ToAddress&quot; name=&quot;column&quot;
                                assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
                        &lt;/apex:commandLink&gt;
                    &lt;/apex:facet&gt;
                &lt;/apex:column&gt;
            &lt;/apex:pageblocktable&gt;
        &lt;/apex:form&gt;
    &lt;/apex:pageblock&gt;
&lt;/apex:page&gt;
</pre>
<p>The Apex controller code has not changed from <a href="http://www.x2od.com/2009/07/14/visualforce-email-inbox.html">Version 1</a>.  </p>
<p>Here's the screenshot, which includes some emails from around the Salesforce ecosystem:</p>
<center>
<div id="attachment_666" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.x2od.com/wp/uploads/EmailInbox-screenshot-2.JPG"><img src="http://www.x2od.com/wp/uploads/EmailInbox-screenshot-2-300x110.jpg" alt="Incoming (and auto-response) Emails" title="EmailInbox screenshot" width="300" height="110" class="size-medium wp-image-666" /></a><p class="wp-caption-text">Incoming (and auto-response) Emails</p></div></center>

Things to notice and things we've learned:
<ol>
	<li>Salesforce will search ANY Email field to match an incoming email to a Contact. (nice job, Salesforce!) - We know because <a href="http://www.pocketsoap.com">Simon Fell</a>'s Contact.Email is sfell at salesforce.com, and his Contact.Secondary_Email__c is simon at fell.com in the sandbox.  Ditto (with her own email addresses) for <a href="http://www.xlerate.ca/">Irene Brodt</a>.</li>
	<li>Auto-response emails will be included.  We turned off auto-response after a few emails came in.</li>
	<li>Though the Email Address field is coded just to show the field value, Salesforce includes the Gmail link. (We assume that's because we activated the integration, but we may be wrong. Has anyone not activated that?)</li>
	<li>We did not (yet) implement the <a href="http://blogs.salesforce.com/support/2009/03/autocreating-a.html">super Email/Web2Case trigger code</a> that Marco Casalaina posted in the Salesforce Support Blog, but if we had, then every email in the list would be associated with a Contact. (Example: Michael Smith of Force2b.net, who will be a Contact from now on!)</li>
</ol>
Enjoy!  ]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2009/07/15/email-inbox-version-2.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Visualforce Email Inbox</title>
		<link>http://www.x2od.com/2009/07/14/visualforce-email-inbox.html</link>
		<comments>http://www.x2od.com/2009/07/14/visualforce-email-inbox.html#comments</comments>
		<pubDate>Tue, 14 Jul 2009 18:00:26 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Visualforce]]></category>
		<category><![CDATA[X-Squared On Demand]]></category>
		<category><![CDATA[salesforce.com]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=642</guid>
		<description><![CDATA[Sonny Cloward, SysAd at Rainforest Alliance, approached us about writing a Visualforce page to display all incoming emails for a given Case Queue. This led to quite a few interesting discoveries. Here's how we handled the project:]]></description>
			<content:encoded><![CDATA[<p>Sonny Cloward, SysAd at <a href="http://rainforestalliance.org/">Rainforest Alliance</a>, approached us about writing a Visualforce page to display all incoming emails for a given Case Queue.  This led a few interesting discoveries.  Here's how we handled the (donated time) project:</p>
<p>First, the page was built upon the <a href="http://salesforcesource.blogspot.com/2008/11/adding-sorting-capability-to.html">template Sam Arjimandi built</a> at <a href="http://salesforcesource.blogspot.com">Salesforce<Source></a>.  Instead of the Account object, we substituted the <a href="http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_emailmessage.htm">EmailMessage object</a>.  But something didn't work.  </p>
Fields have certain <a href="http://www.salesforce.com/us/developer/docs/api/Content/access_for_fields.htm">attributes</a>:<br />
            <table cellpadding="4" cellspacing="0" summary="" class="featureTable">
                <thead align="left">
                    <tr>
                        <th class="featureTableHeader" width="25%" id="d9607e40" colspan="1" rowspan="1">Property</th>
                        <th class="featureTableHeader" width="75%" id="d9607e43" colspan="1" rowspan="1">Description</th>
                    </tr>
                </thead>
                <tbody>

                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435186"><a name="i1435186" shape="rect"><!-- --></a>Autonumber</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">The <span>API</span> creates an autonumber.</td>
                    </tr>
                    <tr>

                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435191"><a name="i1435191" shape="rect"><!-- --></a>Create</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">Value for the field can be specified during create using the <span>API</span>.</td>
                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435199"><a name="i1435199" shape="rect"><!-- --></a>Defaulted on create</span>

                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">When created, a default value is supplied if no other value
is specified.</td>
                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435204"><a name="i1435204" shape="rect"><!-- --></a>Delete</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">Value for the field can be deleted using the <span>API</span>.</td>

                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435212"><a name="i1435212" shape="rect"><!-- --></a>Filter</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">Can be used as filter criteria in a SOQL query FROM or WHERE
clause.</td>
                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="access_lookup"><a name="access_lookup" shape="rect"><!-- --></a>idLookup</span>

                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">Can be used to specify a record in an <span><a href="sforce_api_calls_upsert.htm" shape="rect"><span><samp class="codeph">upsert()</samp></span></a></span> call. The <span class="fieldName">Id</span> field of each
object has this property and some <span class="fieldName">Name</span> fields. <span>There are exceptions, so check for the property in
any object you wish to <a href="sforce_api_calls_upsert.htm" shape="rect"><span><samp class="codeph">upsert()</samp></span>.</a></span></td>

                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435217"><a name="i1435217" shape="rect"><!-- --></a>Nillable</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">The field can contain a null value.</td>
                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435222"><a name="i1435222" shape="rect"><!-- --></a>Query</span>

                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">The field can be queried with SOQL using the <span>API</span>.</td>
                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435230"><a name="i1435230" shape="rect"><!-- --></a>Replicate</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">The value of the field can be replicated using the <span>API</span>.</td>

                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435238"><a name="i1435238" shape="rect"><!-- --></a>Restricted picklist</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">  A picklist that depends on the value of another picklist
for the values it displays.</td>
                    </tr>
                    <tr>

                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435244"><a name="i1435244" shape="rect"><!-- --></a>Retrieve</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">Value of the field can be retrieved using the <span>API</span>.</td>
                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="field_access_search"><a name="field_access_search" shape="rect"><!-- --></a>Search</span>

                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">Can be searched with SOSL using the <span>API</span>.</td>
                    </tr>
                    <tr>
                        <td width="25%" headers="d9607e40" colspan="1" rowspan="1"><span id="i1435259"><a name="i1435259" shape="rect"><!-- --></a>Update</span>
                        </td>
                        <td width="75%" headers="d9607e43" colspan="1" rowspan="1">Can be updated using the <span>API</span>.</td>

                    </tr>
                </tbody>
            </table>
<p>The important one here is "Filter" because (as the documentation states) this allows the field to be used in a WHERE clause.  Also, however, (as the documentation does not state) it allows the field to be used in an ORDER BY clause.  So all fields on the EmailMessage object that do not allow filtering/ordering had to be presented plainly, without Sam's cool PageBlockTable sorting features.
Once this was done, Sonny had some great ideas:</p>
<ol>
	<li>Show the email subject, but make that a hyperlink to the email message itself</li>
	<li>Link the Case Number (EmailMessage.ParentId) to the Case (EmailMessage.Parent)</li>
	<li>Show the Case Contact (EmailMessage.Parent.Contact.Name), linking to the Contact (EmailMessage.Parent.ContactId)</li>
	<li>Show the Case Account (EmailMessage.Parent.Account.Name), linking to the Account (EmailMessage.Parent.AccountId)</li>
	<li>Provide filters - Incoming only, Unread only, etc.</li>
</ol>
You'll see in the Apex Code where we added parent object fields to the SOQL query, and where we used a List<SelectOption> to populate the query's filter.  
<pre class="brush: java;">
public with sharing class EmailMessageController {
   public String EmailMessage { get; set; }
   private List&lt;EmailMessage&gt; messages;
   private String sortDirection = 'ASC';
   private String sortExp = 'MessageDate';
   public String wheretext;

   public EmailMessageController(){
   	wheretext = '';
   }

   public String sortExpression { get {
        return sortExp;
     }
     set {
       //if the column is clicked on then switch between Ascending and Descending modes
       if (value == sortExp)
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
       else
         sortDirection = 'ASC';
       sortExp = value;
     }
   }
   
public void setWhereText(String value) {  
   whereText = value;
 }
public string getWhereText(){
	return wheretext;
}
   
public List&lt;SelectOption&gt; getViews(){
	List&lt;SelectOption&gt; options = new List&lt;SelectOption&gt;();
	options.add(new SelectOption('WHERE e.id != null','All'));
	options.add(new SelectOption('WHERE e.Incoming = true AND e.Status = \'0\' ','Incoming Unread'));
	options.add(new SelectOption('WHERE e.Incoming = true AND e.Status = \'1\' ','Incoming Read'));
        options.add(new SelectOption('WHERE e.Incoming = true','All Incoming'));
        options.add(new SelectOption('WHERE e.Incoming = false','All Outgoing'));
        options.add(new SelectOption('WHERE e.ToAddress = \'support@x2od.com\'','Support Queue'));
        options.add(new SelectOption('WHERE e.ToAddress = \'support2@x2od.com\'','Support Queue2')); //etc.
        return options;
}

 public String getSortDirection() {
    //if not column is selected 
    if (sortExpression == null || sortExpression == '')
      return 'ASC';
    else
     return sortDirection;
 }

 public void setSortDirection(String value) {  
   sortDirection = value;
 }
  
   public List&lt;EmailMessage&gt; getMessages() {
       return messages;
   }
   
   public PageReference ViewData() {
       //build the full sort expression
       string sortFullExp = sortExpression  + ' ' + sortDirection;
      
       //query the database based on the sort expression
       messages = Database.query('Select e.FromAddress, e.Parent.ContactId, e.Parent.Contact.Name, e.Parent.Account.Name, e.ToAddress, e.Parent.CaseNumber, e.Parent.AccountId, e.TextBody, e.SystemModstamp, e.Subject, e.Status, e.ParentId, e.MessageDate, e.LastModifiedDate, e.LastModifiedById, e.IsDeleted, e.Incoming, e.Id, e.HtmlBody, e.Headers, e.HasAttachment, e.FromName, e.CreatedDate, e.CreatedById, e.CcAddress, e.BccAddress, e.ActivityId From EmailMessage e ' + wheretext + ' order by ' + sortFullExp + ' limit 1000');
       return null;
   }
}
</pre>
<p>And here's the Visualforce Page:</p>
<pre class="brush: xml;">
&lt;apex:page controller=&quot;EmailMessageController&quot; action=&quot;{!ViewData}&quot;&gt;
	&lt;apex:sectionHeader title=&quot;Email Messages&quot; subtitle=&quot;&quot;&gt;&lt;/apex:sectionHeader&gt;
	&lt;apex:pageblock id=&quot;emailblock&quot;&gt;
		&lt;apex:facet name=&quot;header&quot;&gt;
			&lt;apex:form&gt;
				&lt;apex:panelGrid styleClass=&quot;list&quot;
					columnClasses=&quot;pbTitle,pbButton,pbHelp&quot; columns=&quot;3&quot; border=&quot;0&quot;
					cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;
					&lt;apex:outputLabel&gt;&lt;h3&gt;Messages&lt;/h3&gt;&lt;/apex:outputLabel&gt;
					&lt;apex:commandButton value=&quot; Refresh &quot; styleClass=&quot;btn&quot;
						action=&quot;{!ViewData}&quot; rerender=&quot;emailblock&quot;&gt;&lt;/apex:commandButton&gt;
					&lt;apex:SelectList value=&quot;{!wheretext}&quot; size=&quot;1&quot;	id=&quot;controllerselectlist&quot;&gt;
						&lt;apex:actionSupport event=&quot;onchange&quot; action=&quot;{!ViewData}&quot;
							reRender=&quot;emailblock&quot;&gt;&lt;/apex:actionSupport&gt;
						&lt;apex:selectOptions value=&quot;{!views}&quot; /&gt;
					&lt;/apex:SelectList&gt;
				&lt;/apex:panelGrid&gt;
			&lt;/apex:form&gt;
		&lt;/apex:facet&gt;
		&lt;apex:form&gt;
			&lt;apex:pageblocktable value=&quot;{!Messages}&quot; var=&quot;e&quot; id=&quot;emailtable&quot;
				bgcolor=&quot;#F3F3EC&quot; styleClass=&quot;list&quot; rowClasses=&quot;dataRow&quot;
				onRowMouseOver=&quot;hiOn(this);&quot; onRowMouseOut=&quot;hiOff(this);&quot;&gt;
				&lt;apex:column&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.EmailMessage.fields.Subject.label}{!IF(sortExpression=='Subject', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;Subject&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
					&lt;apex:outputLink value=&quot;/{!e.Id}&quot; target=&quot;_blank&quot;&gt;{!e.Subject}&lt;/apex:outputLink&gt;
				&lt;/apex:column&gt;
				&lt;apex:column&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
					{!$ObjectType.Contact.fields.Name.label}
					&lt;/apex:facet&gt;
					&lt;apex:outputLink value=&quot;/{!e.Parent.ContactId}&quot; target=&quot;_blank&quot;
						rendered=&quot;{!IF(e.Parent.ContactId != '',true,false)}&quot;&gt;{!e.FromName}&lt;/apex:outputLink&gt;
					&lt;apex:outputtext value=&quot;{!e.FromName}&quot;
						rendered=&quot;{!IF(e.Parent.ContactId != '',false,true)}&quot; /&gt;
				&lt;/apex:column&gt;
				&lt;apex:column&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
					{!$ObjectType.Account.fields.Name.label}
					&lt;/apex:facet&gt;
					&lt;apex:outputLink value=&quot;/{!e.Parent.AccountId}&quot; target=&quot;_blank&quot;&gt;{!e.Parent.Account.Name}&lt;/apex:outputLink&gt;
				&lt;/apex:column&gt;
				&lt;apex:column value=&quot;{!e.FromAddress}&quot;&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.EmailMessage.fields.FromAddress.label}{!IF(sortExpression=='FromAddress', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;FromAddress&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
				&lt;/apex:column&gt;
				&lt;apex:column value=&quot;{!e.Status}&quot;&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.EmailMessage.fields.Status.label}{!IF(sortExpression=='Status', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;Status&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
				&lt;/apex:column&gt;
				&lt;apex:column value=&quot;{!e.MessageDate}&quot;&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.EmailMessage.fields.MessageDate.label}{!IF(sortExpression=='MessageDate', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;MessageDate&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
				&lt;/apex:column&gt;
				&lt;apex:column value=&quot;{!e.Incoming}&quot;&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.EmailMessage.fields.Incoming.label}{!IF(sortExpression=='Incoming', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;Incoming&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
				&lt;/apex:column&gt;
				&lt;apex:column value=&quot;{!e.HasAttachment}&quot;&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.EmailMessage.fields.HasAttachment.label}{!IF(sortExpression=='HasAttachment', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;HasAttachment&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
				&lt;/apex:column&gt;
				&lt;apex:column&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.Case.fields.CaseNumber.label}{!IF(sortExpression=='ParentId', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;ParentId&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
					&lt;apex:outputLink value=&quot;/{!e.ParentId}&quot;&gt;{!e.Parent.CaseNumber}&lt;/apex:outputLink&gt;

				&lt;/apex:column&gt;
				&lt;apex:column value=&quot;{!e.ToAddress}&quot;&gt;
					&lt;apex:facet name=&quot;header&quot;&gt;
						&lt;apex:commandLink action=&quot;{!ViewData}&quot;
							value=&quot;{!$ObjectType.EmailMessage.fields.ToAddress.label}{!IF(sortExpression=='ToAddress', 
							IF(sortDirection='ASC','▼','▲'),'')}&quot;&gt;
							&lt;apex:param value=&quot;ToAddress&quot; name=&quot;column&quot;
								assignTo=&quot;{!sortExpression}&quot;&gt;&lt;/apex:param&gt;
						&lt;/apex:commandLink&gt;
					&lt;/apex:facet&gt;
				&lt;/apex:column&gt;
			&lt;/apex:pageblocktable&gt;
		&lt;/apex:form&gt;
	&lt;/apex:pageblock&gt;
&lt;/apex:page&gt;
</pre>
<p>There are some other cool bits: </p>
<p>If there is no Case.Contact, the table will display the FromName, pulled from the email message.</p>
<p>An interesting point: You may notice that EmailStatus is presented in numerical form.  For instance, Incoming Unread is 0, Incoming Read is 1, etc.  The documentation, however, says, "Read only. The status of the email. For example, “New,” “Unread,” “Replied,” “Sent.”"  So we're not sure of the exact mapping.  3 seems to be Sent, so 2 is probably Replied... but we're not sure.</p>
<p>Don't forget: EmailMessage has two lookups (foreign key): Case, and Activity.  This Activity is the task created when Salesforce receives the email, and is - according to the documentation - assigned to the Case Owner.  We're not sure what happens when the Case is owned by a Queue.  Feel free to comment and share your experiences.</p>
<p>
That's it!  Enjoy.</p>]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2009/07/14/visualforce-email-inbox.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Checkbox Formula Field in Visualforce</title>
		<link>http://www.x2od.com/2009/05/21/checkbox-formula-field-in-visualforce.html</link>
		<comments>http://www.x2od.com/2009/05/21/checkbox-formula-field-in-visualforce.html#comments</comments>
		<pubDate>Thu, 21 May 2009 17:00:52 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Visualforce]]></category>
		<category><![CDATA[salesforce.com]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=609</guid>
		<description><![CDATA[In keeping with the theme of checkboxes and formulas, we provide an easy way to create a checkbox formula field using Visualforce: the checkbox tag with the disabled attribute:]]></description>
			<content:encoded><![CDATA[<p>In keeping with the theme of <a href="http://www.x2od.com/2009/05/11/standard-checkbox-images.html">checkboxes</a> and formulas, we provide an easy way to create a checkbox formula field using Visualforce: the checkbox tag with the disabled attribute:</p>
<p>First, the Apex controller method:</p>
<pre class="brush: java;">
public boolean checkformula(){
boolean b = [insert formula here];
return b;
}
</pre>
<p>And the visualforce code:</p>
<pre class="brush: xml;">
&lt;apex:inputCheckbox disabled=&quot;true&quot; value=&quot;{!checkformula}&quot;/&gt;
</pre>
<p>That's it!  Of course, there are many ways to set up the Apex method, but the important piece is the Visualforce markup.  By using a tag usually associated with an edit page but making it disabled, the checkbox becomes an output field.</p>]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2009/05/21/checkbox-formula-field-in-visualforce.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pulling Code Out of Triggers</title>
		<link>http://www.x2od.com/2009/05/18/pulling-code-out-of-triggers.html</link>
		<comments>http://www.x2od.com/2009/05/18/pulling-code-out-of-triggers.html#comments</comments>
		<pubDate>Mon, 18 May 2009 17:00:41 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Summer 09]]></category>
		<category><![CDATA[salesforce.com]]></category>
		<category><![CDATA[Eclipse IDE]]></category>
		<category><![CDATA[Force.com Platform]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=583</guid>
		<description><![CDATA[To date, triggers have only been accessible on each object's setup page, leading to a lot of hunting for code within the Salesforce CRM application. Even in Eclipse, switching between the Class and Trigger folders for a given project can be a pain.

These pains can partially be alleviated by keeping all Apex code in one place--as Apex Classes.

Read more: http://www.x2od.com/?p=583&#038;preview=true#ixzz0FncHsqPw&#038;B]]></description>
			<content:encoded><![CDATA[<p>To date, triggers have only been accessible on each object's setup page, leading to a lot of hunting for code within the Salesforce CRM application.  Even in Eclipse, switching between the Class and Trigger folders for a given project can be a pain.</p>
<p>These pains can partially be alleviated by keeping all Apex code in one place--as Apex Classes.  With the addition of a consolidated trigger list in <a href="http://wiki.developerforce.com/index.php/Summer_09">Summer09</a>, some may feel that this post is superfluous, but consolidating code in one place, combined with the trigger list, will lead to a better development, debugging, and org administration experience.</p>

<p>First, here's a sample trigger (written by <a href="http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce">Jeff Douglas</a>):</p>
<pre class="brush: java;">
trigger AddOwnerColor on Account (before insert, before update) {

    // create a set of all the unique ownerIds
    Set&amp;lt;Id&amp;gt; ownerIds = new Set&amp;lt;Id&amp;gt;();
    for (Account a : Trigger.new)
        ownerIds.add(a.OwnerId);    

    // query for all the User records for the unique userIds in the records
    // create a map for a lookup / hash table for the user info
    Map&amp;lt;Id, User&amp;gt; owners = new Map&amp;lt;Id, User&amp;gt;([Select Favorite_Color__c from User Where Id in: ownerIds]);   

    // iterate over the list of records being processed in the trigger and
    // set the color before being inserted or updated
    for (Account a : Trigger.new)
        a.Owner_Favorite_Color__c = owners.get(a.OwnerId).Favorite_Color__c; 
}
</pre>
<p>Let's pull the code from the trigger into an Apex Class and leave a reference to that class & method in the trigger.  We need to pass the list Trigger.new as a parameter to the new class's method:<p>
The trigger:
<pre class="brush: java;">
trigger AddOwnerColor on Account (before insert, before update) {
AccountTriggers.AddOwnerColor(Trigger.new);
}
</pre>
And the class:
<pre class="brush: java;">
public class AccountTriggers {
    public static void AddOwnerColor(Account[] accts) {

    // create a set of all the unique ownerIds
    Set&amp;lt;Id&amp;gt; ownerIds = new Set&amp;lt;Id&amp;gt;();
    for (Account a : accts)
        ownerIds.add(a.OwnerId);    

    // query for all the User records for the unique userIds in the records
    // create a map for a lookup / hash table for the user info
    Map&amp;lt;Id, User&amp;gt; owners = new Map&amp;lt;Id, User&amp;gt;([Select Favorite_Color__c from User Where Id in: ownerIds]);   

    // iterate over the list of records being processed in the trigger and
    // set the color before being inserted or updated
    for (Account a : accts)
        a.Owner_Favorite_Color__c = owners.get(a.OwnerId).Favorite_Color__c; 
    } // close AddOwnerColor
}
</pre>
While this may seem trivial, it has a few advantages:<p>
<ul>
	<li>Easier to work in Eclipse (all code in the Classes section)</li>
	<li>Easier to write test code (can see tests and their associated methods in one place)</li>
	<li>Can promote code reuse by allowing other classes and triggers to call the same method.</li>
	<li>For those who like to include test code in the same class as the regular Class, this allows them to do so.</li>
</ul>
<p>There's a catch (there always is):
<p>You should comment into your Class which trigger is calling the class because otherwise, it is almost impossible to see at a glance where the code flows.  This will especially help when writing and debugging tests.
<p>Just a matter of personal style: It may be a good idea to write an Apex Class for each object's triggers (such as class AccountTriggers above).  Code reuse is still possible, but it can track where triggers were originally used.
<p>Happy coding!]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2009/05/18/pulling-code-out-of-triggers.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Hacking a Case Comment Trigger</title>
		<link>http://www.x2od.com/2009/04/01/hacking-a-case-comment-trigger.html</link>
		<comments>http://www.x2od.com/2009/04/01/hacking-a-case-comment-trigger.html#comments</comments>
		<pubDate>Wed, 01 Apr 2009 20:01:23 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Force.com Builder]]></category>
		<category><![CDATA[Force.com Platform]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=477</guid>
		<description><![CDATA[There has been some chatter about asking the salesforce.com team to include the CaseComment object in the "triggerable" list.  After conferring with JP Seabury (ForceMonkey), we designed a Rube Goldberg-esque solution to the problem.  ]]></description>
			<content:encoded><![CDATA[<p>There has been some discussion about asking the salesforce.com team to include the CaseComment object in the &#8220;triggerable&#8221; list.  After conferring with JP Seabury (<a href="http://forcemonkey.blogspot.com">ForceMonkey</a>), we designed a Rube Goldberg-esque solution to the problem.<br />
<span id="more-477"></span><br />
<big><b>Business Use-Case</b></big><br />
Whenever a user adds a comment to a case, add him/her to the Case Team.</p>
<h3>The Plan</h3>
<p>Here it is, in general terms:</p>
<ol>
<li>Workflow on <code>Case Comment</code> sends email to Apex email service</li>
<li>Email service parses <code>CaseId</code> and <code>UserId</code> and adds the User to the Case Team
</ol>
<p><big><b>Case Comment Workflow</b></big><br />
Use existing workflow functionality to send an email whenever a comment is added to a case.</p>
<ul>
<li>Template Includes:</li>
<li>Case Id</li>
<li>User Id</li>
</ul>
<p>The recipient is an Apex email service that we can code using the <a href="http://blog.sforce.com/sforce/2008/06/create-ideas-fr.html">Email to Idea</a> example by Rasmus Mencke.<br />
For more information on setting up the <a href="http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_inbound_using.htm"><code>InboundEmail object</code></a>, check the <a href="http://www.salesforce.com/us/developer/docs/apexcode/index.htm">Apex documentation</a>.<br />
<big><b>Apex Class</b></big><br />
The class needs to add the User to the Case Team, which requires a few fields:</p>
<ul>
<li><code>MemberId</code> &#8211; UserId (received from the email)</li>
<li><code>ParentId</code> &#8211; CaseId (received from the email)</li>
<li><code>TeamRoleId</code> &#8211; Could be hardcoded or found in a SOQL SELECT statement. A better practice would be to pass the name or <code>Id</code> in the email (coding it in the template), allowing the use of one Apex Class with multiple Case workflow email templates.</li>
<li><strong>NOT</strong> <code>TeamTemplateMemberId</code> &#8211; This is not a createable field. It is only available when Case Team members are added automatically in Salesforce CRM.</li>
</ul>
<p>That&#8217;s it!  The unique part of this is that we&#8217;re sending an email FROM Salesforce TO Salesforce.<br />
Thinking outside the box &#8211; isn&#8217;t it more fun?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2009/04/01/hacking-a-case-comment-trigger.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Project: Lookup to Picklist</title>
		<link>http://www.x2od.com/2008/11/11/project-lookup-to-picklist.html</link>
		<comments>http://www.x2od.com/2008/11/11/project-lookup-to-picklist.html#comments</comments>
		<pubDate>Tue, 11 Nov 2008 05:56:37 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Visualforce]]></category>
		<category><![CDATA[salesforce.com]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Dreamforce]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[X-Squared On Demand]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=232</guid>
		<description><![CDATA[In the first installment of post-Dreamforce projects, I present a mechanism to present the user with all available Lookup options in a picklist. This should simplify some Visualforce pages. (Credit to the Developers Guide from the new post-Dreamforce Library.) The key to the code is the ability to customize the SOQL statement to focus as [...]]]></description>
			<content:encoded><![CDATA[<p>In the first installment of post-Dreamforce projects, I present a mechanism to present the user with all available Lookup options in a picklist.  This should simplify some Visualforce pages.  (Credit to the <a href="http://wiki.apexdevnet.com/index.php/force_library">Developers Guide</a> from the new post-Dreamforce Library.)</p>
<p>The key to the code is the ability to customize the SOQL statement to focus as much, or as little, as the developer would like.  Though the code I provide makes no limit on the number of records returned, I encourage developers to limit the select statement as seen fit.</p>
<p><strong>VF Page:</strong><br />
<pre class="brush: xml;">&lt;br /&gt;
&lt;apex:page standardcontroller=&quot;Child__c&quot; Extensions=&quot;ChildExtension&quot;&gt;&lt;br /&gt;
&lt;apex:messages &gt;&lt;/apex:messages&gt;&lt;br /&gt;
&lt;apex:form &gt;&lt;br /&gt;
&lt;apex:pageBlock mode=&quot;edit&quot; id=&quot;thePageBlock&quot;&gt;&lt;br /&gt;
&lt;apex:pageBlockButtons &gt;&lt;br /&gt;
&lt;apex:commandButton value=&quot;Save&quot; action=&quot;{!save}&quot;/&gt;&lt;br /&gt;
&lt;apex:commandButton value=&quot;Cancel&quot; action=&quot;{!cancel}&quot;/&gt;&lt;br /&gt;
&lt;/apex:pageBlockButtons&gt;&lt;br /&gt;
&lt;apex:pageblockSection id=&quot;ParentList&quot; title=&quot;1&quot;&gt;&lt;br /&gt;
&lt;apex:pageBlockSectionItem &gt;&lt;br /&gt;
&lt;apex:outputLabel value=&quot;Parent&quot; for=&quot;p&quot;/&gt;&lt;br /&gt;
&lt;apex:selectList id=&quot;p&quot; value=&quot;{!Child__c.Parent__c}&quot; size=&quot;1&quot; title=&quot;3&quot;&gt;&lt;br /&gt;
    &lt;apex:selectOptions value=&quot;{!ParentOptions}&quot;/&gt;&lt;br /&gt;
&lt;/apex:selectList&gt;&lt;br /&gt;
&lt;/apex:pageBlockSectionItem&gt;&lt;br /&gt;
&lt;/apex:pageblocksection&gt;&lt;br /&gt;
&lt;/apex:pageBlock&gt;&lt;br /&gt;
&lt;/apex:form&gt;&lt;br /&gt;
&lt;/apex:page&gt;&lt;br /&gt;
</pre></p>
<p><strong>Extension:</strong><br />
Note the SOQL query in Line 12 (below) that defines which records are included in the picklist.  You can include WHERE and LIMIT statements to get the picklist down to a manageable number of items.</p>
<p><pre class="brush: java;">&lt;br /&gt;
public class ChildExtension {&lt;/p&gt;
&lt;p&gt;private final Child__c child;&lt;/p&gt;
&lt;p&gt;    public ChildExtension(ApexPages.StandardController controller) {&lt;br /&gt;
        this.child = (Child__c)controller.getRecord();&lt;br /&gt;
        }&lt;/p&gt;
&lt;p&gt;    public List&lt;br /&gt;
&lt;selectOption&gt; PositionOptions {get&lt;br /&gt;
        {&lt;br /&gt;
        List&lt;br /&gt;
&lt;selectOption&gt; parents = new List&lt;br /&gt;
&lt;selectOption&gt;();&lt;br /&gt;
        for (Parent__c prt : [select name from Parent__c pt])&lt;br /&gt;
            parents.add(new selectOption(prt.id, prt.name));&lt;br /&gt;
            return parents;&lt;br /&gt;
        }&lt;br /&gt;
    private set;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
</pre></p>
<p>Because the parents List in the extension sends an ID to the application, it can successfully create a record with the parent Name in the picklist, but the ID in the background.  You can also substitute a different field for Name: If you prefer, put a contact&#8217;s email address or anything else.  Try it out!  </p>
<p>One more thing: This is an easy segue into another project, which will be a way to use the traditional lookup interface but to pre-filter the records available in the lookup.</p>
<p>And because I put this in every post: There&#8217;s a catch.  (There always is.)  I&#8217;m sure that somewhere in there, Salesforce would prefer that we not create a picklist of a few hundred items.  So please feel free to test the limits of this visualforce method, but please be prepared to filter your SOQL query.</p>
<p>On a separate note, I just had lunch with <a href="http://theenforcer.net">John Rotenstein aka The Enforcer</a>, who already put up a <a href="http://theenforcer.net/2008/11/x2od-the-enforcer/">photo</a> of our time in his office.  (I think the pirate logo was an accident, but it does add to my sinister persona, no?)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2008/11/11/project-lookup-to-picklist.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apex &#8211; The Basics</title>
		<link>http://www.x2od.com/2008/10/01/apex-the-basics.html</link>
		<comments>http://www.x2od.com/2008/10/01/apex-the-basics.html#comments</comments>
		<pubDate>Wed, 01 Oct 2008 17:45:02 +0000</pubDate>
		<dc:creator>David Schach</dc:creator>
				<category><![CDATA[Apex]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Force.com Platform]]></category>
		<category><![CDATA[Salesforce.com]]></category>

		<guid isPermaLink="false">http://www.x2od.com/?p=212</guid>
		<description><![CDATA[Jon Mountjoy has written another super post over at developer.force.com. This one is titled An Introduction to Apex. It goes through some of the basics of the language and provides some examples. Everyone should read this; beginners will get a better orientation than otherwise available, and experienced Apex developers will probably also learn a thing [...]]]></description>
			<content:encoded><![CDATA[<p>Jon Mountjoy has written another super post over at developer.force.com.  This one is titled <a href="http://wiki.apexdevnet.com/index.php?title=An_Introduction_to_Apex">An Introduction to Apex</a>.  It goes through some of the basics of the language and provides some examples. </p>
<p>Everyone should read this; beginners will get a better orientation than otherwise available, and experienced Apex developers will probably also learn a thing or two.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.x2od.com/2008/10/01/apex-the-basics.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
