Now that Adobe Flash Builder for Force.com has been released we are starting to see some of the fruits from this joint venture. Salesforce.com released a beta version of the Force.com Explorer on Adobe Air a couple of days ago and I caught an interesting tweet from Jeff Grosse about a new video on the Salesforce Channel. James Ward developed a Flash app with the tool that pulls down accounts and contacts from Salesforce.com and allows you to update records. This might be the preferred method of development for Android devices since:
Android now runs Flash 10.1
Android does not support Axis Web services natively (i.e., no Partner API love)
There is no secure REST API for Force.com
So now you can write Android apps with Adobe Flash Builder for Force.com that uses the same Web services API that you are familiar with.
The video is pretty slick and the source code is available if you would like to start hacking up your own implementation.
At Appirio we routinely hold customer-exclusive webinars to talk about features, trends and the latest technologies in cloud computing. This week I’m hosting a webinar to some of our existing Salesforce.com customers on the new features released in Summer ’10.
This is how the marketing team “effectively” markets the event.
BTW since someone asked… jeffdonthemic is in homage to “Mike D” from the Beastie Boys. I picked it up in the late 90′s and it’s an easy login that’s always available. I tend to fight with the other Jeff Douglas for logins.
The Saleforce UI displays 15 digit, case-sensitive IDs for records. However, for Windows machines you will need to use a case-safe version of the ID for data migration, API calls and such. It’s not hard to generate these 18 digits IDs but it does take some time. I’ve been working on a lot of Google Gadgets and Chrome extensions lately so I thought I would throw together a Chrome extension (sorry Firefox users!) to quickly generated an 18 digit Salesforce.com ID. Thanks to Ron Hess for the conversion script!
Building Chrome extensions is fairly simple with a little bit of HTML and Javascript. The Google Code site has some sample “Hello World” code as well as an extension video to get you going. I’ve already started thinking about my next extension with a bunch of Force.com utilities that should make development life easier. I’ll be glad to share.
A copy of the new Force.com Developer News arrived in my inbox this morning with some interesting news. An Adobe Air (beta) of the Force.com Explorer has been released for people to begin playing with. If you missed the newsletter, you can catch Jon Mountjoy's blog post with the announcement. Jon writes more eloquently than me due to his enchanting accent. You should give the app a whirl as it’s off to a good start. You can download it here.
I was quite excited to see how the Adobe Flash Builder for Force.com was coming along since I developed a small app with the toolkit last January. I took a peek at the docs and they’ve made some really nice progress. However, the main reason I immediately downloaded the Force.com Explorer was to test out the functionality in hopes of a better tool. If you are a Mac user than you are blessed with the options of running Simon Fell’s SoqlXplorer in addition to the Force.com IDE for Eclipse. Neither one is quite the perfect tool. SoqlXplorer doesn’t let you drill into objects to build complex relationship queries like Eclipse but the Eclipse IDE doesn’t allow you to copy the resulting data from queries into your clipboard. I typically live in the SoqlXplorer all day. (Side note: not being able to copy results in the Eclipse IDE is frustrating but Job Plax, the Force.com IDE PM, told me that this feature just missed the last release so hopefully it will be in a future release.)
The Force.com Explorer is a breeze to install if you already have Adobe Air and you can be up and running within seconds. It has a nice clean UI which looks a lot like the .NET Apex Explorer. It has most of the functionality that you find in the other SOQL query tool plus some:
Filter objects by name
Drill down into objects and relationships to create complex queries. Very similar in functionality to the Eclipse IDE
Return results with child object that you can click on to drill into
Copy the text from cells in the query results
View metadata about the objects
Query, query all and batch size options
Right click on an object to view it of its metadata and save it as a PDF
It remembers the fields you selected in previous objects to easily rebuild SOQL queries
My article on developer.force.com, Using OAuth to Authorize External Applications, shows how to develop external Java applications that use OAuth to access your Saleforce.com data. But what if you want to do the opposite? What if you need to write an application on the Force.com platform that uses your data from Google, Twitter, LinkedIn or TripIt using OAuth? Luckily Jesper Jorgenson at Salesforce.com posted an open source project called sfdc-oauth-playground which is a generic consumer implementation of OAuth as a Force.com App.
The main purpose of this beta project is to show you how to write OAuth signed requests in Apex. There is a managed packaged you can install into your org but if you really want to dig into the guts of OAuth (and who doesn’t??) you’ll need to download and install the source code from the project. The managed package doesn’t afford you to opportunity to modify code or view granular debug statements.
Jesper doesn’t provide much documentation for the project so I’ve put together a short video showing how to get started using Google Accounts and Blogger. Unfortunately different providers implement OAuth to their own liking so you sometimes have to make modifications to the requests being sent over the wire. I ran into a number of issues so hopefully this will assist you in getting up and running with OAuth.
In case you don’t have time for the video, here are a couple of screenshots outlining the process using the OAuth Consumer Playground application from the managed package.
Create a new OAuth Service (e.g., Blogger) with various URLs for accessing services
My article on OAuth went up yesterday on developer.force.com (thanks Jon!). It seems like OAuth is gaining adoption so I hope the article comes in handy for people developing applications for Salesforce.com. I’m currently working on another article for the Force.com Web Services Connector (WSC).
“Force.com lets you build external web applications that access data on the Force.com platform using the Oauth 1.0a protocol. OAuth is an open protocol that allows a website to access resources of another website without having to expose a user’s credentials. Instead of supplying a username and password, OAuth allows users to hand out security tokens to specific sites for access to specific resources for a defined duration.
In this article we’ll explain what OAuth is and why you should use it. We’ll also configure a Remote Access Application in Force.com, and develop an application on Google App Engine that uses a Remote Access Application to authorize access to Force.com to display account and contact records. Finally, we’ll look at some tips, tricks, available libraries and best practices to get you up and running in no time.”
After my last post, Create and Email a PDF with Salesforce.com, I received a few comments whether it was possible to do the same with Document stored in Salesforce.com. Could you choose a Document and then send it via email as an attachment? The short answer is yes! However, I tried to do it from a Force.com Sites page but was not able to get it to work. The document is not marked for internal use and it is marked as an externally available image. I also made sure that the public settings for my Site included read access to documents but still the query for the document returns no results. No time to look at it in depth right now so if anyone has an idea, please send it my way.
public with sharing class DocumentEmailController {public ID documentId {get;set;}publicString email {get;set;}public List<SelectOption> documents {
get {if(documents ==null){
documents =new List<SelectOption>();
documents.add(new SelectOption('01570000001NZDn','Cup of Coffee? - DOC'));
documents.add(new SelectOption('01570000001NZDi','Workflow Cheatsheet - PDF'));}return documents;}
set;}public PageReference sendDoc(){Document doc =[select id, name, body, contenttype, developername, type
from Document where id =:documentId];
Messaging.EmailFileAttachment attach =new Messaging.EmailFileAttachment();
attach.setContentType(doc.contentType);
attach.setFileName(doc.developerName+'.'+doc.type);
attach.setInline(false);
attach.Body= doc.Body;
Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(newString[]{ email });
mail.setSubject('Document Email Demo');
mail.setHtmlBody('Here is the email you requested: '+doc.name);
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{ attach });// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Document sent to '+email));returnnull;}}
This is a continuation of my post a couple of days ago, Attach a PDF to a Record in Salesforce, and shows how to dynamically generate a PDF and attach it to an email. The code is fairly similar and has the same issue with testing the PageReference getContent() method.
The Visualforce page simply allows the user to enter their email address and select a sample Account from the picklist. This is Account that is passed to the PdfGeneratorTemplate Visualforce page to generate the PDF.
<apex:page controller="PdfEmailController"><apex:sectionHeader title="PDF Example" subtitle="Email a PDF"
description="Example of how to email a dynamically generated PDF."/><apex:form ><apex:pageMessages /><apex:pageBlock title="PDF Input"><apex:pageBlockButtons ><apex:commandButton action="{!sendPdf}" value="Send PDF"/></apex:pageBlockButtons><apex:pageBlockSection ><apex:pageBlockSectionItem ><apex:outputLabel value="Email to send to"for="email"/><apex:inputText value="{!email}" id="email"/></apex:pageBlockSectionItem><apex:pageBlockSectionItem ><apex:outputLabel value="Account"for="account"/><apex:selectList value="{!accountId}" id="account" size="1"><apex:selectOptions value="{!accounts}"/></apex:selectList></apex:pageBlockSectionItem></apex:pageBlockSection></apex:pageBlock></apex:form></apex:page>
PdfEmailerController
The Controller passes the Account ID that the user entered as a parameter for the Visualforce page being generated. It let creates the attachment from the PDF content. The Body of the attachment uses the Blob returned from the PageReference’s getContent method. You could also use the getContentAsPDF method which always returns the page as a PDF, regardless of the component’s renderAs attribute. However, this method always seems to throw an error in the test class. See the PageReference documentation for more info. The method then constructs the SimpleEmailMessage object and then sends it on its way to the recipient’s inbox.
public with sharing class PdfEmailController {public ID accountId {get;set;}publicString email {get;set;}public List<SelectOption> accounts {
get {if(accounts ==null){
accounts =new List<SelectOption>();
accounts.add(new SelectOption('0017000000LgRMb','United Oil & Gas Corp.'));
accounts.add(new SelectOption('0017000000LgRMV','Burlington Textiles Corp of America'));}return accounts;}
set;}public PageReference sendPdf(){
PageReference pdf = Page.PdfGeneratorTemplate;// add parent id to the parameters for standardcontroller
pdf.getParameters().put('id',accountId);// the contents of the attachment from the pdfBlob body;try{// returns the output of the page as a PDF
body = pdf.getContent();// need to pass unit test -- current bug }catch(VisualforceException e){
body =Blob.valueOf('Some Text');}
Messaging.EmailFileAttachment attach =new Messaging.EmailFileAttachment();
attach.setContentType('application/pdf');
attach.setFileName('testPdf.pdf');
attach.setInline(false);
attach.Body= body;
Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(newString[]{ email });
mail.setSubject('PDF Email Demo');
mail.setHtmlBody('Here is the email you requested! Check the attachment!');
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{ attach });// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ mail });
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+email));returnnull;}}
PdfGeneratorTemplate Visualforce Page
This is the Visualforce page that is generated in the Controller. It simply uses the StandardController and displays the Account name for the ID passed to it.
1
2
3
4
<apex:page standardController="Account" renderAs="pdf"><h1>Congratulations!!</h1><p>You created a PDF for{!account.name}</p></apex:page>
Salesforce.com makes it extremely easy to generate PDF documents on the fly by simply using the renderAs=”pdf” attribute for the <apex:page> component. It’s also a snap to attach these PDFs to records as Attachments. Below is a small Visualforce page and Controller that generates a PDF and saves it to an Account.
Note: there is a small issue when it comes to testing the Controller. Salesforce currently throws an error (Salesforce.com Error “Unable to retrieve object”) when getContent or getContentAsPDF is called from a test method. There’s an Idea to make this work properly. I encourage everyone to vote for this.
PdfGenerator Visualforce Page
The Visualforce page allows the users to enter the ID of the Account to attach the PDF to as well as the name of the PDF. You could just have easily used a Controller Extension instead of entering the ID for the Account but I went this route for simplicity.
<apex:page controller="PdfGeneratorController"><apex:sectionHeader title="PDF Example" subtitle="Attach a PDF"
description="Example of how to attach a PDF to a record."/><apex:form ><apex:pageBlock title="PDF Input"><apex:pageBlockButtons ><apex:commandButton action="{!savePdf}" value="Save PDF"/></apex:pageBlockButtons><apex:pageMessages /><apex:pageBlockSection ><apex:pageBlockSectionItem ><apex:outputLabel value="File Name"for="pdfName"/><apex:inputText value="{!pdfName}" id="pdfName"/></apex:pageBlockSectionItem><apex:pageBlockSectionItem ><apex:outputLabel value="Account ID"for="id"/><apex:inputText value="{!parentId}" id="id"/></apex:pageBlockSectionItem></apex:pageBlockSection></apex:pageBlock></apex:form></apex:page>
PdfGeneratorController Custom Controller
The Controller passes the Account ID that the user entered as a parameter for the Visualforce page being generated. It then creates a new Attachment object and sets some attributes. It sets the ParentId to the value of the Account ID that the user entered so that the PDF is attached to that record. The Body of the attachment uses the Blob returned from the PageReference’s getContent method. You could also use the getContentAsPDF method which always returns the page as a PDF, regardless of the <apex:page> component’s renderAs attribute. However, this method always seems to throw an error in the test class. See the PageReference documentation for more info. The method then redirects the user to the Account page so they can view the PDF attachment.
public with sharing class PdfGeneratorController {public ID parentId {get;set;}publicString pdfName {get;set;}public PageReference savePdf(){
PageReference pdf = Page.PdfGeneratorTemplate;// add parent id to the parameters for standardcontroller
pdf.getParameters().put('id',parentId);// create the new attachment
Attachment attach =new Attachment();// the contents of the attachment from the pdfBlob body;try{// returns the output of the page as a PDF
body = pdf.getContent();// need to pass unit test -- current bug }catch(VisualforceException e){
body =Blob.valueOf('Some Text');}
attach.Body= body;// add the user entered name
attach.Name= pdfName;
attach.IsPrivate=false;// attach the pdf to the account
attach.ParentId= parentId;
insert attach;// send the user to the account to view resultsreturnnew PageReference('/'+parentId);}}
PdfGeneratorTemplate Visualforce Page
This is the Visualforce page that is generated in the Controller. It simply uses the StandardController and displays the Account name for the ID passed to it.
1
2
3
4
<apex:page standardController="Account" renderAs="pdf"><h1>Congratulations!!</h1><p>You created a PDF for{!account.name}</p></apex:page>
@isTest
privateclass Test_PdfGeneratorController {static Account account;static{
account =new Account();
account.Name='Test Account';
insert account;}static testMethod void testPdfGenerator(){
PageReference pref = Page.PdfGenerator;
pref.getParameters().put('id',account.id);
Test.setCurrentPage(pref);
PdfGeneratorController con =new PdfGeneratorController();
Test.startTest();// populate the field with values
con.parentId= account.id;
con.pdfName='My Test PDF';// submit the record
pref = con.savePdf();// assert that they were sent to the correct pageSystem.assertEquals(pref.getUrl(),'/'+account.id);// assert that an attachment exists for the recordSystem.assertEquals(1,[select count() from attachment where parentId =:account.id]);
Test.stopTest();}}
I brushed this code off and thought it might be useful to someone as a starting point for a dynamic search page. It has some cool functionality including passing search criteria via Javascript to the controller, search as you type, sorting of results by clicking on the column header plus much more. Hope you find it useful.
Some of the interesting features of the Visualforce page and Apex controller include:
Instead of the typical overhead of using getters/setters in the controller to maintain the variables for the search form, I used Dave Carroll’s blog as an example to pass the variables via Javascript.
Since I’m using Javascript to pass my parameters, I can get fancy and make the search form more dynamic by running the search as the user types or selects an option in the picklist. No submit button is required.
Users can click on the column headers to toggle the direction of the search results by using a CommandLink component and passing the column name.
Build the picklist using Dynamic Apex’s Describe functionality so that it’s maintenance free.
Perform the search against multiple fields including multi-select picklists using dynamic SOQL and preventing SOQL injection attacks.
ContactSearchController
Take a look at the Apex code below. The search interaction is split up between two separate but integral parts. When the search is fired from the Visualforce page via Javascript, the SOQL is constructed in the runSearch() method and is then passed to the runQuery() method to execute. The SOQL string is persisted so that when the user clicks on the table column the same SOQL can be issued again in the toggleSort() method but ordered by a different field name and sort direction.
public with sharing class ContactSearchController {// the soql without the order and limitprivateString soql {get;set;}// the collection of contacts to displaypublic List<Contact> contacts {get;set;}// the current sort direction. defaults to ascpublicString sortDir {
get {if(sortDir ==null){ sortDir ='asc';}return sortDir;}
set;}// the current field to sort by. defaults to last namepublicString sortField {
get {if(sortField ==null){sortField ='lastName';}return sortField;}
set;}// format the soql for display on the visualforce pagepublicString debugSoql {
get {return soql +' order by '+ sortField +' '+ sortDir +' limit 20';}
set;}// init the controller and display some sample data when the page loadspublic ContactSearchController(){
soql ='select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null';
runQuery();}// toggles the sorting of query from asc<-->descpublicvoid toggleSort(){// simply toggle the direction
sortDir = sortDir.equals('asc')?'desc':'asc';// run the query again
runQuery();}// runs the actual querypublicvoid runQuery(){try{
contacts = Database.query(soql +' order by '+ sortField +' '+ sortDir +' limit 20');}catch(Exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));}}// runs the search with parameters passed via Javascriptpublic PageReference runSearch(){String firstName = Apexpages.currentPage().getParameters().get('firstname');String lastName = Apexpages.currentPage().getParameters().get('lastname');String accountName = Apexpages.currentPage().getParameters().get('accountName');String technology = Apexpages.currentPage().getParameters().get('technology');
soql ='select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null';if(!firstName.equals(''))
soql +=' and firstname LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';if(!lastName.equals(''))
soql +=' and lastname LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';if(!accountName.equals(''))
soql +=' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';if(!technology.equals(''))
soql +=' and interested_technologies__c includes (\''+technology+'\')';// run the query again
runQuery();returnnull;}// use apex describe to build the picklist valuespublic List<String> technologies {
get {if(technologies ==null){
technologies =new List<String>();
Schema.DescribeFieldResult field = Contact.interested_technologies__c.getDescribe();for(Schema.PicklistEntry f : field.getPicklistValues())
technologies.add(f.getLabel());}return technologies;}
set;}}
CustomerSearch Visualforce Page
The Visualforce page has three sections: 1) a search form, 2) a results blockTable and 3) a debug panel displaying the SOQL that was executed.
The search form has a number of fields that fire the Javascript search using the onkeyup and onchange events. Instead of each form field passing the current values to actionFunction’s Javascript method, for ease of use, each form field calls the doSearch() function that gathers up the values and submits them. When the actionFunction renders it creates a Javascript function that POSTs the values to the controller in the same manner as CommandLink or CommandButton.
The search results BlockTable is rerendered when the search is submitted to the controller so that the results display properly. Users can click on the column headers to reorder the results of the query. This uses a CommandLink to pass the sort field to the controller and toggle the sort direction.