In this programming exercise, we are making a query to retrieve Salesforce’s Contact object (or Salesforce likes to call it sObject with type=Contact).
Making a Salesforce Web Service Query
The Query call is a Soap call named “query” with a parameter named “queryString”. The queryString is very much in SQL syntax. Herewith we are retrieving the Id, Name, Email and Birth. Refer to previous blog for creating SessionHeader.
String queryString="select Id, Name, Email, Birthdate from Contact";
SoapObject binding = new SoapObject(NAMESPACE, "query");
binding.addProperty("queryString", queryString);
SoapSerializationEnvelope envelope2=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope2.setOutputSoapObject(binding);
Element[] headerOut = new Element[1];
Element sessionHeader= // Create a SessionHeader Element with sessionId
headerOut[0]=sessionHeader;
envelope2.headerOut=headerOut;
String SOAP_ACTION="\"\"";
transport.call(SOAP_ACTION, envelope2);
SoapObject result=(SoapObject)envelope2.getResponse();
Query’s response
Upon successful query, a query responses with
1. An Element named “done” – true if all result records are returned.
2. An Element named “size” – an integer indicating number of returned records
3. A Sequence of Element named “records” – each element is a returned sObject
4. An Element named “queryLocator” – haven’t figure out what it’s and not care for now.
The following snippet shows how to retrieve the query result …
SoapObject result=(SoapObject)envelope2.getResponse();
int size=Integer.parseInt(result.getProperty("size").toString());
Boolean done= Boolean.parseBoolean( result.getProperty("done").toString());
Log.v(TAG, "query...done="+done);
Log.v(TAG, "query...size="+size);
PropertyInfo pInfo=new PropertyInfo();
int pCount=result.getPropertyCount();
for(int i=0; i< pCount; i++){
result.getPropertyInfo(i,pInfo);
Log.v(TAG,"pInfo="+pInfo);
if("records".equalsIgnoreCase(pInfo.getName())){
SoapObject sObject=(SoapObject)result.getProperty(i);
String id=sObject.getProperty("Id").toString();
String name=sObject.getProperty("Name").toString();
Log.v(TAG, "id, name="+id+","+name);
}
}
Tuesday, September 17, 2013
How to Android Salesforce - Making Salesforce Query
Home »
How to Android Salesforce - Making Salesforce Query