Developing Salesforce.com Applications with Flex and Visualforce
November 12th, 2008
I receive a number of inquiries as to the best way to develop Flex apps for multiple Salesforce.com orgs. My methodology might not be the best but it certainly gets the job done by allowing me to easiy develop locally, test on one of our 10+ sandboxes and deploy to production.
Since the code never lies, here is the skeleton that I essentially use for each new project.
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 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="600" height="300" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FFFFFF, #FFFFFF]" creationComplete="init()"> <mx:Script> <![CDATA[ import com.salesforce.*; import com.salesforce.objects.*; import com.salesforce.results.*; [Bindable] private var sfdc:Connection = new Connection(); [Bindable] private var isLoggedIn:Boolean = false; [Bindable]private var userId:String; //********* DETERMINES DEV/PRD *********// private var isDev:Boolean = true; //*************************************// private function init():void { login(); if (isDev) { // dev/local no flashvars passed to movie if (Application.application.parameters.userId == null) { userId = "005600000000000"; // default to my user // sandbox - flashvars passed to movie } else { userId = Application.application.parameters.userId; } // production - flashvars passed to movie } else { userId = Application.application.parameters.userId; } } private function login():void { var lr:LoginRequest = new LoginRequest(); // hard code values for dev/local if (this.parameters.server_url == null) { sfdc.serverUrl = "https://test.salesforce.com/services/Soap/u/13.0"; lr.username = "your_username"; lr.password = "your_password"; } else { // sandbox if (isDev) { lr.server_url = this.parameters.server_url; lr.session_id = this.parameters.session_id; // production } else { lr.server_url = this.parameters.server_url; lr.session_id = this.parameters.session_id; } } lr.callback = new AsyncResponder(loginSuccess, loginFault); sfdc.login(lr); } private function loginSuccess(result:Object):void { isLoggedIn = true; // start calling methods... } private function loginFault(fault:Object):void { mx.controls.Alert.show("Could not log into SFDC: "+fault.fault.faultString,"Login Error"); } ]]> </mx:Script> <mx:TextArea id="txtLog" left="5" right="5" top="5" bottom="5"/> </mx:Application> |
To call this SWF from your Visualforce page, you will need to upload it to your org as a Static Resource. You can then call the SWF in your Visualforce page with the following code. Notice that the userId parameter is being passed via the flashvars and then picked up in the init() method. It’s only here as an example on how you can pass variables to the SWF and is not actually needed for this demo.
The login method is very interesting as it provides the flexibility for running in different orgs. When the SWF runs locally, the Flex Toolkit logs into the endpoint with the specified username and password. When you upload the SWF to your org and run it from the Visualforce page below, the flashvars attribute passes the current user’s session_id and server_url to the SWF allowing it to login and make call back as the authenticated user.
Note: you will want to remove the hardcoded username and password before uplaoding the SWF to your production org as the SWF can be decompiled, thus exposing your credentials.
1 2 3 4 5 6 | <apex:page > <apex:flash src="{!$Resource.Test}" width="600" height="300" flashvars="userId={!$User.Id}&session_id={!$Api.Session_ID}&server_url={!$Api.Partner_Server_URL_130}" /> </apex:page> |
Categories: Code Sample, Flex, Salesforce, Visualforce











[...] can run this demo on my Developer Site. You might also want to check out this post on the basics of connecting Flex to [...]
Hi, I read your comments and integrated a flex application with my account then packaged using unmanaged pck. But it doesn’t load in my test account.
I detailed the problem http://community.salesforce.com/sforce/board/message?board.id=general_development&thread.id=25335 What might be the problem? Thanks…
Hi jeff,
I am using a visualforce page with a swf file (main.swf) embeded in it.
Now I want to use another swf file in the main.swf file using swf loader. I am able to do it local end. But when I am trying to run it through visualforce page, its not showing the content.swf file.
I think the path of content.swf creating problem .
can you show some examples ?
hi jeff,
I am using a visualforce page with a swf file(exactly same code as u written).But i am getting login error when running my vf page.
can u tell me why not able to login ?
Make sure your password contains both your password and security token: “YOUR_PASSWORDYOUR_TOKEN”.
thanks for the post Jeff. it was a great help
Awesome! Glad you got something out of it Zeeshan!
hi jeff,
I have been trying to find out how i create a login and sigup page to interact with salesforce database and store and retrieve values but where i get stuck is how to get click of button in flex and save data in salesforce. Can you help me out with some sample..
Take a look at this code. It has the method to save records to Salesforce.com.
Hi Jeff,
I am pretty new to flex, started using adobe flex builder 3. I am unable to run the code snippets you provided . The compiler throwing error that the definition com.SalesForce is not found. Please advice how to proceed
Krishna, did you download the toolkit from http://developer.force.com/flextoolkit?
hi,
i have a new problem i whatever i query i get it into a arraycollection and which is a object collection. I want to individually access the record of each record but what i get is object and not value. Can you help?
Can you post some code with your query and how you are trying to access the records?
I got my problem solved by reading your other blogs. Actually i was not getting intellescence so got stuck.
HI jeff,
I am trying to set set the value of checkbox as true or false from flex but the object when i try to insert gives me error. without inserting that field everything works fine. Can you help.
I have worked out the code getting values of picklist from object if you want i will post here
I was getting the above error because i was login into salesforce using profile which dnt have upade right on the textbox. Sorri for posting silly questions.