use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.
the class PartnerSamples method upsertSample.
private void upsertSample() {
// call the login function to do so
if (!loggedIn) {
if (!login()) {
return;
}
}
try {
DescribeSObjectResult dsr = binding.describeSObject("Account");
HashMap fieldMap = makeFieldMap(dsr.getFields());
if (!fieldMap.containsKey("External_Id__c")) {
System.out.println("\n\nATTENTION: To run this sample you need to \ncreate a custom text field on the Account object \nnamed External_Id with a length of 8 characters \nand with the 'external id' checkbox checked.");
} else {
// First, we need to make sure the test accounts do not exist.
QueryResult qr = binding.query("Select Id From Account Where External_Id__c = '11111111' or External_Id__c = '22222222'");
if (qr.getSize() > 0) {
SObject[] accounts = qr.getRecords();
// Get the ids
String[] ids = new String[accounts.length];
for (int i = 0; i < ids.length; i++) {
ids[i] = accounts[i].getId();
}
// Delete the accounts
binding.delete(ids);
}
// Create a new account using create, we wil use this to update via upsert
// We will set the external id to be ones so that we can use that value for the upsert
SObject newAccount = new SObject();
newAccount.setType("Account");
MessageElement acctName = newMessageElement("Name", "Account to update");
MessageElement extId = newMessageElement("External_Id__c", "11111111");
newAccount.set_any(new MessageElement[] { acctName, extId });
binding.create(new SObject[] { newAccount });
// Now we will create an account that should be updated on insert based
// on the external id field.
SObject updateAccount = new SObject();
updateAccount.setType("Account");
MessageElement webSite = newMessageElement("Website", "http://www.website.com");
MessageElement extId1 = newMessageElement("External_Id__c", "11111111");
updateAccount.set_any(new MessageElement[] { webSite, extId1 });
// This account is meant to be new
SObject createAccount = new SObject();
createAccount.setType("Account");
MessageElement cacctName = newMessageElement("Name", "My Company, Inc");
MessageElement extId3 = newMessageElement("External_Id__c", "22222222");
createAccount.set_any(new MessageElement[] { cacctName, extId3 });
// We have our two accounts, one should be new, the other should be updated.
try {
// Invoke the upsert call and save the results.
// Use External_Id custom field for matching records
UpsertResult[] upsertResults = binding.upsert("External_Id__c", new SObject[] { createAccount, updateAccount });
for (UpsertResult result : upsertResults) {
if (result.isSuccess()) {
System.out.println("\nUpsert succeeded.");
System.out.println((result.isCreated() ? "Inserted" : "Updated") + " account, id is " + result.getId().toString());
} else {
System.out.println("The Upsert failed because: " + result.getErrors(0).getMessage());
}
}
} catch (RemoteException ex) {
System.out.println("An unexpected error has occurred." + ex.getMessage());
}
}
getUserInput("\nPress the RETURN key to continue...");
} catch (ApiFault e) {
System.out.println("Error merging account: " + e.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (RemoteException e) {
System.out.println("Error from the server on the merge sample: " + e.getMessage());
getUserInput("\nHit return to continue...");
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.
the class PartnerSamples method getLeadStatus.
private String getLeadStatus() throws UnexpectedErrorFault, RemoteException {
QueryResult qr = binding.query("Select Id, MasterLabel from LeadStatus Where IsConverted = true");
if (qr.getSize() > 0) {
System.out.println("\n");
for (int i = 0; i < qr.getRecords().length; i++) {
System.out.println(new Integer(i + 1).toString() + ": " + getFieldValue(qr.getRecords(i).get_any(), "MasterLabel"));
}
String stat = getUserInput("\nEnter the number of the status to use. ");
if (stat != null)
return getFieldValue(qr.getRecords(new Integer(stat).intValue() - 1).get_any(), "MasterLabel");
else
return getFieldValue(qr.getRecords(0).get_any(), "MasterLabel");
} else {
return null;
}
}
use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.
the class Test4Feature8540 method doQuery.
public void doQuery() throws Exception {
binding = (SoapBindingStub) new SforceServiceLocator().getSoap(new URL("https://www.salesforce.com/services/Soap/u/16.0"));
binding.setTimeout(60000);
LoginResult loginResult = binding.login("musicatcher@gmail.com", "1234qwer");
binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
SessionHeader sh = new SessionHeader();
sh.setSessionId(loginResult.getSessionId());
binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
// com.sforce.soap.partner.QueryOptions qOptions = new com.sforce.soap.partner.QueryOptions();
// qOptions.setBatchSize(new Integer(2));
// binding.setHeader(
// new com.sforce.soap.partner.SforceServiceLocator()
// .getServiceName().getNamespaceURI(),
// "QueryOptions", qOptions);
QueryResult qr = binding.query("SELECT Name, Type, Phone, Account.CreatedBy.CreatedBy.CreatedBy.Email, Account.Owner.city, (SELECT Contact.LastName, Contact.FirstName " + "FROM Account.Contacts Order By Contact.LastName), (SELECT Note.Title FROM Account.Notes), Account.Owner.Country " + "FROM Account WHERE Name !='United Oil & Gas Corp.'");
// QueryResult qr = binding
// .query("select Name,Id,Type from Account");
TopQueryResult topqr = new TopQueryResult();
topqr.processTopQueryResult(qr);
topqr.printResult();
List<TopRecord> allTopRecords = topqr.getAllTopRecords();
}
use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.
the class PartnerSamples method querySample.
private void querySample() {
// call the login function to do so
if (!loggedIn) {
if (!login()) {
return;
}
}
QueryResult qr = null;
QueryOptions qo = new QueryOptions();
qo.setBatchSize(new Integer(3));
binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "QueryOptions", qo);
try {
qr = binding.query("select id, Website, Name from Account where Name = 'Golden Straw'");
ArrayList records = this.loadQueryResults(qr);
if (records.size() != 0) {
HashMap record = (HashMap) records.get(0);
System.out.println("Retrieved " + new Integer(records.size()).toString() + " account(s) using Name = 'Golden Straw', String = " + (record.containsKey("id") ? record.get("id").toString() : " ").toString() + ", website = " + (record.containsKey("website") ? record.get("website").toString() : " ").toString());
}
// if (qr.getSize() != 0) {
// SObject account = qr.getRecords()[0];
//
// System.out.println("Retrieved "
// + new Integer(qr.getSize()).toString()
// + " account(s) using Name = 'Golden Straw', String = "
// + account.getId() + ", website = "
// + account.get_any()[1]);
// }
qr = binding.query("select FirstName, LastName from Contact");
int loopCount = 0;
boolean continueLoop = true;
while (continueLoop) {
System.out.println("Results set " + new Integer(loopCount++).toString() + " - ");
// process the query results
for (int i = 0; i < qr.getRecords().length; i++) {
SObject con = qr.getRecords()[i];
String fName = "";
String lName = "";
if (con.get_any()[0].getName().toLowerCase().equals("firstname")) {
fName = con.get_any()[0].getValue();
lName = con.get_any()[1].getValue();
} else
lName = con.get_any()[0].getValue();
if (fName == null) {
System.out.println("Contact " + (i + 1) + ": " + lName);
} else {
System.out.println("Contact " + (i + 1) + ": " + fName + " " + lName);
}
}
// recent queryResult
if (qr.isDone())
continueLoop = false;
else
qr = binding.queryMore(qr.getQueryLocator());
}
System.out.println("\nQuery succesfully executed.");
getUserInput("\nHit return to continue...");
} catch (ApiFault af) {
System.out.println("\nFailed to execute query succesfully, error message was: \n" + af.getExceptionMessage());
getUserInput("\nHit return to continue...");
} catch (Exception ex) {
System.out.println("\nFailed to execute query succesfully, error message was: \n" + ex.getMessage());
getUserInput("\nHit return to continue...");
}
}
use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.
the class PartnerSamples method getContact.
private SObject getContact() throws Exception {
SObject contactToReturn = null;
// get a list of contacts so the user can select one
QueryResult qr = binding.query("Select Id, FirstName, LastName, AccountId from Contact Where not AccountId = null");
if (qr.getSize() == 0) {
// No leads where found that have not been
// converted, so....
// we will create a lead and then run the query again
System.out.println("No contacts found, will create one for you...");
this.createContactSample();
qr = binding.query("Select Id, FirstName, LastName, AccountId from Contact");
}
if (qr.getSize() > 0) {
for (int i = 0; i < qr.getRecords().length; i++) {
SObject contact = qr.getRecords(i);
System.out.println(new Integer(i + 1).toString() + ": " + getFieldValue(contact.get_any(), "FirstName") + " " + getFieldValue(contact.get_any(), "LastName"));
}
String selectedContact = getUserInput("/nSelect the number of the contact to use. ");
if (selectedContact != null) {
try {
int selContact = new Integer(selectedContact).intValue();
contactToReturn = qr.getRecords(selContact - 1);
} catch (Exception ex) {
System.out.println("The number you selected is not valid, exiting...");
}
}
}
if (contactToReturn == null) {
throw new Exception("No contact was selected, conversion failed.");
} else {
return contactToReturn;
}
}
Aggregations