Developing Flex Applications for Force.com Sites
September 7th, 2009
Developing Flex applications for a Force.com Site is a little different than developing Flex applications that run inside the standard Salesforce.com UI. Since visitors are not required to log in to your Force.com Site there is no concept of an actual user. All visitors simply run as a specific profile under the Guest license.
Since there is no named user for Sites, there is no associated session to pass to your Flex application. Therefore, you need to explicitly code a username and password to log into Salesforce.com in your Flex application. This is a similar concept to authenticating via web services to Salesforce.com.
Here’s a quick example of a Flex application running on my developer Site. You can run this demo on my Developer Site.
Here is the Visualforce page running in my developer Site.
1 2 3 4 5 6 7 8 9 10 | <apex:page> <apex:sectionHeader title="Required Field and Validation Example"/> <apex:pageBlock > <apex:flash src="{!$Resource.DisplayValidation}" width="500" height="300"/> </apex:pageBlock> </apex:page> |
The Flex application specifying the username and password with which the application authenticates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#F3F3EC, #F3F3EC]" creationComplete="login()" layout="vertical" height="300" width="500"> <mx:Script> <![CDATA[ import com.salesforce.*; import com.salesforce.objects.*; import com.salesforce.results.*; import mx.controls.Alert; [Bindable] public var sfdc:Connection = new Connection(); private function login():void { var lr:LoginRequest = new LoginRequest(); sfdc.protocol = "http"; sfdc.serverUrl = "http://na5.salesforce.com/services/Soap/u/14.0"; lr.username = "YOUR_USERNAME"; lr.password = "YOUR_PASSWORD_AND_TOKEN"; lr.callback = new AsyncResponder(loginSuccess, loginFault); sfdc.login(lr); } private function submitForm():void { var aSo:Array = new Array(); var so:SObject = new SObject("Contact"); so.FirstName = firstName.text; so.LastName = lastName.text; so.Email = email.text; aSo.push(so); sfdc.create(aSo, new AsyncResponder( function (obj:Object):void { if (obj[0].success == true) { Alert.show("Created record: "+obj[0].id); } else { Alert.show(obj[0].errors[0].message) } }, sfdcFailure ) ); } private function loginSuccess(result:Object):void { contactForm.enabled = true; } private function sfdcFailure(fault:Object):void { Alert.show(fault.faultstring); } private function loginFault(fault:Object):void { Alert.show("Could not log into SFDC: "+fault.fault.faultString,"Login Error"); } ]]> </mx:Script> <mx:Text text="To create a new Contact, Last Name is required by Salesforce.com while Email is required via a custom validation rule. 

Submit the form with different combinations to view the resulting messages returned from Salesforce.com.
" width="449"/> <mx:Form id="contactForm" width="100%" height="100%" enabled="false"> <mx:FormItem label="First Name"> <mx:TextInput id="firstName"/> </mx:FormItem> <mx:FormItem label="Last Name"> <mx:TextInput id="lastName"/> </mx:FormItem> <mx:FormItem label="Email"> <mx:TextInput id="email"/> </mx:FormItem> <mx:FormItem> <mx:Button label="Submit" click="submitForm()"/> </mx:FormItem> </mx:Form> </mx:Application> |
Categories: Code Sample, Flex, Salesforce, Visualforce











Thanks for the post, this actually has given me an idea. We want to use Salesforce to accept webservice calls from third party vendors but don’t want to give them the api username/pas/token. I think using sites we can create the login function like you have here and then perform what we need to.
Thoughts on that?
I think that sounds like a perfect use case. We did almost exactly the same thing for another project. Good luck!!
Jeff, when I visit the link to run the demo on your developer site, I always get an error message indicating a failure to connect to SF. Any ideas why that might be?
Exact message is “security error accessing url”.
I bet you are using Internet Explorer? Try using Firefox or Chrome.
Thanks, Jeff. You were right – it wasn’t working in IE7, but it worked both in IE8 and Firefox.
Why would this be? Does it have to do with how the swf is being processed in the different browsers, i.e. different sandboxes?
Christian, here’s an in-depth explanation for your reading enjoyment.
HI Jeff,
From your previous example i tried not giving hardcoded userid and password and only use server url and session id but i am not able to log in when i try it in my org. Can you help what wrong i must be doing
Here is the code
private function init():void
{
var lr:LoginRequest = new LoginRequest();
lr.server_url = this.parameters.server_url;
lr.session_id = this.parameters.session_id;
lr.callback = new AsyncResponder(loginHandler,faultHandler);
force.login(lr);
}
I am still facing the same problem. Mentioned above. Does it work only in production org. I am using a development org
Did you append your session token to the end of your password?