function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Do I need bridge to connect Flex and Apex?

Hi there,
Im new to salesforce, and my company has asked me to develope a salesforce application that anyone can search throught their online publication without loging in to salesforce. that means I need to create a web application that connects to salesforce database.
I've been reading about this for 3 days now. I see flex can be used to connect to apex without any problem, however in all the examples I've seen, flex is in a s-control. My application needs to be stand alone.
Just to make my mind clear and get me going: Can I connect to Apex from flex without anything like Java to establish connection?
Thank you
The existence of the Scontrol does not imply the presence of Java or any other mediator.  Using the Flex toolkit you can connect directly to the Salesforce web services API, even from outside a browser context (e.g. Adobe AIR).  The Scontrol just happens to be a convenient container in which to show components in the Salesforce web experience.
Thank you very much. I thought maybe scontrol is kinda required to run flex applications. It seems much easier now, coz I have previously done something similar with flex and j2ee. gatta start learning apex now...

Arun BArun B
Can you pls give me a hint on how to do this ? I have a similar requirement and I tried connecting to SalesForce Objects doing some action script coding like :

import com.salesforce.objects.LoginRequest;
            var lrLogin:LoginRequest = new LoginRequest(
                    callback:new AsyncResponder(_result, _fault)               


I call this Action Script from a MXML  .. but what should be the ServerURL and Session id that I need to pass ? pls help or let me know if you have some samples ??

Arun B

Hi there
First download Salesforce Flex toolkit

there you will find a sample applicaiton which will answer all you questions
Arun BArun B
Hi Maudi,
Thanks for your prompt response. I did download the flex tool kit and tried to run the samples in that. But I get the following error message when the app is tring to connect to/retrieve data from Sales Force.

urrentTarget = (
        bytesLoaded = 0
        bytesTotal = 0
        data = (null)
        dataFormat = "text"
      eventPhase = 2
      target = (
      text = "Error #2032: Stream Error. URL:"
      type = "ioError"
  headers = (null)
  message = (mx.messaging.messages::ErrorMessage)#4
    body = (Object)#5
    clientId = "DirectHTTPChannel0"
    correlationId = "000961F9-2668-F82C-5D73-7E8E05FAA7DC"
    destination = ""
    extendedData = (null)
    faultCode = "Server.Error.Request"
    faultDetail = "Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL:"]. URL:"
    faultString = "HTTP request error"
    headers = (Object)#6
    messageId = "A336B450-FD66-0556-79E4-7E8E0E1DDA74"
    rootCause = (
    timestamp = 0
    timeToLive = 0
  messageId = "A336B450-FD66-0556-79E4-7E8E0E1DDA74"
  target = (null)
  token = (mx.rpc::AsyncToken)#7
    message = (mx.messaging.messages::HTTPRequestMessage)#8
      body = "<se:Envelope xmlns:se=""><se:Header xmlns:sfns=""/><se:Body><login xmlns="" xmlns:ns1=""><username>theusername</username><password>pwd</password></login></se:Body></se:Envelope>"

I am very sure that I am doing some basic mistake here .. but not sure where. Can you pls help if you know the root cause of the problem ? My actual MXML code is as below (the same code runs fine if I defined the SWF as an s-control and run it from; but what I am trying now is to run this as a standalone flex app).

<?xml version="1.0" encoding="utf-8"?>
<mx:Application creationComplete="login(event)" xmlns:mx="" layout="absolute" xmlns:salesforce="">
 <salesforce:Connection id="apex" serverUrl="" />
   import mx.collections.ArrayCollection;
   import com.salesforce.results.QueryResult;
   import mx.utils.ObjectUtil;
   import mx.controls.Alert;
   import com.salesforce.AsyncResponder;
   import com.salesforce.objects.LoginRequest;
   private function login(event:Event):void {
    //var lr:LoginRequest = new LoginRequest();
    //lr.username = "";
    //lr.password = "haap04";
    //lr.callback = new AsyncResponder(loadData, handleFault);
    var lr:LoginRequest = new LoginRequest(    {
            username : <useid>,             // put your own info here to test standalone
            password : <pwd>,            // put your own info here to test standalone   
            callback : new AsyncResponder(loadData, handleFault)
            } );
    //    Util.debug(this, 'apex.login ( ' +ObjectUtil.toString(lr) +'\n);' );   
   // apex.login((lr.username, lr.password, lr.callback);

   private var tmp:ArrayCollection = new ArrayCollection();
   private var activityHistory:ArrayCollection = new ArrayCollection();
   private function handleFault(fault:Object):void {;
   private function loadData(lr:Object):void {
    apex.query("Select Id from Account where name like 'new account%' limit 1'", new AsyncResponder(
     function(qr:QueryResult):void {
      if (qr.size > 0) {
       tmp = qr.records;
       activityHistory = tmp.getItemAt(0).ActivityHistories.records;
 <mx:DataGrid dataProvider="{activityHistory}" width="587" x="10" height="386" y="10">
   <mx:DataGridColumn dataField="Id"/>

I would truly appreciate your help here.
Thats because of your security setting
As I learned, you have 2 ways to access the salesforce.
Either from your localhost, or running the standalone swf file which I think this is your case
In case you put your file in localhost you need to change the access setting after the connection is initiated. you have to change your connection access to http by adding protocol property
<salesforce:Connection id="apex" sendRequest="sendRequestListener(event)" protocol="http"/>

however, if you just press "Run" button on your flex builder, you have to do the following
go to to find out your own ip address, copy it...
log into your salesforce and go to setup>Administration Setup> Network access
in trusted IP ranges click  "new" and enter a range that contains your ip
save and try again, it should work

Arun BArun B
Thanks very much for the response Mavdi. I tried this and it worked. Earlier I also forgot to add the security token to my password. Thanks again for all your help !!

Arun B