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>
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Categories: Code Sample, Flex, Salesforce, Visualforce

Tags: , , Leave a comment

Comments Feed16 Comments

  1. Flex Salesforce.com Tree Control « Jeff Douglas - Technology, Coding and Bears… OH MY!

    [...] can run this demo on my Developer Site. You might also want to check out this post on the basics of connecting Flex to [...]

  2. Umut Dogan

    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…

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  3. flex cone

    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 ?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  4. swa

    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 ?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  5. jeffdonthemic

    Make sure your password contains both your password and security token: “YOUR_PASSWORDYOUR_TOKEN”.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  6. Zeeshan

    thanks for the post Jeff. it was a great help :)

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  7. Jeff Douglas

    Awesome! Glad you got something out of it Zeeshan!

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  8. Sanket

    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..

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  9. Jeff Douglas

    Take a look at this code. It has the method to save records to Salesforce.com.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  10. krishna

    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

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  11. Jeff Douglas

    Krishna, did you download the toolkit from http://developer.force.com/flextoolkit?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  12. Sanket

    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?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  13. Jeff Douglas

    Can you post some code with your query and how you are trying to access the records?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  14. Sanket

    I got my problem solved by reading your other blogs. Actually i was not getting intellescence so got stuck.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  15. Sanket

    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

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  16. Sanket

    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.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

Leave a comment

Feed

http://blog.jeffdouglas.com / Developing Salesforce.com Applications with Flex and Visualforce

WordPress Appliance - Powered by TurnKey Linux