Thursday, November 22, 2012

How to Android Salesforce – Webservice call to Salesforce

Salesforce has excellent documentation on using Apache Axis making Soap call. However, making Soap call in Android is totally different story as Android runtime doesn’t include the essential RMI packages. Having Android communicating with Salesforce in webservice requires programming from pretty ground up.

What you need ?

a) KSOAP2
Instead of directly messing with XML, and HTTP call, KSOAP2 is a light weight Soap communication framework that handles the XML and HTTP tedious work.
KSOAP2 can be downloaded from …

http://code.google.com/p/ksoap2-android/downloads/detail?name=ksoap2-android-assembly-2.3-jar-with-dependencies.jar&can=2&q=


b) Download WSDL from Salesforce.com
Salesforce has different WSDL based on your intent;
Enterprise WSDL provides schema specific to your setup and provide direct access to your customized objects. However, it only works with your organization.
Partner WSDL provides a generic and loosely typed schema that work for any organization.

To download …
Login to Salesforce.com
Click Setup -> Develop -> API

i) Find the Soap namespace
From the downloaded WSDL, find the targetNameSpace.
e.g. targetNamespace="urn:sobject.partner.soap.sforce.com"

ii) Find the Soap service end point
At the very bottom of the downloaded WSDL, it has an xml element indicating your server url which is the initial Soap end point.
e.g. soap:address location ="https://www.salesforce.com/services/Soap/u/18.0"

c) Salesforce login Security Token
You need a security token for Soap login to Salesforce.
To get/reset a Security Token
Login to Salesforce.com
Click Setup -> My Personal Information -> Reset My Security Token

Login to Salesforce with Soap

With reference to the code snippet below…

i) Login user password
When authenticating with Soap, the user password is the concatenation of your password and the security token. For instance, if your password=”password” and security token=”XXXXXXX”, the USER_PW = “passwordXXXXXXX”

ii) Soap end point
The initial URL (i.e. URL variable below) is set to
URL=
"https://www.salesforce.com/services/Soap/u/18.0"

iii) SOAP_ACTION="Soap"

Source code ...

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.serialization.SoapSerializationEnvelope;


SoapObject binding = new SoapObject(NAMESPACE, "login");
binding.addProperty("username", USER_ID);
binding.addProperty("password", USER_PW);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(binding);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
SoapObject result=null;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
result = (SoapObject)envelope.getResponse();
// At this point, Soap login success.
Log.v(TAG, result.toString());
Object serverUrl=result.getProperty("serverUrl");
Log.v(TAG,"serverUrl="+serverUrl);
Object passwordExpired=result.getProperty("passwordExpired");
Log.v(TAG,"passwordExpired="+passwordExpired);
Log.v(TAG, "Ending Login with Success.");

} catch (SoapFault aSoapFault) {
Log.d(TAG, "SoapFault actor="+aSoapFault.faultactor);
Log.d(TAG, "SoapFault code="+aSoapFault.faultcode);
Log.d(TAG, "SoapFault message="+aSoapFault.faultstring);
// do something – Soap Fault occurred
}