Sample JAVA program for Web Service API

Sample JAVA program for Web Service API

Sample Code:


package wsc;

import java.io.BufferedReader;


import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.IOException;

//import com.sforce.soap.enterprise.DeleteResult;


import com.sforce.soap.enterprise.DescribeGlobalResult;
import com.sforce.soap.enterprise.DescribeGlobalSObjectResult;
import com.sforce.soap.enterprise.DescribeSObjectResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
//import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.Field;
//import com.sforce.soap.enterprise.FieldType;
import com.sforce.soap.enterprise.GetUserInfoResult;
//import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.PicklistEntry;
import com.sforce.soap.enterprise.QueryResult;
//import com.sforce.soap.enterprise.SaveResult;
//import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectorConfig;
import com.sforce.ws.ConnectionException;

public class QuickstartApiSample


{
private static BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
EnterpriseConnection con;
String authEndPoint = “”;
String passwd = “”;
public static void main(String[] args)
{   
QuickstartApiSample sample = new QuickstartApiSample(“https://login.salesforce.com/services/Soap/c/24.0/0DF90000000PX8r”);
if ( sample.login() )
{
sample.describeGlobalSample();
sample.describeSample();
sample.querySample();
}
}
public QuickstartApiSample(String authEndPoint)
{
this.authEndPoint = authEndPoint;
}
public String getUserInput(String prompt)
{
String result = “”;
try
{
System.out.print(prompt);
result = reader.readLine();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
return result;
}
public boolean login()
{
boolean success = false;
String userId = getUserInput(“UserID: “);
String passwd = getUserInput(“Password: “);
try
{
ConnectorConfig config = new ConnectorConfig();
config.setAuthEndpoint(authEndPoint);
config.setUsername(userId);
config.setPassword(passwd);
config.setCompression(true);
//config.setProxy(“Your Proxy”, 80);
System.out.println(“AuthEndPoint: ” + authEndPoint);
config.setTraceFile(“traceLogs.txt”);
config.setTraceMessage(true);
config.setPrettyPrintXml(true);

con = new EnterpriseConnection(config);


GetUserInfoResult userInfo = con.getUserInfo();
System.out.println(“nLogging in …n”);
System.out.println(“UserID: ” + userInfo.getUserId());
System.out.println(“User Full Name: ” +
userInfo.getUserFullName());
System.out.println(“User Email: ” +
userInfo.getUserEmail());
System.out.println();
System.out.println(“SessionID: ” +
config.getSessionId());
System.out.println(“Auth End Point: ” +
config.getAuthEndpoint());
System.out.println(“Service End Point: ” +
config.getServiceEndpoint());
System.out.println();
success = true;
}
catch (ConnectionException ce)
{
ce.printStackTrace();
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();




}
return success;
}
public void logout()
{
try
{
con.logout();
System.out.println(“Logged out”);
}
catch (ConnectionException ce)
{
ce.printStackTrace();
}
}
/**
* To determine the objects that are available to the logged-in
* user, the sample client application executes a describeGlobal
* call, which returns all of the objects that are visible to
* the logged-in user. This call should not be made more than
* once per session, as the data returned from the call likely
* does not change frequently. The DescribeGlobalResult is
* simply echoed to the console.
*/
public void describeGlobalSample()
{
try
{
DescribeGlobalResult describeGlobalResult =
con.describeGlobal();
DescribeGlobalSObjectResult[] sobjectResults =
describeGlobalResult.getSobjects();
for (int i = 0; i < sobjectResults.length; i++)
{
System.out.println(sobjectResults[i].getName());
}
}
catch (ConnectionException ce)
{
ce.printStackTrace();
}
}
/**
* The following code segment illustrates the type of metadata
* information that can be obtained for each object available
* to the user. The sample client application executes a
* describeSObject call on a given object and then echoes
* the returned metadata information to the console. Object
* metadata information includes permissions, field types
* and length and available values for picklist fields
* and types for referenceTo fields.
*/
private void describeSample()
{
String objectToDescribe = getUserInput(“nType the name of the object to ” +
“describe (Example: Account): “);
try
{
DescribeSObjectResult describeSObjectResult =
con.describeSObject(objectToDescribe);
if (describeSObjectResult != null)
{
Field[] fields = describeSObjectResult.getFields();
System.out.println(“Metadata for the ” +
describeSObjectResult.getName() + ” SObject”
);
System.out.println(“tActivateable: ” +
describeSObjectResult.isActivateable()
);
System.out.println(“tNumber of fields: ” +
fields.length
);
if (fields != null)
{
for (Field field : fields)
{
//String name = field.getName();
System.out.println(“tField name: ” +
field.getName()
);
PicklistEntry[] picklistValues =
field.getPicklistValues();
if (picklistValues != null && picklistValues.length > 0)
{
System.out.println(“ttPicklist values: “);
for (int j = 0; j < picklistValues.length; j++)
{
if (picklistValues[j].getLabel() != null)
{
System.out.println(“ttValue: ” +
picklistValues[j].getLabel()
);
}
}
}
String[] referenceTos = field.getReferenceTo();
if (referenceTos != null && referenceTos.length > 0)
{
System.out.println(“ttThis field references the ” +
“following objects:”
);
for (int j = 0; j < referenceTos.length; j++)
{
System.out.println(“tt” + referenceTos[j]);
}
}
}
}
}
}
catch (ConnectionException ce)
{
ce.printStackTrace();
}
}
public void querySample()
{
try
{
String soqlQuery = “SELECT FirstName, LastName FROM Contact”;
QueryResult result = con.query(soqlQuery);
boolean done = false;
if (result.getSize() > 0)
{
System.out.println(“nLogged-in user can see ” +
result.getRecords().length +
” contact records.”
);
while (! done)
{
SObject[] records = result.getRecords();
for ( int i = 0; i < records.length; ++i )
{
Contact con = (Contact) records[i];
String fName = con.getFirstName();
String lName = con.getLastName();
if (fName == null)
{
System.out.println(“Contact ” + (i + 1) +
“: ” + lName
);
}
else
{
System.out.println(“Contact ” + (i + 1) + “: ” +
fName + ” ” + lName
);
}
}
if (result.isDone())
{
done = true;
}
else
{
result =
con.queryMore(result.getQueryLocator());
}
}
}
else
{
System.out.println(“No records found.”);
}
System.out.println(“nQuery succesfully executed.”);
}
catch (ConnectionException ce)
{
ce.printStackTrace();
}
}
}

Leave a Reply