<?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; Force.com Platform</title>
	<atom:link href="http://www.x2od.com/cat/salesforce/force-com-platform/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>
	</channel>
</rss>
