Friday, October 29, 2010

Flex and Charts in Salesforce - Continued

As I was saying, the mashup reference that salesforce published is definitely old.  The login object they refer to and some of the query syntax seemed to be no longer supported in Flex 4. 

In the end, I used the same approach that the author did: hardcode initially to prove that login and query work.  Then, parameterize to get the chart more business-ready.

Here's what I learned.

LoginBySessionId
Logging in by credentials (LoginbyCredentials) was very easy.  The problem is I wanted this application to work from within an existing salesforce session and I didn't want the user to login again.  I found that the session Id could be reused, however, LoginBySessionId was less obvious in implementation.

It took a few tries but after determining that I was getting the session Id, I started getting an error that I was not using the correct URL.  To fix this, I just needed to set the url property to the value from my salesforce session:

                app.serverUrl = parameters.surl;
                app.loginBySessionId(parameters.sid);

So, putting this into a function:

            private function init():void
            {
                _sid = parameters.sid;
                _webWrapper=app.wrapper;

                app.serverUrl = parameters.surl;
                app.loginBySessionId(parameters.sid);
            }
           
init() gets called from the property creationComplete on the Application tag.

Getting past the URL error, I started to get an error stating that my session id was invalid.  A quick google search turned up this helpful community post which indicated that the global API session value that is used in the mashup guide is not a reliable way to get the session id.  To get the correct session id, you have to write a controller extension and fetch the id for the user.  Here's the link to the community thread:

http://forums.sforce.com/t5/Adobe-Flash-Builder-for-Force/Invalid-Session-Id-using-Flex/m-p/156531

Executing a Salesforce Query from Flex
The mashup guide shows the AsyncResponder approach to fetching data from salesforce.  I could not use this method for my use case so I used the LoginResultEvent to invoke my query.  The query comes back as an array so the only way I could see if my query was working was to use Alert.Show to show the contents.  Here is the class that should be imported:
  • import mx.utils.ObjectUtil;
and the syntax to print the array contents:
  • Alert.show(ObjectUtil.toString(result));
where "result" is a variable of type DataArrayCollection.

Where is the SWF file generated?
The Mashup guide complete skips this detail!  The SWF gets generated in your working bin-debug folder.  Just upload your SWF to the static resources in Salesforce to reference it in your VF page.

Debugging
Lots of use of Alert.Show to see what was going on.  I never figured out the debugger but Alert.Show and the ObjectUtil class were a ton of help to see what I was getting and when.

Error 2032???
Many thanks to this blogger for posting this finding.  The error was being thrown I don't think I would have ever found the fix for this error code:

http://blog.prasannadeshpande.com/2010/02/salesforce-flex-error-2032-stream-error-url/

1 comment:

  1. Thanks for giving credits for finding solution on the Error 2032! :)

    ReplyDelete