Friday 15. of February 2008
Tags:apex, apex execute, flex, salesforce, salesforce methods     By:  Posted in Uncategorized

Recently we have completed a project which meshes Flex together with Salesforce apex method calls.

When beginning this project, we found it hard to find a good tutorial on how to actually decipher these methods and apply them to our application, so i hope this is useful to anyone looking for the same.

So when you first see the method, it may be a bit confusing (i know i was), but once you learn how to read it, it is really easy, and logical. It may look something like this:

webService static void updateSomething (Id anId, String theString)

Well…looks like a normal function call…but how do i actually USE this! You cannot just place this call into your flex code, it will not work. This has to be place within an Apex method call called “execute

First, we must import some items to get this kicking, I am also assuming that you have made a connection to salesforce with username and password:

import com.salesforce.AsyncResponder; –> This will allow us to call function or execute simple lines of code immediately after the result is returned

import com.salesforce.Connection; –> This will allow us to keep the connection alive on separate .mxml pages, so we do not keep calling a login function

import com.salesforce.objects.Parameter; –> This will allow us to set parameters to send the apex execute function

import mx.core.Application; –> This is imported because we’re running this on a different .mxml page than the page where our Login function resides

import com.salesforce.results.QueryResult; –> This allows us to catch the results sent back by the”execute” function

OK, we’ve imported what we need to get this to work!

Once again, our method looks like this:

webServices static void updateSomething (Id anId, String theString)

an apex execute statement of this, will look something like this:

apex.execute(”WebServices“, “updateSomething“, params,
new AsyncResponder(
function (qr:Object):void
{
Alert.show(”It’s Updated!”);
},
function (fault:Object):void {
Alert.show(”Failed: “+ObjectUtil.toString(fault));
}
));

If you look at the bod items, you can see where these two items relate, but where are the parameters? We add these BEFORE the execute call:

var firstParam:Parameter = new Parameter (”id”, “1232323″);
var secondParam:Parameter = new Parameter (”string”, “this is my string to send”);
var params:Array = [firstParam, secondParam];

We set our two variables, and pass them in a variable called ‘params, the third item in the apex ‘execute’ call

This is where our AsyncResponder comes in. In this example, we are returning nothing, as you can see in the method:

webService static void updateSomething (Id anId, String theString)

So, if it updates, we may just want to alert something: Alert.show(”It’s Updated!”);

Or, if it fails, lets Alert the reason: Alert.show(”Failed: “+ObjectUtil.toString(fault));

And lets not forget to ADD THE CONNETION before any of this, because our login is not on the same page!!!!!

var apex:Connection = Application.application.apex; !!This will very base on what page your login is on, and where you’re calling this!!

Here is the full code:

var apex:Connection = Application.application.apex;

var firstParam:Parameter = new Parameter (”id”, “1232323″);
var secondParam:Parameter = new Parameter (”string”, “this is my string to send”);
var params:Array = [firstParam, secondParam];

apex.execute(”WebServices”, “updateSomething”, params,
new AsyncResponder(
function (qr:Object):void
{
Alert.show(”It’s Updated!”);
},
function (fault:Object):void {
Alert.show(”Failed: “+ObjectUtil.toString(fault));
}
));

Hopefully this helps some people out!

Happy Coding.

You can leave a trackback from your own site.
Comments

Displaying results 1 to 1 out of 1
 
   Richappsconsulting Monday, 25-10-10 14:02
[...] something about richappsconsulting[...]
Sorry, comments are closed for this post.