CRM

An Introduction to Exception Handling in Apex

Tuesday, January 12th, 2010

I was privileged to get to write an article for DeveloperForce.com on exception handling in Apex. I wrote this article not from the perspective of an expert on the subject, but as someone who really wanted to learn more about my options for using exception handling in my Apex code.

I’ve always had a nagging feeling that I wasn’t following best practices in my exception handling in Apex. In most cases I wasn’t doing any exception handling at all. I decided to write up what I have learned about error handling on the Force.com platform so that others could jump into it full-bore, and make their programs more robust and fault tolerant.

I hope it’s helpful to Apex programmers out there. Let me know what you think!

I’m going to Rwanda

Tuesday, December 29th, 2009

Today I got confirmation of some great news–in February I’ll be going to Rwanda for a week to help install an open source medical records system at a rural health clinic! I’ll be going with Lucky Gunasekara and meeting with Partners in Health.

I’m really excited to work with Lucky! This project will be amazing, and Lucky has a longer-term vision. He wants to answer the question: does cloud computing have a role in health systems in rural Africa? He’s planning some really interesting efforts in Kenya later this year. In time, I think Salesforce.com can be a boon in rural Africa, and I’m really interested to start the work to figure out if that time is now, soon, or farther down the road. I see this trip as a great first step on that path.

When I started working with Salesforce.com five years ago, I felt part of my role was to go down the path of trying to deeply customize the platform for nonprofits, and then to report on that experience, warts and all. I really enjoyed that work, and I hope to do similar work and storytelling around Salesforce.com in parts of the world where people assume the cloud can’t work.

But most of all, I’m really excited to have all my assumptions and expectations blown out of the water! I can’t wait to walk the roads, meet folks, and most of all listen and learn. I will be arriving with no answers, and I don’t expect to leave with any. For me, it’s all about getting to the right questions. There will be so much to take in, to start orienting myself to this new set of challenges.

Only just a week ago a friend pointed me to this piece by David Brancaccio on Partners In Health’s work in Rwanda. I was floored by the outcomes they are getting, and the thoughtful design of the community health program. I recommend watching it–it’s amazing.

So many times people look to technology as a savior. That never works in my estimation–human systems are how problems are solved. Technology can augment and transform those solutions, but it isn’t the answer. I’m incredibly excited by what’s already working in Rwanda, and am fired up to start thinking about how technology can help extend that impact.

I watched this video and emailed the link to a friend of mine with the message, “this is the work I want to do with my life–helping people build community-based systems that really work.” Four days later I got an email from Lucky inviting me to Rwanda, to the very region where that video was filmed. Serendipity is a fabulous thing!

Ahh! I’m going to Rwanda! I still can’t really believe it. It all feels so fast and so amazingly exciting. Of course I’ll be writing about the experience, as well as taking tons of pictures and videos. So much to do to prepare!

A great job opportunity

Friday, December 11th, 2009

Groundwire is an organization near and dear to my heart. I spent many wonderful years there back when it went by the name ONE/Northwest. My Salesforce-related career got it’s start there. Some of my most cherished relationships began there. And I was given the space and encouragement to grow exponentially as a person, technologist, and agent of change. I look back on those years very fondly–the work, the nonprofits we worked with, but most importantly the people.

Groundwire is looking for a top-notch CRM Consultant. Opportunities to join this amazing team are rare. If you are at all interested in using your Salesforce.com skills to make a difference in this world, you should follow up with Groundwire and make sure you don’t miss this chance.

Add a Donor Status Funnel to the Nonprofit Starter Pack

Wednesday, December 9th, 2009

donorstatusfunnelThe vision of the Nonprofit Starter Pack has always been that it would be a starting place for nonprofits. It’s not a “application” so much as it’s a “platform.” We’ve tried to architect it in a way that gives some immediate benefit, while not precluding the change and extension that I feel is necessary for successful use. In that spirit, I want to show how to add some donor segmentation you might find interesting if you’re using the One-to-one Account model.

The image on the right is from a Dashboard showing all Contacts in an instance with the Nonprofit Starter Pack installed. All Contacts have been categorized by something called “Donor Status.” We’re looking at their giving and putting them in one of four categories–never a donor, a donor previous to last year, a donor last year, or a donor this year. Here’s how I created it.

First, we need to have 2 rollup summary fields on Account that calculate the Opportunity values over this year and last year. You could also add a third rollup to get the value from two years back:

  • Total Giving Year To Date – (Won EQUALS true) AND (Close Date GREATER THAN 12/31/2008) AND (Close Date LESS THAN 1/1/2010)
  • Total Giving Last Year – (Won EQUALS true) AND (Close Date GREATER THAN 12/31/2007) AND (Close Date LESS THAN 1/1/2009)
  • Total Giving 2 Years Previous – (Won EQUALS true) AND (Close Date GREATER THAN 12/31/2006) AND (Close Date LESS THAN 1/1/2008)

These dates need to be hard-coded for the rollup summaries to work. On Jan 1, you’ll need to add a year to all the dates. Don’t worry, all the rollups will then recalculate for you.

Now you want to show these Account fields on the Contact. Create three formulas that just show the Account field value:

  • YTD Giving Total – Account.Total_Giving_Year_To_Date__c
  • Last Year Giving Total – Account.Total_Giving_Last_Year__c
  • 2 Years Previous Giving Total – Account.Total_Giving_2_Years_Previous__c

Once you have these summary values, you can use them in another Formula, our Donor Status formula:

IF( npe01__Lifetime_Giving_History_Amount__c >0, IF( YTD_Giving_Total__c =0, IF( Previous_Year_Giving_Total__c =0, "2. SYBUNT" , "3. LYBUNT") , "4. Current Donor") , "1. Not Donor")

Note that the npe01__Lifetime_Giving_History_Amount__c field is included in the Nonprofit Starter Pack. The others are fields you just created, so make sure the names match.

Now that you’ve got the formula field built, create a Contact and Organizations report that takes all Contacts and groups by Donor Status. Variations on this report can easily be used to send a letter to everyone who gave in the past but not this year. Once you save this report, you can build a funnel dashboard based on it and you’re done! You’ve now segmented your donors based on when they last gave.

My Nonprofit Starter Pack Session from Dreamforce 2009

Saturday, November 28th, 2009

Getting the Most of of the Nonprofit Starter Pack. We cover 3 main things:

  1. Configuring the Starter Pack
  2. Importing Contacts via the wizard
  3. Upgrading a package

Feel free to fast forward through the first 4 minutes, where we’re getting 110 people logged into their orgs…

Inflection Point

Sunday, November 22nd, 2009

Dreamforce is a 4 day software user conference. It’s easy to look at it from afar and see only that–money to be made, customers to retain, sales to be won. And it’s absolutely true that Dreamforce is about all of that. Customers want value. Salesforce.com wants to give it to them. Partners want to get the word out about their solutions. All of that is true.

But I left Dreamforce feeling transformed–personally and professionally. I didn’t expect it. And I’m not exactly sure what to do with it.

I realized last week the reason I took my current job. People ask me all the time. It was clearly a good fit for my skills. The people are great. And it’s a lot of fun. But I realized the real reason last week.

I took this job so that I can help create better lives for millions of people.

Not tens, not hundreds, but millions. Even hundreds of millions. That’s why I’m on this earth. That’s what I think I can contribute to our shared existence. And I think I can do that in my job.

What brought about this clarity? I met some amazing people. I met one woman who has an amazingly clarity of her purpose in life, and that purpose is to help Africa transform itself. To break the colonial, top-down models and return to self-organized, systemic change, completely in partnership with Africans. No more $1M schools dropped from the sky. Holistic, collaborative, community-based change. Generational change. She burns with this goal, and nothing will stand in her way.

Her passion lit a fire in me. And caused me to ask questions of myself. How does my bug fixing contribute to the reemergence of Africa? Am I making the kind of difference I need to make? What can I do differently?

I met a man who has a full-time job and volunteers with a group working in the Sudan. They are building a school from compressed soil, hand dug, hand compressed, and it will be hand built. I had talked to him about his Salesforce needs before I realized he was a volunteer. I thought he was an employee. But it turns out his organization only has one employee. He quietly does this work in his spare time, because he can’t possibly not do it. That wouldn’t make sense to him. And his work is infectious. He asked me, “do you want to go to the Sudan?” Hell yes I want to go to the Sudan.

I met a woman who is taking a holistic approach to education in Tanzania. Children found abandoned in garbage heaps find a home with her organization. They learn and grow because of her work. Their lives are forever changed. She does this work which has nothing to do with her professional history. She was called to it. And is phenomenal.

These people showed me their fire, their passion, the absolute unacceptability of doing nothing. The necessity of action today. The absolute need for impatience and patience all at once. It bowled me over. I’m reeling from it still.

It has caused me to look at my work differently. I will continue to do the best work I possibly can with the responsibilities I have. And I feel compelled to start initiatives of my own. The first thoughts I’ve had is to make Salesforce.com the absolute standard for use in Africa. Here’s what I think needs to be done to show that Salesforce.com can excel for nonprofits in Africa:

Nonprofit Starter Pack

I need to make this code base the best it can be. I want no software bugs standing in the way of this work. It’s the easiest place for me to make an impact on Monday, and I will.

Language Support

Salesforce is translatable into many languages. At this writing, that list includes 20 languages. English, French and Spanish are official languages in many African countries, and those languages are supported currently. I want Salesforce.com to be available in the next tier of African languages: Arabic, Swahili, Portuguese (Brazilian Portuguese is currently available, and maybe that’s close enough?), and perhaps Afrikaans. There are over 2000 languages spoken in Africa, so we may never get to support Berber, Hausa, Xhosa, and the countless others.

Are these the right languages? I’m new to this, but Arabic, Swahili, and Portuguese seem like the place to start. Arabic is a right to left language, so Salesforce.com first must be able to be switched to that mode, but once that’s possible, I want Arabic the next day.

And as soon as Salesforce.com supports a language, we’ll start the work to translate the Nonprofit Starter Pack to that language.

Offline Support

As you know, much of Africa lacks reliable Internet access. Much of Africa lacks clean water, electricity, and other services much more basic than that. So to support Africans working to transform their countries, we need rock-solid offline tools. Salesforce.com has had offline support in Internet Explorer for years, and many African nonprofits are currently using that to successfully support their work.

Salesforce.com recently released Adobe Flashbuilder for Salesforce a toolkit that includes easy to use offline support. I think we need to build some killer offline apps with this toolkit to show the way in this space. Classroom attendance, health clinic service tracking, micro-finance payment collection. We need to build these and share the source code.

Deep Case Studies

I want to work with a few nonprofits in Africa to go deep in their usage of Salesforce.com. I want to hear the stories of how this helps them achieve their goals, and I want to share those stories with the world. Knowing what’s possible is an important step in the path to impact and excellence. We need to prove it, show it, tell it.

I normally don’t share my goals publicly. I like to under promise and over deliver. That’s worked for me for years. But I can’t seem to contain this new fire I feel. So I’m laying out what I want to see happen and I’ll do what I can to bring it about.

If you feel a stirring when you read this, join me. What will we do? I don’t know. How will we do it? We’ll figure it out. But I need to act today. If you feel that need, drop me a line, and we’ll help Africa together.

Using Javascript to validate an Apex form

Monday, November 16th, 2009

Recently I came into a project where an Apex form on a Sites page had to validate itself in Javascript before sending an email via a controller method. I hadn’t ever done this before, so after some digging and some help, I thought I’d share the solution.

First, here is the VisualForce form:

<apex:form id="emailform"  >

<input type="hidden" id="redirect" name="redirect" value="contactus">
Your Email: <apex:inputText value="{!youremail}" id="youremail" maxlength="80" size="40" /><br/>

Subject: <apex:inputText value="{!subject}" id="Subject" maxlength="80" size="40" /><br/>

Message: <apex:inputTextarea value="{!body}" id="Body" rows="6" cols="40"/><br/>
<apex:commandButton value="Send" id="sendButton" onclick="validate3()"/>

	<apex:actionFunction id="emailSendFunction" name="sendEmail" action="{!send}">
		<apex:param name="youremailparam" assignTo="{!youremail}" value=""/>
		<apex:param name="subjectparam" assignTo="{!subject}" value=""/>
		<apex:param name="topicparam" assignTo="{!topic}" value=""/>
		<apex:param name="bodyparam" assignTo="{!body}" value=""/>
	</apex:actionFunction>
</apex:form>

Note that the form doesn’t have an action: we’re invoking code via the onclick action of the command button. When the button is clicked, we’re calling Validate() a Javascript function on the page that looks like this:

function validate() {
     var errorMessage = "Please fix the following errors: \n\n";
     var errorFound = 0; 

     if (!isValidEmail(document.getElementById("about_contact:emailform:youremail").value)) { errorMessage += "Enter a Valid Email Address\n"; errorFound = 1; }
     if (document.getElementById("about_contact:emailform:Subject").value.length == 0) {errorMessage += "Enter a Subject\n"; errorFound = 1; }
     if (document.getElementById("about_contact:emailform:Body").value.length == 0) {errorMessage += "Enter your Message\n"; errorFound = 1; }    

     if (errorFound == 1) {
	alert(errorMessage);
     } else {
	sendEmail();
     }
}

It’s a simple validation scheme. Check a bunch of fields and if they don’t look right, add a message and pop it up to the user. If everything is successful, we call sendEmail().

SendEmail() is the tricky part. It’s actually an Apex controller method exposed as a Javascript function so that it can be called by Javascript. This is done via the ActionFunction tag in Visualforce. You can see it in the VisualForce form above, but I’ll reproduce it here:

	<apex:actionFunction id="emailSendFunction" name="sendEmail" action="{!send}">
		<apex:param name="youremailparam" assignTo="{!youremail}" value=""/>
		<apex:param name="subjectparam" assignTo="{!subject}" value=""/>
		<apex:param name="topicparam" assignTo="{!topic}" value=""/>
		<apex:param name="bodyparam" assignTo="{!body}" value=""/>
	</apex:actionFunction>

The controller’s send method is exposed and 4 parameters are asked for. Each of these parameters are mapped to public variables in the controller, where they are then used by the send method.

public class sffSendEmail {

	public String youremail { get; set; }
	public String subject { get; set; }
	public String topic { get; set; }
	public String body { get; set; }
	public String redirect { get; set; }	// Value tells us where to redirect

	// Constructor
	public sffSendEmail() {
	}

	public PageReference send() {
	 // Define the email
	 Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

	 String[] toAddresses = new String[] { 'destination@email.com' };

	 // Sets the paramaters of the email
	 email.setSubject( subject );
	 email.setToAddresses( toAddresses );
	 email.setPlainTextBody( 'From: ' + youremail + '\n\n\r' + 'Subject: ' + subject + '\n\n\r' + 'Message: ' + body + '\n\n\r' );

	 // Sends the email
	 Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

	 PageReference secondPage = Page.confirmation_page;

	 secondPage.setRedirect(true);
	 return secondPage;

	}
}

If you don’t use the param tags and explicitly map the form fields to the public variables, I found that the send method is invoked before the setters have run on your form, leaving you with null variables. Using the params gets the values into those variables for use.

Update: From the comments, Andrew Waite has a better way of constructing the validate() javaScript:

FYI, it’s a best practice to avoid the dependency on the generated DOM ID, i.e.: document.getElementById(”about_contact:emailform:youremail”. You’ve qualified every element in your path which is good but your code is still susceptible to being broken by a new layer in the hierarchy.

The better approach is to define your validation function to accept the DOM IDs as args and use $Component. For example, assume the validate function took one arg for the “youremail” field: function validate(emailid) {….}, and then your onclick would be something like: onclick=”validate({!$component.youremail});”.

Nonprofit Salesforce Users – you need to get to Dreamforce

Friday, October 30th, 2009

Metcalfe’s Law states that the value of a network is proportional to the square of the number of connected users. Network value isn’t additive–it’s geometric.

Right now we’re at 115% of the number of registrants from last year. Following Metcalfe, and if Dreamforce was tomorrow, that means it would be 128% move valuable this year than it was last year. You can’t argue with math. ;)

But Dreamforce isn’t tomorrow–it starts November 17. If you come, you nonprofit user who hasn’t registered yet, you who has great ideas and insight to share with others, you who has so much to learn, the power of our network increases amazingly quickly.

If we can get to double last year’s attendance, we will acheive 4 times the value of last year’s Dreamforce! It’s true–Metcalfe is right!

This year will be my 4th Dreamforce conference. Every year has been much better than the previous. Every nonprofit organization that has taken the time and effort to use Salesforce.com to help manage their operations should find a way to send at least one staff member to the conference. The information, relationships, and experience will pay off many times the costs of traveling and registration.

If you’re at a Nonprofit using Salesforce.com, or thinking about using it, we currently have a $199 registration rate for you. To get the promotion code, email crmdonation@salesforce.com.

Come be a part of network power. See Metcalfe’s Law up close and personal! Join us in November!

ONE/Northwest is hiring a CRM consultant

Monday, October 26th, 2009

Anyone who asks me has heard the story–ONE/Northwest was an absolutely fabulous job with amazing people. I loved my time there–the technical challenges, the freedom to lead, the opportunity to shape the strategy. I won’t ever stop talking about ONE/Northwest, and the folks I shared that time with will always have a place in my heart.

So good news for you! ONE/Northwest is hiring a CRM consultant. You could deliver cutting-edge Salesforce solutions to groups that will change the world with your work. Check out the job description and see if you’re up for some of what I think is the most fulfilling work around!

Force.com User Group starting in Seattle

Tuesday, September 22nd, 2009

I just heard from Val that the Seattle Force.com Developer User group will kick off on October 1st. If you’re near Seattle and code in Apex and VisualForce, come join the group. I’m really excited about it.

Want to take a deeper dive into the technical capabilities of Salesforce? Would you like to meet other Force.com developers in the Seattle area? Interested in learning about new/different tools that will help you create and develop better solutions? A new Salesforce user group will be starting up on Thursday Oct 1. Meetings will be held on the first Thursday of every month.

We will try to cover a different range of topics at these monthly meetings such as:

  • Summary of new functionality in the upcoming release
  • Project spotlight where someone will demo their project and the technical details
  • Best Practices
  • 3rd Party development and integration tools
  • Apex and VisualForce
  • Open session to discuss specific questions you may have regarding your current projects

Time: 8-9am
Date: Oct 1, 2009
Location:

West Monroe Partners
1215 4th Ave Suite 1010
Seattle, WA 98161

Please contact Val Grasparil (vgrasparil@westmonroepartners.com) or Andrew Brown (abrown@westmonroepartners.com) if you have any questions.

Since it is in the morning, coffee and snacks will be provided. Please RSVP if you are planning on attending so we can make sure to have enough food for everyone.