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
typemismatchtypemismatch 

Apex and Flex 2 Web Services

I know this is supposed to be possible but I can't find any docs - I saw it at the last dev conf.
 
I have a webService exposed via Apex and I need to access that data from Flex Builder 2 but I can't seem to get it working.
 
If anyone has done this or knows where the docs are please get back to me.
 
thanks
-c
Ron HessRon Hess
the part about exposed via apex is found in here

http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf

WebService Methods

Apex Code class methods can also be exposed as custom ApexWeb Services API calls. To do so, use the
webService keyword.

For example:
class MyWebService {
    webService static Id makeContact(String lastName, Account a) {
        Contact c = new Contact(lastName = 'Weissman', AccountId = a.Id);
        insert c;
        return c.id;
    }
}


then, from flex there are objects that come with Flex Builder, bring up the help contents , search for webservice

Packagemx.rpc.soap.mxml
Classpublic dynamic class WebService
InheritanceWebService Inheritance WebService Inheritance AbstractService Inheritance Proxy Inheritance Object
ImplementsIMXMLObject, IMXMLSupport
The <mx:WebService> tag gives you access to the operations of SOAP-compliant web services. MXML Syntaxcollapsed Show MXML Syntax
expanded Hide MXML Syntax

The <mx:WebService> tag accepts the following tag attributes:

 <mx:WebService
Properties
concurrency="multiple|single|last"
destination="No default."
id="No default."
serviceName="No default."
showBusyCursor="false|true"
makeObjectsBindable="false|true"
useProxy="false|true"
wsdl="No default."
Events
fault="No default."
result="No default."
/>


there is still lots of work to put these two together, but this is the starting doc.
michaelforcemichaelforce

Ron,

I'm just starting to dive into this and have noticed that when creating a WebService instance in MXML it requires an address to the WSDL... which is only accesible after logging in.  Does this mean we would have to login first and then create the instance later with ActionScript?  (obviously I am referring to the scenario of logging in 'apollo style' from the desktop.)

michaelforcemichaelforce
I've decided to create an extremely simple example to post which I think will help anyone out there trying to call an APEX web service from within a flex application.  The catch is... that I need help figuring out why it doesn't work first.... help if you can.
 
First I wrote a sweet package in salesforce.com:
 
Code:
package myPackage{

 webService Integer sweet(Integer x) {
  return x*2;
 }
}
  Then to limit possible problems, I took the required WSDL and put it in the same directory as the application.  I then have the Flex application do the following after it confirms I am logged in successfully:
 
Code:
public var ws:WebService = new WebService();

private function runsweet():void 
{
 ws.wsdl="myPackage.xml";
 ws.loadWSDL();

 ws.sweet.addEventListener(ResultEvent.RESULT,resulthandler);
 ws.sweet.addEventListener(FaultEvent.FAULT,errorhandler);
       
 ws.sweet(2);
}
  There is then a fault event that states: [FaultEvent fault=[RPC Fault faultString="HTTP request error" faultCode="Server.Error.Request" faultDetail="Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032"]. URL: https://na1-api.salesforce.com/services/Soap/package/DrivEnable/myPackage messageId="65A3439A-3145-7B33-C82E-98A285A9BA1E" type="fault" bubbles=false cancelable=true eventPhase=2]
 
This leads me to believe I am not calling my webservice properly.  Any ideas?
 
-Michael
SuperfellSuperfell
Its a pity that flex doesn't give you the real soap response, i'd guess you're getting an INVALID_SESSION Soap fault returned, because your request doesn't contain a sessionId header.
Ron HessRon Hess
my suggestion is don't use the WebService() object in Actionscript, rather use the next level (lower) down as is done in the Flex toolkit when talking to the Soap API.

you can find examples in the src code for the flex toolkit, look at

connection.as and transport.as, xmlwriter.as

we use  mx.rpc.http.HTTPService and not WebService().

just like the Ajax toolkit we roll our own XML from objects ( look for toXml() and XmlWriter() )

in fact i strongly suggest you use Transport().send(), which takes an XmlWriter() object
This class has a (special) flag to get back Faults from the Soap API in a manner compat with Flash, which you will need to see any error messages at all.


the pseudo code is basicaly

_invoke()
   new xmlWriter()
  add namespaces, header stuff
  loop over methods, objects buildind XML
  add end tags
  call new Transport(). send()



see line ~850 of Connection.as
 

jchanowitzjchanowitz

I've just banged my head up against this, and found this thread from a few months back.  I need to use apex transaction support when updating the database from a Flex app, and this seems the best way to do it.  Has anyone successfully called an Apex class web service from Flex? 

From this thread, it looks as if the problem with using the Flex WebService by itself is you don't get a sessionId in the SOAP header.  The Connection object from the toolkit has that cached after Login, so extending Connection to also support the class web service seems like a good way to support this.  I tried that and of course it didn't work.  Two questions: 1) I assume I need to pass another url to 'invoke' other than what I got back from login, should that be the url of the wsdl the class generates?   2) Do I need to include any of the other invoke parameters that aren't being used by the api calls (like nsMap, sfNs, etc.) ?

Any help would be greatly appreciated.

Jay